Skip to content

Commit

Permalink
fix(find): improve error messages on product find
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed Oct 21, 2024
1 parent 4257f1a commit 4301dde
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 66 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"type": "module",
"dependencies": {
"@restorecommerce/acs-client": "^3.0.0",
"@restorecommerce/acs-client": "^3.0.1",
"@restorecommerce/cart": "^1.0.9",
"@restorecommerce/chassis-srv": "^1.6.2",
"@restorecommerce/grpc-client": "^2.2.4",
Expand Down
13 changes: 12 additions & 1 deletion src/services/fulfillment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
DefaultACSClientContextFactory,
Operation,
DefaultResourceFactory,
injects_meta_data
injects_meta_data,
resolves_subject
} from '@restorecommerce/acs-client';
import { type Logger } from '@restorecommerce/logger';
import { type ServiceConfig } from '@restorecommerce/service-config';
Expand Down Expand Up @@ -827,6 +828,7 @@ export class FulfillmentService
return super.read(request, context);
}

@resolves_subject()
@injects_meta_data()
@access_controlled_function({
action: AuthZAction.CREATE,
Expand All @@ -850,6 +852,7 @@ export class FulfillmentService
return super.create(request, context);
}

@resolves_subject()
@access_controlled_function({
action: AuthZAction.MODIFY,
operation: Operation.isAllowed,
Expand All @@ -865,6 +868,7 @@ export class FulfillmentService
return super.update(request, context);
}

@resolves_subject()
@injects_meta_data()
@access_controlled_function({
action: AuthZAction.MODIFY,
Expand All @@ -881,6 +885,7 @@ export class FulfillmentService
return super.upsert(request, context);
}

@resolves_subject()
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
Expand Down Expand Up @@ -917,6 +922,7 @@ export class FulfillmentService
}
}

@resolves_subject()
@injects_meta_data()
@access_controlled_function({
action: AuthZAction.EXECUTE,
Expand Down Expand Up @@ -993,6 +999,7 @@ export class FulfillmentService
}
}

@resolves_subject()
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
Expand Down Expand Up @@ -1147,6 +1154,7 @@ export class FulfillmentService
}
}

@resolves_subject()
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
Expand All @@ -1159,6 +1167,7 @@ export class FulfillmentService
return null;
}

@resolves_subject()
@access_controlled_function({
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
Expand Down Expand Up @@ -1279,6 +1288,7 @@ export class FulfillmentService
}
}

@resolves_subject()
@access_controlled_function({
action: AuthZAction.DELETE,
operation: Operation.isAllowed,
Expand All @@ -1294,6 +1304,7 @@ export class FulfillmentService
return super.delete(request, context);
}

@resolves_subject()
@access_controlled_function({
action: AuthZAction.CREATE,
operation: Operation.isAllowed,
Expand Down
160 changes: 100 additions & 60 deletions src/services/fulfillment_product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ export class FulfillmentProductService
id: '',
code: 404,
message: '{entity} {id} has no shipping address!',
},
NO_SHOP_ID: {
id: '',
code: 400,
message: 'Shop ID not provided!'
},
NO_CUSTOMER_ID: {
id: '',
code: 400,
message: 'Customer ID not provided!'
}
};

Expand Down Expand Up @@ -462,22 +472,34 @@ export class FulfillmentProductService
}

protected async findCouriers(
queries: PackageSolutionTotals[],
query: PackageSolutionTotals,
subject?: Subject,
context?: any,
): Promise<FulfillmentCourierListResponse> {
const call = ReadRequest.fromPartial({
filters: [{
filters: queries.flatMap(
item => item.preferences?.couriers?.map(
att => ({
field: att.id,
operation: Filter_Operation.eq,
value: att.value,
})
)
).filter(item => !!item),
operator: FilterOp_Operator.or
filters: [{
filters: [{
filters: query.preferences?.couriers?.map(
att => ({
field: att.id,
operation: Filter_Operation.eq,
value: att.value,
})
).filter(item => !!item),
operator: FilterOp_Operator.or
}]
},{
filters: [{
filters: [{
field: 'shop_ids',
operation: Filter_Operation.in,
value: query.shop_id
}],
operator: FilterOp_Operator.and
}],
}],
operator: FilterOp_Operator.and
}],
subject,
});
Expand All @@ -496,12 +518,12 @@ export class FulfillmentProductService
}

