Skip to content

Commit

Permalink
feat: create order fulfilment mediator in the order effect
Browse files Browse the repository at this point in the history
  • Loading branch information
belsman committed Oct 11, 2024
1 parent cf95535 commit 3d6d7aa
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
75 changes: 73 additions & 2 deletions packages/core/state/src/lib/+state/order/order.effects.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { of, throwError } from 'rxjs';
import { catchError, exhaustMap, map, switchMap, tap } from 'rxjs/operators';

import { ROUTER } from '@console-core/config';
import {
IIoRestorecommerceStatusStatus,
IIoRestorecommerceFulfillmentFulfillmentList,
IoRestorecommerceFulfillmentFulfillmentState,
IoRestorecommerceResourcebaseFilterOperation,
IoRestorecommerceResourcebaseFilterValueType,
ModeType,
} from '@console-core/graphql';
import {
ENotificationTypes,
Expand All @@ -17,9 +21,56 @@ 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 = (
_: IOrder
): IIoRestorecommerceFulfillmentFulfillmentList => {
const orderToFulfillment: IIoRestorecommerceFulfillmentFulfillmentList = {
items: [
{
id: '',
customerId: '',
shopId: '',
userId: '',
fulfillmentState:
IoRestorecommerceFulfillmentFulfillmentState.Submitted,
labels: [
{
parcelId: '',
url: '',
pdf: '',
png: '',
shipmentNumber: '',
status: '' as IIoRestorecommerceStatusStatus,
state: IoRestorecommerceFulfillmentFulfillmentState.Submitted,
},
],
packaging: {
invoiceNumber: '',
exportType: '',
exportDescription: '',
notify: '',
parcels: [],
sender: {},
recipient: {},
},
references: [
{
instanceId: '',
instanceType: '',
},
],
},
],
mode: ModeType.Create,
};

return orderToFulfillment;
};
@Injectable()
export class OrderEffects {
orderReadRequest$ = createEffect(() => {
Expand Down Expand Up @@ -241,6 +292,25 @@ export class OrderEffects {
);
});

createFulfilmentForSelectedOrder$ = createEffect(() => {
return this.actions$.pipe(
ofType(orderActions.createFulfilment),
concatLatestFrom(() => this.orderFacade.selected$),
exhaustMap(([, selectedOrder]) => {
if (!selectedOrder) {
return throwError(() => new Error('No selected Order'));
}

const fulfilmentInput = mapOrderToFulfilment(selectedOrder);
return of(
fulfillmentActions.fulfillmentCreateRequest({
payload: fulfilmentInput,
})
);
})
);
});

handleNotificationErrors$ = createEffect(
() => {
return this.actions$.pipe(
Expand Down Expand Up @@ -268,6 +338,7 @@ export class OrderEffects {
private readonly actions$: Actions,
private readonly appFacade: AppFacade,
private readonly orderService: OrderService,
private readonly orderFacade: OrderFacade,
private readonly errorHandlingService: ErrorHandlingService
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ export class OrderTemplateComponent implements OnInit, OnDestroy {
if (id === null) {
return;
}
console.log(
'[Log] ~ file: order-template.component.ts:118 ~ OrderTemplateComponent ~ tap ~ console: triggerCreateFulfillment --->',
id
);

this.orderFacade.createFulfilment(id);

Expand Down

0 comments on commit 3d6d7aa

Please sign in to comment.