From 009aa4b0aa9a7fbadbccb341217d77f62884ffa3 Mon Sep 17 00:00:00 2001 From: Bello Babakolo Date: Fri, 18 Oct 2024 15:26:55 +0100 Subject: [PATCH] chore: refactor order fulfilment creation --- .../ordering/create-order-fulfilment.gql | 36 +++++++ .../graphql/src/lib/generated/generated.ts | 102 ++++++++++++++++++ .../src/lib/+state/order/order.effects.ts | 88 +++------------ .../lib/services/ordering/order.service.ts | 32 +++++- 4 files changed, 183 insertions(+), 75 deletions(-) create mode 100644 packages/core/graphql/src/lib/documents/ordering/create-order-fulfilment.gql diff --git a/packages/core/graphql/src/lib/documents/ordering/create-order-fulfilment.gql b/packages/core/graphql/src/lib/documents/ordering/create-order-fulfilment.gql new file mode 100644 index 00000000..e774e157 --- /dev/null +++ b/packages/core/graphql/src/lib/documents/ordering/create-order-fulfilment.gql @@ -0,0 +1,36 @@ +mutation CreateOrderFulfillment( + $input: IIoRestorecommerceOrderFulfillmentRequestList! +) { + ordering { + order { + CreateFulfillment(input: $input) { + details { + items { + payload { + id + fulfillmentState + labels { + parcelId + pdf + png + shipmentNumber + status { + code + message + } + } + } + status { + code + message + } + } + operationStatus { + code + message + } + } + } + } + } +} diff --git a/packages/core/graphql/src/lib/generated/generated.ts b/packages/core/graphql/src/lib/generated/generated.ts index e53b5d35..5511efee 100644 --- a/packages/core/graphql/src/lib/generated/generated.ts +++ b/packages/core/graphql/src/lib/generated/generated.ts @@ -8905,6 +8905,56 @@ export type MasterDataTimezoneReadQuery = { }; }; +export type CreateOrderFulfillmentMutationVariables = Exact<{ + input: IIoRestorecommerceOrderFulfillmentRequestList; +}>; + +export type CreateOrderFulfillmentMutation = { + __typename?: 'Mutation'; + ordering: { + __typename?: 'OrderingMutation'; + order: { + __typename?: 'OrderingOrderMutation'; + CreateFulfillment?: { + __typename?: 'ProtoIoRestorecommerceFulfillmentFulfillmentListResponse'; + details?: { + __typename?: 'IoRestorecommerceFulfillmentFulfillmentListResponse'; + items?: Array<{ + __typename?: 'IoRestorecommerceFulfillmentFulfillmentResponse'; + payload?: { + __typename?: 'IoRestorecommerceFulfillmentFulfillment'; + id?: string | null; + fulfillmentState?: IoRestorecommerceFulfillmentFulfillmentState | null; + labels?: Array<{ + __typename?: 'IoRestorecommerceFulfillmentLabel'; + parcelId?: string | null; + pdf?: string | null; + png?: string | null; + shipmentNumber?: string | null; + status?: { + __typename?: 'IoRestorecommerceStatusStatus'; + code?: number | null; + message?: string | null; + } | null; + }> | null; + } | null; + status?: { + __typename?: 'IoRestorecommerceStatusStatus'; + code?: number | null; + message?: string | null; + } | null; + }> | null; + operationStatus?: { + __typename?: 'IoRestorecommerceStatusOperationStatus'; + code?: number | null; + message?: string | null; + } | null; + } | null; + } | null; + }; + }; +}; + export type OrderingInvoiceCreateMutationVariables = Exact<{ input: IIoRestorecommerceOrderOrderingInvoiceRequestList; }>; @@ -12105,6 +12155,58 @@ export class MasterDataTimezoneReadGQL extends Apollo.Query< super(apollo); } } +export const CreateOrderFulfillmentDocument = gql` + mutation CreateOrderFulfillment( + $input: IIoRestorecommerceOrderFulfillmentRequestList! + ) { + ordering { + order { + CreateFulfillment(input: $input) { + details { + items { + payload { + id + fulfillmentState + labels { + parcelId + pdf + png + shipmentNumber + status { + code + message + } + } + } + status { + code + message + } + } + operationStatus { + code + message + } + } + } + } + } + } +`; + +@Injectable({ + providedIn: 'root', +}) +export class CreateOrderFulfillmentGQL extends Apollo.Mutation< + CreateOrderFulfillmentMutation, + CreateOrderFulfillmentMutationVariables +> { + override document = CreateOrderFulfillmentDocument; + + constructor(apollo: Apollo.Apollo) { + super(apollo); + } +} export const OrderingInvoiceCreateDocument = gql` mutation OrderingInvoiceCreate( $input: IIoRestorecommerceOrderOrderingInvoiceRequestList! diff --git a/packages/core/state/src/lib/+state/order/order.effects.ts b/packages/core/state/src/lib/+state/order/order.effects.ts index 8332d5fb..7665b0ae 100644 --- a/packages/core/state/src/lib/+state/order/order.effects.ts +++ b/packages/core/state/src/lib/+state/order/order.effects.ts @@ -6,11 +6,9 @@ import { catchError, exhaustMap, map, switchMap, tap } from 'rxjs/operators'; import { ROUTER } from '@console-core/config'; import { - IIoRestorecommerceFulfillmentFulfillmentList, - IoRestorecommerceFulfillmentFulfillmentState, IoRestorecommerceResourcebaseFilterOperation, IoRestorecommerceResourcebaseFilterValueType, - ModeType, + IoRestorecommerceAddressShippingAddress, } from '@console-core/graphql'; import { ENotificationTypes, @@ -20,75 +18,10 @@ import { import { ErrorHandlingService, OrderService } from '../../services'; import { AppFacade } from '../app'; -import * as fulfillmentActions from '../fulfillment/fulfillment.actions'; import * as orderActions from './order.actions'; import { OrderFacade } from './order.facade'; -export const mapOrderToFulfilment = ( - order: IOrder -): IIoRestorecommerceFulfillmentFulfillmentList => { - const parcelItems = order.items?.map((item) => { - return { - productId: item.productId, - variantId: item.variantId, - package: item.product?.product?.physical?.variants?.find( - (variant) => variant.id === item.variantId - )?.package, - quantity: item.quantity, - }; - }); - - const orderToFulfillment: IIoRestorecommerceFulfillmentFulfillmentList = { - items: [ - { - customerId: order.customerId, - shopId: order.shopId, - userId: order.userId, - fulfillmentState: IoRestorecommerceFulfillmentFulfillmentState.Pending, - labels: [], - packaging: { - invoiceNumber: '', - exportType: '', - exportDescription: '', - notify: order.notificationEmail, - parcels: [ - { - productId: 'n-fuse-shop000-fp-dhl-domestic', - variantId: 'n-fuse-shop000-fp-dhl-domestic_max_weight_1kg', - items: parcelItems, - }, - ], - recipient: { - address: { - buildingNumber: order.shippingAddress?.address?.buildingNumber, - street: order.shippingAddress?.address?.street, - locality: order.shippingAddress?.address?.locality, - region: order.shippingAddress?.address?.region, - postcode: order.shippingAddress?.address?.postcode, - countryId: order.shippingAddress?.address?.countryId, - }, - contact: { - name: order.shippingAddress?.contact?.name, - email: order.shippingAddress?.contact?.email, - phone: order.shippingAddress?.contact?.phone, - }, - }, - }, - references: [ - { - instanceId: 'urn:restorecommerce:io:order:Order', - instanceType: order.id, - }, - ], - meta: order.meta, - }, - ], - mode: ModeType.Create, - }; - - return orderToFulfillment; -}; @Injectable() export class OrderEffects { orderReadRequest$ = createEffect(() => { @@ -318,13 +251,20 @@ export class OrderEffects { if (!selectedOrder) { return throwError(() => new Error('No selected Order')); } - - const fulfilmentInput = mapOrderToFulfilment(selectedOrder); - return of( - fulfillmentActions.fulfillmentCreateRequest({ - payload: fulfilmentInput, + return this.orderService + .createFulfilment({ + id: selectedOrder.id, + senderAddress: + selectedOrder.shippingAddress as IoRestorecommerceAddressShippingAddress, }) - ); + .pipe( + map((response) => { + const fulfilmentItems = + response.data?.ordering.order.CreateFulfillment?.details?.items; + + return { type: 'DUMMY', payload: fulfilmentItems }; + }) + ); }) ); }); diff --git a/packages/core/state/src/lib/services/ordering/order.service.ts b/packages/core/state/src/lib/services/ordering/order.service.ts index 7577821a..4c054e3d 100644 --- a/packages/core/state/src/lib/services/ordering/order.service.ts +++ b/packages/core/state/src/lib/services/ordering/order.service.ts @@ -16,6 +16,9 @@ import { OrderingOrderMutateMutation, OrderingOrderReadGQL, OrderingOrderReadQuery, + CreateOrderFulfillmentGQL, + CreateOrderFulfillmentMutation, + IIoRestorecommerceAddressShippingAddress, } from '@console-core/graphql'; @Injectable({ @@ -26,7 +29,8 @@ export class OrderService { private readonly orderingOrderReadGQL: OrderingOrderReadGQL, private readonly orderingOrderMutateGQL: OrderingOrderMutateGQL, private readonly orderingOrderDeleteMutateGQL: OrderingOrderDeleteMutateGQL, - private readonly orderingInvoiceCreateGQL: OrderingInvoiceCreateGQL + private readonly orderingInvoiceCreateGQL: OrderingInvoiceCreateGQL, + private readonly orderingFulfilmentGQL: CreateOrderFulfillmentGQL ) {} read( @@ -73,4 +77,30 @@ export class OrderService { input: orderInvoiceInput, }); } + + createFulfilment(payload: { + id: string; + senderAddress: IIoRestorecommerceAddressShippingAddress; + }): Observable> { + return this.orderingFulfilmentGQL.mutate({ + input: { + items: [ + { + orderId: payload.id, + // selectedItems: [], + senderAddress: { + address: { + buildingNumber: payload.senderAddress.address?.buildingNumber, + street: payload.senderAddress.address?.buildingNumber, + locality: payload.senderAddress.address?.locality, + region: payload.senderAddress.address?.region, + postcode: payload.senderAddress.address?.postcode, + countryId: payload.senderAddress.address?.countryId, + }, + }, + }, + ], + }, + }); + } }