protected async findFulfillmentProducts(
queries: PackageSolutionTotals[],
query: PackageSolutionTotals,
subject?: Subject,
context?: any,
): Promise<FulfillmentProductListResponse> {
const stubs = await this.findCouriers(
queries,
query,
subject,
context,
).then(
Expand Down Expand Up @@ -625,14 +647,14 @@ export class FulfillmentProductService
this.customer_service,
request.subject,
context,
);
) ?? {};

const shop_map = await this.get<Shop>(
queries.map(q => q.shop_id),
this.shop_service,
request.subject,
context,
);
) ?? {};

const orga_map = await this.get<Organization>(
[
Expand All @@ -647,7 +669,7 @@ export class FulfillmentProductService
this.organization_service,
request.subject,
context,
);
) ?? {};

const contact_point_map = await this.get<ContactPoint>(
[
Expand All @@ -661,7 +683,7 @@ export class FulfillmentProductService
this.contact_point_service,
request.subject,
context,
);
) ?? {};

const address_map = await this.get<Address>(
Object.values(contact_point_map).map(
Expand All @@ -670,7 +692,7 @@ export class FulfillmentProductService
this.address_service,
request.subject,
context,
);
) ?? {};

const country_map = await this.get<Country>(
[
Expand All @@ -683,49 +705,64 @@ export class FulfillmentProductService
this.country_service,
request.subject,
context,
);
) ?? {};

const product_map = await this.findFulfillmentProducts(
queries,
request.subject,
context,
).then(
response => response.items.reduce(
(a: ResponseMap<FulfillmentProduct>, b) => {
a[b.payload?.id ?? b.status?.id!] = b;
return a;
},
{} as ResponseMap<FulfillmentProduct>
)
);
const promises = queries.flatMap(async query => {
try {
if (!query.shop_id) {
this.throwStatusCode(
'Shop',
query.reference?.instance_id,
this.status_codes.NO_SHOP_ID,
);
}
if (!query.customer_id) {
this.throwStatusCode(
'Customer',
query.reference?.instance_id,
this.status_codes.NO_CUSTOMER_ID,
);
}

const tax_map = await this.get<Tax>(
Object.values(product_map).flatMap(
p => p.payload.tax_ids
),
this.tax_service,
request.subject,
context,
);
const product_map = await this.findFulfillmentProducts(
query,
request.subject,
context,
).then(
response => response.items.reduce(
(a: ResponseMap<FulfillmentProduct>, b) => {
a[b.payload?.id ?? b.status?.id!] = b;
return a;
},
{} as ResponseMap<FulfillmentProduct>
)
);

const offer_lists = Object.values(product_map).map(
(product): Offer[] => product.payload?.variants?.map(
(variant): Offer => (
{
name: `${product.payload?.id}\t${variant.id}`,
price: variant.price.sale ? variant.price.sale_price : variant.price.regular_price,
maxWeight: variant.max_weight,
width: variant.max_size?.width,
height: variant.max_size?.height,
depth: variant.max_size?.length,
type: 'parcel'
}
)
)
);
const tax_map = await this.get<Tax>(
Object.values(product_map).flatMap(
p => p.payload.tax_ids
),
this.tax_service,
request.subject,
context,
);

const promises = queries.map(async query => {
try {
const offer_lists = Object.values(product_map).map(
(product): Offer[] => product.payload?.variants?.map(
(variant): Offer => (
{
name: `${product.payload?.id}\t${variant.id}`,
price: variant.price.sale ? variant.price.sale_price : variant.price.regular_price,
maxWeight: variant.max_weight,
width: variant.max_size?.width,
height: variant.max_size?.height,
depth: variant.max_size?.length,
type: 'parcel'
}
)
)
);

const goods = query.items.map((good): IItem => ({
desc: `${good.product_id}\t${good.variant_id}`,
quantity: good.quantity,
Expand Down Expand Up @@ -776,7 +813,10 @@ export class FulfillmentProductService
contact_point.payload.physical_address_id
)
).then(
address => this.getById(country_map, address.payload.country_id)
address => this.getById(
country_map,
address.payload.country_id
)
);

const customer = await this.getById(
Expand Down Expand Up @@ -922,7 +962,7 @@ export class FulfillmentProductService
const solution: PackingSolutionResponse = {
solutions,
status: {
id: query.reference.instance_id,
id: query.reference?.instance_id,
code: 200,
message: `Best Solution: ${
Math.min(
Expand All @@ -940,7 +980,7 @@ export class FulfillmentProductService
const solution: PackingSolutionResponse = {
solutions: [],
status: this.catchStatusError(
query.reference.instance_id,
query.reference?.instance_id,
e,
),
};
Expand Down

0 comments on commit 4301dde

Please sign in to comment.