From 359a01799bc4c150bd63d13d123753b43e52f748 Mon Sep 17 00:00:00 2001 From: Gerald Baulig Date: Thu, 2 May 2024 13:39:34 +0200 Subject: [PATCH] fix(submit): ensure fulfillment.shop_id is in courier.shop_ids --- .github/workflows/build.yaml | 5 +- src/services/fulfillment.ts | 123 ++++++++++++++++++++++++++-- src/services/fulfillment_courier.ts | 7 ++ src/services/fulfillment_product.ts | 7 ++ 4 files changed, 133 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d426c83..00c4db7 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -34,10 +34,7 @@ jobs: node-version-file: '.nvmrc' - uses: restorecommerce/setup-system-action@v1 - - - name: Check Env - run: echo a $STUBS__DHLSOAP__DEFAULTS__ORDERING__USERNAME b $stubs__DHLSoap__defaults__ordering__username c $secrets.STUBS__DHLSOAP__DEFAULTS__ORDERING__USERNAME d ${{ secrets.STUBS__DHLSOAP__DEFAULTS__ORDERING__USERNAME }} - + - name: Install Dependencies run: npm ci diff --git a/src/services/fulfillment.ts b/src/services/fulfillment.ts index c863977..143e7ef 100644 --- a/src/services/fulfillment.ts +++ b/src/services/fulfillment.ts @@ -138,6 +138,11 @@ export class FulfillmentService code: 400, message: '{entity} {id} is not submitted!', }, + SHOP_ID_NOT_IDENTICAL: { + id: '', + code: 400, + message: '{entity} {id} Fulfillment.shopId must be listed in Courier.shopIds!', + }, }; protected readonly operation_status_codes: { [key: string]: OperationStatus } = { @@ -293,6 +298,106 @@ export class FulfillmentService } as T; } + protected getProductsBySuper( + ids: string[], + subject?: Subject, + context?: any, + ): Promise> { + ids = [...new Set(ids)]; + + if (ids.length > 1000) { + throwOperationStatusCode( + 'FulfillmentProduct', + this.operation_status_codes.LIMIT_EXHAUSTED, + ); + } + + const request = ReadRequest.fromPartial({ + filters: [{ + filters: [ + { + field: 'id', + operation: Filter_Operation.in, + value: JSON.stringify(ids), + type: Filter_ValueType.ARRAY, + } + ] + }], + limit: ids.length, + subject, + }); + + return this.fulfillmentProductSrv.superRead( + request, + context, + ).then( + (response: any) => { + if (response.operation_status?.code === 200) { + return response.items?.reduce( + (a: ResponseMap, b: any) => { + a[b.payload?.id] = b; + return a; + }, + {} as ResponseMap + ); + } + else { + throw response.operation_status; + } + } + ); + } + + protected getCouriersBySuper( + ids: string[], + subject?: Subject, + context?: any, + ): Promise> { + ids = [...new Set(ids)]; + + if (ids.length > 1000) { + throwOperationStatusCode( + 'Courier', + this.operation_status_codes.LIMIT_EXHAUSTED, + ); + } + + const request = ReadRequest.fromPartial({ + filters: [{ + filters: [ + { + field: 'id', + operation: Filter_Operation.in, + value: JSON.stringify(ids), + type: Filter_ValueType.ARRAY, + } + ] + }], + limit: ids.length, + subject, + }); + + return this.fulfillmentCourierSrv.superRead( + request, + context, + ).then( + (response: any) => { + if (response.operation_status?.code === 200) { + return response.items?.reduce( + (a: ResponseMap, b: any) => { + a[b.payload?.id] = b; + return a; + }, + {} as ResponseMap + ); + } + else { + throw response.operation_status; + } + } + ); + } + protected get( ids: string[], service: CRUDClient, @@ -351,7 +456,7 @@ export class FulfillmentService } else { throwStatusCode( - 'Object', + ({} as new() => T).name, id, this.status_codes.NOT_FOUND ); @@ -458,20 +563,18 @@ export class FulfillmentService context, ); - const product_map = await this.get( + const product_map = await this.getProductsBySuper( fulfillments.flatMap( f => f.packaging.parcels.map(p => p.product_id) ), - this.fulfillmentProductSrv, subject, context, ); - const courier_map = await this.get( + const courier_map = await this.getCouriersBySuper( Object.values(product_map).map( p => p.payload?.courier_id ), - this.fulfillmentCourierSrv, subject, context, ); @@ -509,6 +612,16 @@ export class FulfillmentService products.map( product => product.payload?.courier_id ) + ) + + couriers.every( + courier => courier.payload?.shop_ids?.includes( + item.shop_id + ) + ) || throwStatusCode( + 'Fulfillment', + item.id, + this.status_codes.SHOP_ID_NOT_IDENTICAL, ); const status: Status[] = [ diff --git a/src/services/fulfillment_courier.ts b/src/services/fulfillment_courier.ts index a572b3f..8dabcdb 100644 --- a/src/services/fulfillment_courier.ts +++ b/src/services/fulfillment_courier.ts @@ -89,6 +89,13 @@ export class FulfillmentCourierService return super.read(request, context); } + public superRead( + request: ReadRequest, + context?: any, + ) { + return super.read(request, context); + } + @access_controlled_function({ action: AuthZAction.READ, operation: Operation.whatIsAllowed, diff --git a/src/services/fulfillment_product.ts b/src/services/fulfillment_product.ts index 573daab..17925b3 100644 --- a/src/services/fulfillment_product.ts +++ b/src/services/fulfillment_product.ts @@ -535,6 +535,13 @@ export class FulfillmentProductService ); } + public superRead( + request: ReadRequest, + context?: any, + ) { + return super.read(request, context); + } + @access_controlled_function({ action: AuthZAction.READ, operation: Operation.whatIsAllowed,