Skip to content

Commit

Permalink
fix(acs): fix acs decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed Mar 12, 2024
2 parents cde7cdd + 89412b9 commit f7fe8b1
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 23 deletions.
79 changes: 58 additions & 21 deletions src/services/fulfillment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import {
access_controlled_function,
access_controlled_service,
DefaultACSClientContextFactory,
Operation
Operation,
DefaultResourceFactory,
injects_meta_data
} from '@restorecommerce/acs-client';
import { database } from '@restorecommerce/chassis-srv';
import { Topic } from '@restorecommerce/kafka-client';
import { DeepPartial } from '@restorecommerce/kafka-client/lib/protos.js';
import { ReadRequest } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base.js';
import {
DeleteRequest,
ReadRequest
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base.js';
import {
OperationStatus,
Status,
Expand Down Expand Up @@ -86,15 +91,18 @@ export class FulfillmentService
implements FulfillmentServiceImplementation {
private static async ACSContextFactory(
self: FulfillmentService,
request: FulfillmentList | FulfillmentIdList | FulfillmentInvoiceRequestList,
request: FulfillmentList & FulfillmentIdList & FulfillmentInvoiceRequestList,
context: any,
): Promise<ACSClientContext> {
const ids = request.items?.map((item: any) => item.id);
const resources = await self.getFulfillmentsByIds(ids, request.subject, context);
return {
...context,
subject: request.subject,
resources,
resources: [
...resources.items ?? [],
...request.items ?? [],
],
};
}

Expand Down Expand Up @@ -345,7 +353,11 @@ export class FulfillmentService
));
}

protected getFulfillmentsByIds(ids: string[], subject?: Subject, context?: any): Promise<DeepPartial<FulfillmentListResponse>> {
protected getFulfillmentsByIds(
ids: string[],
subject?: Subject,
context?: any
): Promise<DeepPartial<FulfillmentListResponse>> {
ids = [...new Set(ids).values()];
if (ids.length > 1000) {
throw {
Expand Down Expand Up @@ -642,26 +654,34 @@ 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' }],
context: FulfillmentService.ACSContextFactory,
resource: DefaultResourceFactory('fulfillment'),
database: 'arangoDB',
useCache: true,
})
public override create(
request: FulfillmentList,
context?: any
) {
request?.items?.forEach(
item => {
if (!item.state || item.state === State.UNRECOGNIZED) {
item.state = State.CREATED;
}
}
);
return super.create(request, context);
}

@access_controlled_function({
action: AuthZAction.MODIFY,
operation: Operation.isAllowed,
context: FulfillmentService?.ACSContextFactory,
resource: [{ resource: 'fulfillment' }],
context: FulfillmentService.ACSContextFactory,
resource: DefaultResourceFactory('fulfillment'),
database: 'arangoDB',
useCache: true,
})
Expand All @@ -672,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' }],
context: FulfillmentService.ACSContextFactory,
resource: DefaultResourceFactory('fulfillment'),
database: 'arangoDB',
useCache: true,
})
Expand All @@ -690,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,
})
Expand Down Expand Up @@ -723,11 +744,12 @@ export class FulfillmentService
}
}

@injects_meta_data()
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
context: FulfillmentService?.ACSContextFactory,
resource: [{ resource: 'fulfillment' }],
context: FulfillmentService.ACSContextFactory,
resource: DefaultResourceFactory('execution.submitFulfillments'),
database: 'arangoDB',
useCache: true,
})
Expand Down Expand Up @@ -801,8 +823,8 @@ export class FulfillmentService
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
context: FulfillmentService?.ACSContextFactory,
resource: [{ resource: 'fulfillment' }],
context: FulfillmentService.ACSContextFactory,
resource: DefaultResourceFactory('execution.trackFulfillments'),
database: 'arangoDB',
useCache: true,
})
Expand Down Expand Up @@ -956,8 +978,8 @@ export class FulfillmentService
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
context: FulfillmentService?.ACSContextFactory,
resource: [{ resource: 'fulfillment' }],
context: FulfillmentService.ACSContextFactory,
resource: DefaultResourceFactory('execution.withdrawOrder'),
database: 'arangoDB',
useCache: true,
})
Expand All @@ -968,8 +990,8 @@ export class FulfillmentService
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
context: FulfillmentService?.ACSContextFactory,
resource: [{ resource: 'fulfillment' }],
context: FulfillmentService.ACSContextFactory,
resource: DefaultResourceFactory('execution.cancelFulfillments'),
database: 'arangoDB',
useCache: true,
})
Expand Down Expand Up @@ -1085,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,
Expand Down
139 changes: 137 additions & 2 deletions src/services/fulfillment_product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,22 @@ import {
} from '@restorecommerce/grpc-client';
import { ResourcesAPIBase, ServiceBase } from '@restorecommerce/resource-base-interface';
import { DatabaseProvider } from '@restorecommerce/chassis-srv';
import {
ACSClientContext,
AuthZAction,
DefaultACSClientContextFactory,
DefaultResourceFactory,
Operation,
access_controlled_function,
access_controlled_service,
injects_meta_data
} from '@restorecommerce/acs-client';
import { Topic } from '@restorecommerce/kafka-client';
import { DeepPartial } from '@restorecommerce/kafka-client/lib/protos.js';
import { ReadRequest } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base.js';
import {
DeleteRequest,
ReadRequest
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base.js';
import {
FilterOp_Operator,
Filter_Operation,
Expand Down Expand Up @@ -107,10 +120,28 @@ const countItems = (goods: Item[], container: Container) => {
return [...item_map.values()];
};

@access_controlled_service
export class FulfillmentProductService
extends ServiceBase<FulfillmentProductListResponse, FulfillmentProductList>
implements FulfillmentProductServiceImplementation
{
private static async ACSContextFactory(
self: FulfillmentProductService,
request: FulfillmentProductList,
context: any,
): Promise<ACSClientContext> {
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: '',
Expand Down Expand Up @@ -383,6 +414,33 @@ export class FulfillmentProductService
));
}

protected getFulfillmentProductsByIds(
ids: string[],
subject?: Subject,
context?: any
): Promise<DeepPartial<FulfillmentProductListResponse>> {
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,
Expand Down Expand Up @@ -441,7 +499,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<PackingSolutionListResponse> {
Expand Down Expand Up @@ -787,4 +907,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);
}
}
Empty file added src/stubs/dummy.ts
Empty file.

0 comments on commit f7fe8b1

Please sign in to comment.