Skip to content

Commit

Permalink
chore: refactor order fulfilment creation
Browse files Browse the repository at this point in the history
  • Loading branch information
belsman committed Oct 18, 2024
1 parent 72fb299 commit 009aa4b
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}
}
102 changes: 102 additions & 0 deletions packages/core/graphql/src/lib/generated/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}>;
Expand Down Expand Up @@ -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!
Expand Down
88 changes: 14 additions & 74 deletions packages/core/state/src/lib/+state/order/order.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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 };
})
);
})
);
});
Expand Down
32 changes: 31 additions & 1 deletion packages/core/state/src/lib/services/ordering/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
OrderingOrderMutateMutation,
OrderingOrderReadGQL,
OrderingOrderReadQuery,
CreateOrderFulfillmentGQL,
CreateOrderFulfillmentMutation,
IIoRestorecommerceAddressShippingAddress,
} from '@console-core/graphql';

@Injectable({
Expand All @@ -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(
Expand Down Expand Up @@ -73,4 +77,30 @@ export class OrderService {
input: orderInvoiceInput,
});
}

createFulfilment(payload: {
id: string;
senderAddress: IIoRestorecommerceAddressShippingAddress;
}): Observable<MutationResult<CreateOrderFulfillmentMutation>> {
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,
},
},
},
],
},
});
}
}

0 comments on commit 009aa4b

Please sign in to comment.