From 89412b94e38641b227ef1c880c24cac9fb255838 Mon Sep 17 00:00:00 2001 From: Gerald Baulig Date: Tue, 12 Mar 2024 12:48:45 +0100 Subject: [PATCH] intermediate --- src/services/fulfillment.ts | 62 ++++++++++--- src/services/fulfillment_product.ts | 136 +++++++++++++++++++++++++++- src/stubs/dummy.ts | 0 3 files changed, 182 insertions(+), 16 deletions(-) create mode 100644 src/stubs/dummy.ts diff --git a/src/services/fulfillment.ts b/src/services/fulfillment.ts index 6d071bf..4b5879a 100644 --- a/src/services/fulfillment.ts +++ b/src/services/fulfillment.ts @@ -8,12 +8,14 @@ import { access_controlled_function, access_controlled_service, DefaultACSClientContextFactory, - Operation + Operation, + DefaultResourceFactory, + injects_meta_data } from '@restorecommerce/acs-client'; import { DatabaseProvider } from '@restorecommerce/chassis-srv'; import { Topic } from '@restorecommerce/kafka-client'; import { DeepPartial } from '@restorecommerce/kafka-client/lib/protos'; -import { ReadRequest } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base'; +import { DeleteRequest, ReadRequest } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base'; import { OperationStatus, Status, @@ -89,7 +91,7 @@ export class FulfillmentService { private static async ACSContextFactory( self: FulfillmentService, - request: FulfillmentList | FulfillmentIdList | FulfillmentInvoiceRequestList, + request: FulfillmentList & FulfillmentIdList & FulfillmentInvoiceRequestList, context: any, ): Promise { const ids = request.items?.map((item: any) => item.id); @@ -97,7 +99,10 @@ export class FulfillmentService return { ...context, subject: request.subject, - resources, + resources: [ + ...resources.items ?? [], + ...request.items ?? [], + ], }; } @@ -348,7 +353,11 @@ export class FulfillmentService )); } - protected getFulfillmentsByIds(ids: string[], subject?: Subject, context?: any): Promise> { + protected getFulfillmentsByIds( + ids: string[], + subject?: Subject, + context?: any + ): Promise> { ids = [...new Set(ids).values()]; if (ids.length > 1000) { throw { @@ -645,11 +654,12 @@ export class FulfillmentService return super.read(request, context); } + @injects_meta_data() @access_controlled_function({ action: AuthZAction.CREATE, operation: Operation.isAllowed, context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + resource: DefaultResourceFactory('fulfillment'), database: 'arangoDB', useCache: true, }) @@ -657,6 +667,13 @@ export class FulfillmentService request: FulfillmentList, context?: any ) { + request?.items?.forEach( + item => { + if (!item.state || item.state === State.UNRECOGNIZED) { + item.state = State.CREATED; + } + } + ); return super.create(request, context); } @@ -664,7 +681,7 @@ export class FulfillmentService action: AuthZAction.MODIFY, operation: Operation.isAllowed, context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + resource: DefaultResourceFactory('fulfillment'), database: 'arangoDB', useCache: true, }) @@ -675,11 +692,12 @@ export class FulfillmentService return super.update(request, context); } + @injects_meta_data() @access_controlled_function({ action: AuthZAction.MODIFY, operation: Operation.isAllowed, context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + resource: DefaultResourceFactory('fulfillment'), database: 'arangoDB', useCache: true, }) @@ -693,8 +711,8 @@ export class FulfillmentService @access_controlled_function({ action: AuthZAction.EXECUTE, operation: Operation.isAllowed, - context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + context: DefaultACSClientContextFactory, + resource: DefaultResourceFactory('fulfillment'), database: 'arangoDB', useCache: true, }) @@ -726,11 +744,12 @@ export class FulfillmentService } } + @injects_meta_data() @access_controlled_function({ action: AuthZAction.EXECUTE, operation: Operation.isAllowed, context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + resource: DefaultResourceFactory('execution.submitFulfillments'), database: 'arangoDB', useCache: true, }) @@ -805,7 +824,7 @@ export class FulfillmentService action: AuthZAction.EXECUTE, operation: Operation.isAllowed, context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + resource: DefaultResourceFactory('execution.trackFulfillments'), database: 'arangoDB', useCache: true, }) @@ -960,7 +979,7 @@ export class FulfillmentService action: AuthZAction.EXECUTE, operation: Operation.isAllowed, context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + resource: DefaultResourceFactory('execution.withdrawOrder'), database: 'arangoDB', useCache: true, }) @@ -972,7 +991,7 @@ export class FulfillmentService action: AuthZAction.EXECUTE, operation: Operation.isAllowed, context: FulfillmentService.ACSContextFactory, - resource: [{ resource: 'fulfillment' }], + resource: DefaultResourceFactory('execution.cancelFulfillments'), database: 'arangoDB', useCache: true, }) @@ -1088,6 +1107,21 @@ export class FulfillmentService } } + @access_controlled_function({ + action: AuthZAction.DELETE, + operation: Operation.isAllowed, + context: FulfillmentService.ACSContextFactory, + resource: DefaultResourceFactory('order'), + database: 'arangoDB', + useCache: true, + }) + public override delete( + request: DeleteRequest, + context: any, + ) { + return super.delete(request, context); + } + @access_controlled_function({ action: AuthZAction.CREATE, operation: Operation.isAllowed, diff --git a/src/services/fulfillment_product.ts b/src/services/fulfillment_product.ts index cfda120..3a51093 100644 --- a/src/services/fulfillment_product.ts +++ b/src/services/fulfillment_product.ts @@ -9,7 +9,7 @@ import { ResourcesAPIBase, ServiceBase } from '@restorecommerce/resource-base-in import { DatabaseProvider } from '@restorecommerce/chassis-srv'; import { Topic } from '@restorecommerce/kafka-client'; import { DeepPartial } from '@restorecommerce/kafka-client/lib/protos'; -import { ReadRequest } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base'; +import { DeleteRequest, ReadRequest } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base'; import { FilterOp_Operator, Filter_Operation, @@ -66,6 +66,16 @@ import { CustomerResponseMap, filterTax, } from '..'; +import { + ACSClientContext, + AuthZAction, + DefaultACSClientContextFactory, + DefaultResourceFactory, + Operation, + access_controlled_function, + access_controlled_service, + injects_meta_data +} from '@restorecommerce/acs-client'; interface PackageSolutionTotals extends PackingSolutionQuery { @@ -107,10 +117,28 @@ const countItems = (goods: Item[], container: Container) => { return [...item_map.values()]; }; +@access_controlled_service export class FulfillmentProductService extends ServiceBase implements FulfillmentProductServiceImplementation { + private static async ACSContextFactory( + self: FulfillmentProductService, + request: FulfillmentProductList, + context: any, + ): Promise { + const ids = request.items?.map((item: any) => item.id); + const resources = await self.getFulfillmentProductsByIds(ids, request.subject, context); + return { + ...context, + subject: request.subject, + resources: [ + ...resources.items ?? [], + ...request.items ?? [], + ], + }; + } + protected readonly status_codes: { [key: string]: Status } = { OK: { id: '', @@ -383,6 +411,33 @@ export class FulfillmentProductService )); } + protected getFulfillmentProductsByIds( + ids: string[], + subject?: Subject, + context?: any + ): Promise> { + ids = [...new Set(ids).values()]; + if (ids.length > 1000) { + throw { + code: 500, + message: 'Query for fulfillmentProducts exceeds limit of 1000!' + } as OperationStatus + } + + const request = ReadRequest.fromPartial({ + filters: [{ + filters: [{ + field: 'id', + operation: Filter_Operation.in, + value: JSON.stringify(ids), + type: Filter_ValueType.ARRAY + }] + }], + subject + }); + return super.read(request, context); + } + protected async findCouriers( queries: PackageSolutionTotals[], subject?: Subject, @@ -441,7 +496,69 @@ export class FulfillmentProductService subject, }); - return this.read(call, context); + return super.read(call, context); + } + + @access_controlled_function({ + action: AuthZAction.READ, + operation: Operation.whatIsAllowed, + context: DefaultACSClientContextFactory, + resource: [{ resource: 'fulfillment' }], + database: 'arangoDB', + useCache: true, + }) + public override read( + request: ReadRequest, + context?: any + ) { + return super.read(request, context); + } + + @injects_meta_data() + @access_controlled_function({ + action: AuthZAction.CREATE, + operation: Operation.isAllowed, + context: FulfillmentProductService.ACSContextFactory, + resource: DefaultResourceFactory('fulfillment'), + database: 'arangoDB', + useCache: true, + }) + public override create( + request: FulfillmentProductList, + context?: any + ) { + return super.create(request, context); + } + + @access_controlled_function({ + action: AuthZAction.MODIFY, + operation: Operation.isAllowed, + context: FulfillmentProductService.ACSContextFactory, + resource: DefaultResourceFactory('fulfillment'), + database: 'arangoDB', + useCache: true, + }) + public override update( + request: FulfillmentProductList, + context?: any + ) { + return super.update(request, context); + } + + @injects_meta_data() + @access_controlled_function({ + action: AuthZAction.MODIFY, + operation: Operation.isAllowed, + context: FulfillmentProductService.ACSContextFactory, + resource: DefaultResourceFactory('fulfillment'), + database: 'arangoDB', + useCache: true, + }) + public override upsert( + request: FulfillmentProductList, + context?: any + ) { + return super.upsert(request, context); } async find(request: PackingSolutionQueryList, context?: any): Promise { @@ -787,4 +904,19 @@ export class FulfillmentProductService this.catchOperationError(e); } } + + @access_controlled_function({ + action: AuthZAction.DELETE, + operation: Operation.isAllowed, + context: FulfillmentProductService.ACSContextFactory, + resource: DefaultResourceFactory('order'), + database: 'arangoDB', + useCache: true, + }) + public override delete( + request: DeleteRequest, + context: any, + ) { + return super.delete(request, context); + } } \ No newline at end of file diff --git a/src/stubs/dummy.ts b/src/stubs/dummy.ts new file mode 100644 index 0000000..e69de29