Skip to content

Commit

Permalink
feat: implement add new item order on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
belsman committed Aug 29, 2024
1 parent 74d1c75 commit 97ef2ae
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 140 deletions.
1 change: 1 addition & 0 deletions packages/modules/order/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './lib/modules-order.module';
export * from './lib/lib.routes';
export * from './lib/utils';
135 changes: 2 additions & 133 deletions packages/modules/order/src/lib/components/order-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { map, tap } from 'rxjs/operators';
import { VCLFormFieldSchemaRoot } from '@vcl/ng-vcl';

import { ROUTER } from '@console-core/config';
import {
IIoRestorecommerceOrderItem,
IIoRestorecommerceOrderOrder,
IoRestorecommerceAmountAmount,
IoRestorecommerceOrderItem,
ModeType,
} from '@console-core/graphql';
import { ModeType } from '@console-core/graphql';
import {
OrderFacade,
RouterFacade,
Expand All @@ -26,132 +20,7 @@ import {
import { IOrder } from '@console-core/types';

import { buildOrderSchema } from '../jss-forms';

export const transformOrderToInput = (
order: IOrder
): IIoRestorecommerceOrderOrder => {
const inputItems: IIoRestorecommerceOrderItem[] = order.items
? order.items.map(
(item: IoRestorecommerceOrderItem): IIoRestorecommerceOrderItem => {
return {
// id: item.id,
productId: item.productId,
variantId: item.variantId,
parentItemId: item.parentItemId,
quantity: item.quantity,
unitPrice: {
regularPrice: item.unitPrice?.regularPrice,
sale: item.unitPrice?.sale,
salePrice: item.unitPrice?.salePrice,
currencyId: item.unitPrice?.currencyId,
},
};
}
)
: [];

const totalAmounts = order.totalAmounts
? order.totalAmounts.map((orderAmount): IoRestorecommerceAmountAmount => {
return {
gross: orderAmount.gross,
net: orderAmount.net,
currencyId: orderAmount.currencyId,
};
})
: [];

return {
id: order.id,
shopId: order.shopId,
notificationEmail: order.notificationEmail,
orderState: order.orderState,
customerType: order.customerType,
customerVatId: order.customerVatId,
paymentMethodId: order.paymentMethodId,
shippingAddress: {
address: {
// id: order.shippingAddress?.address?.id,
locality: order.shippingAddress?.address?.locality,
street: order.shippingAddress?.address?.street,
region: order.shippingAddress?.address?.region,
countryId: order.shippingAddress?.address?.countryId,
buildingNumber: order.shippingAddress?.address?.buildingNumber,
postcode: order.shippingAddress?.address?.postcode,
// altitude: null,
// addressAddition: {
// field1: '',
// field2: '',
// },
// businessAddress: {
// name: order.shippingAddress?.address?.businessAddress?.name,
// },
// residentialAddress: {
// title: null,
// givenName: null,
// midName: null,
// familyName: null,
// },
// geoCoordinates: {
// latitude: null,
// longitude: null,
// },
// packStation: {
// provider: null,
// stationNumber: null,
// postNumber: null,
// },
},
contact: {
name: null,
email: null,
phone: null,
},
// comments: null,
},
billingAddress: {
address: {
// id: order.billingAddress?.address?.id,
postcode: order.billingAddress?.address?.postcode,
countryId: order.billingAddress?.address?.countryId,
locality: order.billingAddress?.address?.locality,
street: order.billingAddress?.address?.street,
region: order.billingAddress?.address?.region,
// altitude: null,
buildingNumber: order.billingAddress?.address?.buildingNumber,
// geoCoordinates: {
// latitude: null,
// longitude: null,
// },
// addressAddition: {
// field1: '',
// field2: '',
// },
// businessAddress: {
// name: order.billingAddress?.address?.businessAddress?.name,
// },
// residentialAddress: {
// title: null,
// givenName: null,
// midName: null,
// familyName: null,
// },
// packStation: {
// provider: null,
// stationNumber: null,
// postNumber: null,
// },
},
contact: {
name: null,
email: null,
phone: null,
},
// comments: null,
},
totalAmounts: [...totalAmounts],
items: [...inputItems],
};
};
import { transformOrderToInput } from '../utils';

@Component({
selector: 'app-module-order-edit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
RouterFacade,
filterEmptyAndNullishAndUndefined,
} from '@console-core/state';
import { IOrder, IProduct } from '@console-core/types';

import { OrderItemFormComponent } from '../modals/order-item/order-item-form.component';

Expand All @@ -26,7 +27,7 @@ import { OrderItemFormComponent } from '../modals/order-item/order-item-form.com
<ng-container *ngIf="vm$ | async as vm">
<rc-order-view
[order]="vm.order"
(openAddItemModal)="onAddOrder()"
(openAddItemModal)="onAddOrder(vm.order, vm.products)"
/>
</ng-container>
`,
Expand Down Expand Up @@ -77,11 +78,12 @@ export class OrderViewComponent implements OnInit, OnDestroy {
this.addItemLayer?.destroy();
}

onAddOrder() {
onAddOrder(order: IOrder, products: IProduct[]) {
this.addItemLayer
.open({
data: {
products: [], //this.products,
order,
products,
},
})
.subscribe((result) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import { SubSink } from 'subsink';

import { ComponentLayerRef } from '@vcl/ng-vcl';

import { IoRestorecommerceProductPhysicalVariant } from '@console-core/graphql';
import { IProduct } from '@console-core/types';
import {
IIoRestorecommerceOrderOrder,
IoRestorecommerceProductPhysicalVariant,
ModeType,
} from '@console-core/graphql';
import { OrderFacade } from '@console-core/state';
import { IOrder, IProduct } from '@console-core/types';

import { transformOrderToInput } from '../../utils';

@Component({
selector: 'app-order-item-form',
Expand All @@ -33,7 +40,10 @@ export class OrderItemFormComponent implements OnInit, OnDestroy {
}),
});

constructor(private layer: ComponentLayerRef) {}
constructor(
private layer: ComponentLayerRef,
private readonly orderFacade: OrderFacade
) {}

ngOnInit(): void {
this.subscriptions.add(
Expand Down Expand Up @@ -71,13 +81,38 @@ export class OrderItemFormComponent implements OnInit, OnDestroy {
this.subscriptions.unsubscribe();
}

get order(): IOrder {
return this.layer.data.order;
}

get products(): IProduct[] {
return this.layer.data.products;
}

onSubmit(): void {
if (this.formGroup.valid) {
console.log(this.formGroup.value);
const currentOrderInput = transformOrderToInput(this.order);
const updatedOrderInput: IIoRestorecommerceOrderOrder = {
...currentOrderInput,
items: [
...(currentOrderInput.items || []),
{
...this.formGroup.value,
quantity: +(this.formGroup.value.quantity || 0),
unitPrice: {
salePrice: +(this.formGroup.value.unitPrice?.salePrice || 0),
regularPrice: +(
this.formGroup.value.unitPrice?.regularPrice || 0
),
},
},
],
};

this.orderFacade.update({
items: [updatedOrderInput],
mode: ModeType.Update,
});
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/modules/order/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './order.utils';
133 changes: 133 additions & 0 deletions packages/modules/order/src/lib/utils/order.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {
IIoRestorecommerceOrderItem,
IIoRestorecommerceOrderOrder,
IoRestorecommerceAmountAmount,
IoRestorecommerceOrderItem,
} from '@console-core/graphql';
import { IOrder } from '@console-core/types';

export const transformOrderToInput = (
order: IOrder
): IIoRestorecommerceOrderOrder => {
const inputItems: IIoRestorecommerceOrderItem[] = order.items
? order.items.map(
(item: IoRestorecommerceOrderItem): IIoRestorecommerceOrderItem => {
return {
// id: item.id,
productId: item.productId,
variantId: item.variantId,
parentItemId: item.parentItemId,
quantity: item.quantity,
unitPrice: {
regularPrice: item.unitPrice?.regularPrice,
sale: item.unitPrice?.sale,
salePrice: item.unitPrice?.salePrice,
currencyId: item.unitPrice?.currencyId,
},
};
}
)
: [];

const totalAmounts = order.totalAmounts
? order.totalAmounts.map((orderAmount): IoRestorecommerceAmountAmount => {
return {
gross: orderAmount.gross,
net: orderAmount.net,
currencyId: orderAmount.currencyId,
};
})
: [];

return {
id: order.id,
shopId: order.shopId,
notificationEmail: order.notificationEmail,
orderState: order.orderState,
customerType: order.customerType,
customerVatId: order.customerVatId,
paymentMethodId: order.paymentMethodId,
shippingAddress: {
address: {
// id: order.shippingAddress?.address?.id,
locality: order.shippingAddress?.address?.locality,
street: order.shippingAddress?.address?.street,
region: order.shippingAddress?.address?.region,
countryId: order.shippingAddress?.address?.countryId,
buildingNumber: order.shippingAddress?.address?.buildingNumber,
postcode: order.shippingAddress?.address?.postcode,
// altitude: null,
// addressAddition: {
// field1: '',
// field2: '',
// },
// businessAddress: {
// name: order.shippingAddress?.address?.businessAddress?.name,
// },
// residentialAddress: {
// title: null,
// givenName: null,
// midName: null,
// familyName: null,
// },
// geoCoordinates: {
// latitude: null,
// longitude: null,
// },
// packStation: {
// provider: null,
// stationNumber: null,
// postNumber: null,
// },
},
contact: {
name: null,
email: null,
phone: null,
},
// comments: null,
},
billingAddress: {
address: {
// id: order.billingAddress?.address?.id,
postcode: order.billingAddress?.address?.postcode,
countryId: order.billingAddress?.address?.countryId,
locality: order.billingAddress?.address?.locality,
street: order.billingAddress?.address?.street,
region: order.billingAddress?.address?.region,
// altitude: null,
buildingNumber: order.billingAddress?.address?.buildingNumber,
// geoCoordinates: {
// latitude: null,
// longitude: null,
// },
// addressAddition: {
// field1: '',
// field2: '',
// },
// businessAddress: {
// name: order.billingAddress?.address?.businessAddress?.name,
// },
// residentialAddress: {
// title: null,
// givenName: null,
// midName: null,
// familyName: null,
// },
// packStation: {
// provider: null,
// stationNumber: null,
// postNumber: null,
// },
},
contact: {
name: null,
email: null,
phone: null,
},
// comments: null,
},
totalAmounts: [...totalAmounts],
items: [...inputItems],
};
};

0 comments on commit 97ef2ae

Please sign in to comment.