Skip to content

Commit

Permalink
fix(submit): ensure fulfillment.shop_id is in courier.shop_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed May 2, 2024
1 parent 3a92a3b commit 359a017
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 9 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
123 changes: 118 additions & 5 deletions src/services/fulfillment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {
Expand Down Expand Up @@ -293,6 +298,106 @@ export class FulfillmentService
} as T;
}

protected getProductsBySuper(
ids: string[],
subject?: Subject,
context?: any,
): Promise<ResponseMap<FulfillmentProduct>> {
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<FulfillmentProduct>, b: any) => {
a[b.payload?.id] = b;
return a;
},
{} as ResponseMap<FulfillmentProduct>
);
}
else {
throw response.operation_status;
}
}
);
}

protected getCouriersBySuper(
ids: string[],
subject?: Subject,
context?: any,
): Promise<ResponseMap<Courier>> {
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<Courier>, b: any) => {
a[b.payload?.id] = b;
return a;
},
{} as ResponseMap<Courier>
);
}
else {
throw response.operation_status;
}
}
);
}

protected get<T>(
ids: string[],
service: CRUDClient,
Expand Down Expand Up @@ -351,7 +456,7 @@ export class FulfillmentService
}
else {
throwStatusCode<T>(
'Object',
({} as new() => T).name,
id,
this.status_codes.NOT_FOUND
);
Expand Down Expand Up @@ -458,20 +563,18 @@ export class FulfillmentService
context,
);

const product_map = await this.get<FulfillmentProduct>(
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<Courier>(
const courier_map = await this.getCouriersBySuper(
Object.values(product_map).map(
p => p.payload?.courier_id
),
this.fulfillmentCourierSrv,
subject,
context,
);
Expand Down Expand Up @@ -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<any>(
'Fulfillment',
item.id,
this.status_codes.SHOP_ID_NOT_IDENTICAL,
);

const status: Status[] = [
Expand Down
7 changes: 7 additions & 0 deletions src/services/fulfillment_courier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/services/fulfillment_product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 359a017

Please sign in to comment.