Skip to content

Commit 4301dde

Browse files
author
Gerald Baulig
committed
fix(find): improve error messages on product find
1 parent 4257f1a commit 4301dde

File tree

4 files changed

+117
-66
lines changed

4 files changed

+117
-66
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"type": "module",
1212
"dependencies": {
13-
"@restorecommerce/acs-client": "^3.0.0",
13+
"@restorecommerce/acs-client": "^3.0.1",
1414
"@restorecommerce/cart": "^1.0.9",
1515
"@restorecommerce/chassis-srv": "^1.6.2",
1616
"@restorecommerce/grpc-client": "^2.2.4",

src/services/fulfillment.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
DefaultACSClientContextFactory,
1111
Operation,
1212
DefaultResourceFactory,
13-
injects_meta_data
13+
injects_meta_data,
14+
resolves_subject
1415
} from '@restorecommerce/acs-client';
1516
import { type Logger } from '@restorecommerce/logger';
1617
import { type ServiceConfig } from '@restorecommerce/service-config';
@@ -827,6 +828,7 @@ export class FulfillmentService
827828
return super.read(request, context);
828829
}
829830

831+
@resolves_subject()
830832
@injects_meta_data()
831833
@access_controlled_function({
832834
action: AuthZAction.CREATE,
@@ -850,6 +852,7 @@ export class FulfillmentService
850852
return super.create(request, context);
851853
}
852854

855+
@resolves_subject()
853856
@access_controlled_function({
854857
action: AuthZAction.MODIFY,
855858
operation: Operation.isAllowed,
@@ -865,6 +868,7 @@ export class FulfillmentService
865868
return super.update(request, context);
866869
}
867870

871+
@resolves_subject()
868872
@injects_meta_data()
869873
@access_controlled_function({
870874
action: AuthZAction.MODIFY,
@@ -881,6 +885,7 @@ export class FulfillmentService
881885
return super.upsert(request, context);
882886
}
883887

888+
@resolves_subject()
884889
@access_controlled_function({
885890
action: AuthZAction.EXECUTE,
886891
operation: Operation.isAllowed,
@@ -917,6 +922,7 @@ export class FulfillmentService
917922
}
918923
}
919924

925+
@resolves_subject()
920926
@injects_meta_data()
921927
@access_controlled_function({
922928
action: AuthZAction.EXECUTE,
@@ -993,6 +999,7 @@ export class FulfillmentService
993999
}
9941000
}
9951001

1002+
@resolves_subject()
9961003
@access_controlled_function({
9971004
action: AuthZAction.EXECUTE,
9981005
operation: Operation.isAllowed,
@@ -1147,6 +1154,7 @@ export class FulfillmentService
11471154
}
11481155
}
11491156

1157+
@resolves_subject()
11501158
@access_controlled_function({
11511159
action: AuthZAction.EXECUTE,
11521160
operation: Operation.isAllowed,
@@ -1159,6 +1167,7 @@ export class FulfillmentService
11591167
return null;
11601168
}
11611169

1170+
@resolves_subject()
11621171
@access_controlled_function({
11631172
action: AuthZAction.EXECUTE,
11641173
operation: Operation.isAllowed,
@@ -1279,6 +1288,7 @@ export class FulfillmentService
12791288
}
12801289
}
12811290

1291+
@resolves_subject()
12821292
@access_controlled_function({
12831293
action: AuthZAction.DELETE,
12841294
operation: Operation.isAllowed,
@@ -1294,6 +1304,7 @@ export class FulfillmentService
12941304
return super.delete(request, context);
12951305
}
12961306

1307+
@resolves_subject()
12971308
@access_controlled_function({
12981309
action: AuthZAction.CREATE,
12991310
operation: Operation.isAllowed,

src/services/fulfillment_product.ts

+100-60
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ export class FulfillmentProductService
161161
id: '',
162162
code: 404,
163163
message: '{entity} {id} has no shipping address!',
164+
},
165+
NO_SHOP_ID: {
166+
id: '',
167+
code: 400,
168+
message: 'Shop ID not provided!'
169+
},
170+
NO_CUSTOMER_ID: {
171+
id: '',
172+
code: 400,
173+
message: 'Customer ID not provided!'
164174
}
165175
};
166176

@@ -462,22 +472,34 @@ export class FulfillmentProductService
462472
}
463473

464474
protected async findCouriers(
465-
queries: PackageSolutionTotals[],
475+
query: PackageSolutionTotals,
466476
subject?: Subject,
467477
context?: any,
468478
): Promise<FulfillmentCourierListResponse> {
469479
const call = ReadRequest.fromPartial({
470480
filters: [{
471-
filters: queries.flatMap(
472-
item => item.preferences?.couriers?.map(
473-
att => ({
474-
field: att.id,
475-
operation: Filter_Operation.eq,
476-
value: att.value,
477-
})
478-
)
479-
).filter(item => !!item),
480-
operator: FilterOp_Operator.or
481+
filters: [{
482+
filters: [{
483+
filters: query.preferences?.couriers?.map(
484+
att => ({
485+
field: att.id,
486+
operation: Filter_Operation.eq,
487+
value: att.value,
488+
})
489+
).filter(item => !!item),
490+
operator: FilterOp_Operator.or
491+
}]
492+
},{
493+
filters: [{
494+
filters: [{
495+
field: 'shop_ids',
496+
operation: Filter_Operation.in,
497+
value: query.shop_id
498+
}],
499+
operator: FilterOp_Operator.and
500+
}],
501+
}],
502+
operator: FilterOp_Operator.and
481503
}],
482504
subject,
483505
});
@@ -496,12 +518,12 @@ export class FulfillmentProductService
496518
}
497519

498520
protected async findFulfillmentProducts(
499-
queries: PackageSolutionTotals[],
521+
query: PackageSolutionTotals,
500522
subject?: Subject,
501523
context?: any,
502524
): Promise<FulfillmentProductListResponse> {
503525
const stubs = await this.findCouriers(
504-
queries,
526+
query,
505527
subject,
506528
context,
507529
).then(
@@ -625,14 +647,14 @@ export class FulfillmentProductService
625647
this.customer_service,
626648
request.subject,
627649
context,
628-
);
650+
) ?? {};
629651

630652
const shop_map = await this.get<Shop>(
631653
queries.map(q => q.shop_id),
632654
this.shop_service,
633655
request.subject,
634656
context,
635-
);
657+
) ?? {};
636658

637659
const orga_map = await this.get<Organization>(
638660
[
@@ -647,7 +669,7 @@ export class FulfillmentProductService
647669
this.organization_service,
648670
request.subject,
649671
context,
650-
);
672+
) ?? {};
651673

652674
const contact_point_map = await this.get<ContactPoint>(
653675
[
@@ -661,7 +683,7 @@ export class FulfillmentProductService
661683
this.contact_point_service,
662684
request.subject,
663685
context,
664-
);
686+
) ?? {};
665687

666688
const address_map = await this.get<Address>(
667689
Object.values(contact_point_map).map(
@@ -670,7 +692,7 @@ export class FulfillmentProductService
670692
this.address_service,
671693
request.subject,
672694
context,
673-
);
695+
) ?? {};
674696

675697
const country_map = await this.get<Country>(
676698
[
@@ -683,49 +705,64 @@ export class FulfillmentProductService
683705
this.country_service,
684706
request.subject,
685707
context,
686-
);
708+
) ?? {};
687709

688-
const product_map = await this.findFulfillmentProducts(
689-
queries,
690-
request.subject,
691-
context,
692-
).then(
693-
response => response.items.reduce(
694-
(a: ResponseMap<FulfillmentProduct>, b) => {
695-
a[b.payload?.id ?? b.status?.id!] = b;
696-
return a;
697-
},
698-
{} as ResponseMap<FulfillmentProduct>
699-
)
700-
);
710+
const promises = queries.flatMap(async query => {
711+
try {
712+
if (!query.shop_id) {
713+
this.throwStatusCode(
714+
'Shop',
715+
query.reference?.instance_id,
716+
this.status_codes.NO_SHOP_ID,
717+
);
718+
}
719+
if (!query.customer_id) {
720+
this.throwStatusCode(
721+
'Customer',
722+
query.reference?.instance_id,
723+
this.status_codes.NO_CUSTOMER_ID,
724+
);
725+
}
701726

702-
const tax_map = await this.get<Tax>(
703-
Object.values(product_map).flatMap(
704-
p => p.payload.tax_ids
705-
),
706-
this.tax_service,
707-
request.subject,
708-
context,
709-
);
727+
const product_map = await this.findFulfillmentProducts(
728+
query,
729+
request.subject,
730+
context,
731+
).then(
732+
response => response.items.reduce(
733+
(a: ResponseMap<FulfillmentProduct>, b) => {
734+
a[b.payload?.id ?? b.status?.id!] = b;
735+
return a;
736+
},
737+
{} as ResponseMap<FulfillmentProduct>
738+
)
739+
);
710740

711-
const offer_lists = Object.values(product_map).map(
712-
(product): Offer[] => product.payload?.variants?.map(
713-
(variant): Offer => (
714-
{
715-
name: `${product.payload?.id}\t${variant.id}`,
716-
price: variant.price.sale ? variant.price.sale_price : variant.price.regular_price,
717-
maxWeight: variant.max_weight,
718-
width: variant.max_size?.width,
719-
height: variant.max_size?.height,
720-
depth: variant.max_size?.length,
721-
type: 'parcel'
722-
}
723-
)
724-
)
725-
);
741+
const tax_map = await this.get<Tax>(
742+
Object.values(product_map).flatMap(
743+
p => p.payload.tax_ids
744+
),
745+
this.tax_service,
746+
request.subject,
747+
context,
748+
);
726749

727-
const promises = queries.map(async query => {
728-
try {
750+
const offer_lists = Object.values(product_map).map(
751+
(product): Offer[] => product.payload?.variants?.map(
752+
(variant): Offer => (
753+
{
754+
name: `${product.payload?.id}\t${variant.id}`,
755+
price: variant.price.sale ? variant.price.sale_price : variant.price.regular_price,
756+
maxWeight: variant.max_weight,
757+
width: variant.max_size?.width,
758+
height: variant.max_size?.height,
759+
depth: variant.max_size?.length,
760+
type: 'parcel'
761+
}
762+
)
763+
)
764+
);
765+
729766
const goods = query.items.map((good): IItem => ({
730767
desc: `${good.product_id}\t${good.variant_id}`,
731768
quantity: good.quantity,
@@ -776,7 +813,10 @@ export class FulfillmentProductService
776813
contact_point.payload.physical_address_id
777814
)
778815
).then(
779-
address => this.getById(country_map, address.payload.country_id)
816+
address => this.getById(
817+
country_map,
818+
address.payload.country_id
819+
)
780820
);
781821

782822
const customer = await this.getById(
@@ -922,7 +962,7 @@ export class FulfillmentProductService
922962
const solution: PackingSolutionResponse = {
923963
solutions,
924964
status: {
925-
id: query.reference.instance_id,
965+
id: query.reference?.instance_id,
926966
code: 200,
927967
message: `Best Solution: ${
928968
Math.min(
@@ -940,7 +980,7 @@ export class FulfillmentProductService
940980
const solution: PackingSolutionResponse = {
941981
solutions: [],
942982
status: this.catchStatusError(
943-
query.reference.instance_id,
983+
query.reference?.instance_id,
944984
e,
945985
),
946986
};

0 commit comments

Comments
 (0)