Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/allow user select multiple addresses #780

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ export class CustomerEditComponent implements OnInit {
protected async updateCustomer() {
const geoLocationInput = this.locationForm.getValue();
geoLocationInput.loc.coordinates.reverse();
this._currentCustomer.setDefaultLocation(geoLocationInput);
const allCustomerAddresses = this._currentCustomer.customerAddress;
try {
this.loading = true;
const customer = await this._customerRouter.updateUser(
this._currentCustomer.id,
{
...this.basicInfoForm.getValue(),
geoLocation: geoLocationInput as IGeoLocation
geoLocation: allCustomerAddresses
}
);
this.loading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class CustomerLocationComponent implements OnDestroy, OnInit {
.pipe(first())
.toPromise();
const coordinates = new google.maps.LatLng(
user['geoLocation'].coordinates.lat,
user['geoLocation'].coordinates.lng
user.getDefaultGeolocation().coordinates.lat,
user.getDefaultGeolocation().coordinates.lng
);
this.showMap(coordinates);
this.marker = this.addMarker(coordinates, this.map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class CustomerProductsComponent implements OnDestroy, OnInit {
}

this.availableProductsSubscription$ = this.geoLocationProductService
.getGeoLocationProducts(user.geoLocation)
.getGeoLocationProducts(user.getDefaultGeolocation())
.subscribe((products) => {
this.availableProducts = products;
this.sourceSmartTable.load(products);
Expand Down
7 changes: 5 additions & 2 deletions backend/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class ApplicationModule implements NestModule, OnModuleInit {
private readonly moduleRef: ModuleRef,
private readonly command$: CommandBus,
private readonly event$: EventBus
) { }
) {}

onModuleInit() {
// initialize CQRS
Expand All @@ -137,7 +137,7 @@ export class ApplicationModule implements NestModule, OnModuleInit {
// https://github.com/apollographql/apollo-server/issues/1649#issuecomment-420840287
const { ObjectId } = mongoose.Types;

ObjectId.prototype.valueOf = function () {
ObjectId.prototype.valueOf = function() {
return this.toString();
};

Expand All @@ -159,6 +159,9 @@ export class ApplicationModule implements NestModule, OnModuleInit {
log.info(
`GraphQL playground available at http://localhost:${port}/graphql`
);
console.log(
`GraphQL playground available at http://localhost:${port}/graphql`
);
}

/*
Expand Down
40 changes: 31 additions & 9 deletions shared/core/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import GeoLocation from './GeoLocation';
import { DBObject, getSchema, ModelName, Schema, Types } from '../@pyro/db';
import IUser, { IUserCreateObject } from '../interfaces/IUser';
import { Entity, Column } from 'typeorm';
import IGeoLocation from '../interfaces/IGeoLocation';
import IGeoLocation, {
IGeoLocationCreateObject
} from '../interfaces/IGeoLocation';

/**
* Customer who make orders
Expand Down Expand Up @@ -174,20 +176,40 @@ class User extends DBObject<IUser, IUserCreateObject> implements IUser {
* @memberof User
*/
get fullAddress(): string {
return '';
// return (
// `${this.geoLocation.city}, ${this.geoLocation.streetAddress} ` +
// `${this.apartment}/${this.geoLocation.house}`
// );
const address = this.getDefaultGeolocation();
if (address) {
return (
`${address.city}, ${address.streetAddress} ` +
`${address.apartment}/${address.house}`
);
} else {
return '';
}
}

get customerAddress(): Array<GeoLocation> {
return this.geoLocation;
}

getDefaultGeolocation() {
getDefaultGeolocation(): GeoLocation | null {
const defaultLocation = this.geoLocation.filter(
(location: GeoLocation) => {
return (location.default = true);
return location.default;
}
);
return defaultLocation;
if (defaultLocation) {
return defaultLocation[0];
} else {
return null;
}
}

setDefaultLocation(input: IGeoLocationCreateObject) {
this.customerAddress.forEach((address: GeoLocation) => {
address.default = false;
});
input.default = true;
this.customerAddress.push(input);
}
}

Expand Down
36 changes: 23 additions & 13 deletions shared/core/entities/UserOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,39 @@ class UserOrder extends DBObject<IUserOrder, IUserOrderCreateObject>
@Schema(Boolean)
@Column()
isRegistrationCompleted: boolean;

/**
* Get full address of customer (including apartment)
* Note: does not include country
*
* @readonly
* @memberof UserOrder
* @memberof User
*/
get fullAddress(): string {
const defaultAddress = this.geoLocation.filter(
const address = this.getDefaultGeolocation();
if (address) {
return (
`${address.city}, ${address.streetAddress} ` +
`${address.apartment}/${address.house}`
);
} else {
return '';
}
}

get customerAddress(): Array<GeoLocation> {
return this.geoLocation;
}

getDefaultGeolocation(): GeoLocation | null {
const defaultLocation = this.geoLocation.filter(
(location: GeoLocation) => {
return location.default === true;
return location.default;
}
);
if (defaultAddress) {
return (
`${this.defaultAddress[0].city}, ${
this.defaultAddress[0].streetAddress
} ` +
`${this.defaultAddress[0].apartment}/${
this.defaultAddress[0].house
}`
);
if (defaultLocation) {
return defaultLocation[0];
} else {
return null;
}
}
}
Expand Down