Skip to content

Commit

Permalink
Merge pull request #780 from zessu/feature/allow-user-select-multiple…
Browse files Browse the repository at this point in the history
…-addresses

Feature/allow user select multiple addresses
  • Loading branch information
evereq authored Aug 5, 2019
2 parents 543711d + 50c1201 commit 4b8136e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
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

0 comments on commit 4b8136e

Please sign in to comment.