From 3d6d7aabc5ee6d0b70841d63e3152f7edc818dc7 Mon Sep 17 00:00:00 2001 From: Bello Babakolo Date: Fri, 11 Oct 2024 14:46:01 +0100 Subject: [PATCH] feat: create order fulfilment mediator in the order effect --- .../src/lib/+state/order/order.effects.ts | 75 ++++++++++++++++++- .../template/order-template.component.ts | 4 - 2 files changed, 73 insertions(+), 6 deletions(-) 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 c35e7cc2..a303c863 100644 --- a/packages/core/state/src/lib/+state/order/order.effects.ts +++ b/packages/core/state/src/lib/+state/order/order.effects.ts @@ -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, @@ -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(() => { @@ -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( @@ -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 ) {} } diff --git a/packages/modules/order/src/lib/components/template/order-template.component.ts b/packages/modules/order/src/lib/components/template/order-template.component.ts index c28a4998..b6bfe5e6 100644 --- a/packages/modules/order/src/lib/components/template/order-template.component.ts +++ b/packages/modules/order/src/lib/components/template/order-template.component.ts @@ -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);