@@ -161,6 +161,16 @@ export class FulfillmentProductService
161
161
id : '' ,
162
162
code : 404 ,
163
163
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!'
164
174
}
165
175
} ;
166
176
@@ -462,22 +472,34 @@ export class FulfillmentProductService
462
472
}
463
473
464
474
protected async findCouriers (
465
- queries : PackageSolutionTotals [ ] ,
475
+ query : PackageSolutionTotals ,
466
476
subject ?: Subject ,
467
477
context ?: any ,
468
478
) : Promise < FulfillmentCourierListResponse > {
469
479
const call = ReadRequest . fromPartial ( {
470
480
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
481
503
} ] ,
482
504
subject,
483
505
} ) ;
@@ -496,12 +518,12 @@ export class FulfillmentProductService
496
518
}
497
519
498
520
protected async findFulfillmentProducts (
499
- queries : PackageSolutionTotals [ ] ,
521
+ query : PackageSolutionTotals ,
500
522
subject ?: Subject ,
501
523
context ?: any ,
502
524
) : Promise < FulfillmentProductListResponse > {
503
525
const stubs = await this . findCouriers (
504
- queries ,
526
+ query ,
505
527
subject ,
506
528
context ,
507
529
) . then (
@@ -625,14 +647,14 @@ export class FulfillmentProductService
625
647
this . customer_service ,
626
648
request . subject ,
627
649
context ,
628
- ) ;
650
+ ) ?? { } ;
629
651
630
652
const shop_map = await this . get < Shop > (
631
653
queries . map ( q => q . shop_id ) ,
632
654
this . shop_service ,
633
655
request . subject ,
634
656
context ,
635
- ) ;
657
+ ) ?? { } ;
636
658
637
659
const orga_map = await this . get < Organization > (
638
660
[
@@ -647,7 +669,7 @@ export class FulfillmentProductService
647
669
this . organization_service ,
648
670
request . subject ,
649
671
context ,
650
- ) ;
672
+ ) ?? { } ;
651
673
652
674
const contact_point_map = await this . get < ContactPoint > (
653
675
[
@@ -661,7 +683,7 @@ export class FulfillmentProductService
661
683
this . contact_point_service ,
662
684
request . subject ,
663
685
context ,
664
- ) ;
686
+ ) ?? { } ;
665
687
666
688
const address_map = await this . get < Address > (
667
689
Object . values ( contact_point_map ) . map (
@@ -670,7 +692,7 @@ export class FulfillmentProductService
670
692
this . address_service ,
671
693
request . subject ,
672
694
context ,
673
- ) ;
695
+ ) ?? { } ;
674
696
675
697
const country_map = await this . get < Country > (
676
698
[
@@ -683,49 +705,64 @@ export class FulfillmentProductService
683
705
this . country_service ,
684
706
request . subject ,
685
707
context ,
686
- ) ;
708
+ ) ?? { } ;
687
709
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
+ }
701
726
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
+ ) ;
710
740
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
+ ) ;
726
749
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
+
729
766
const goods = query . items . map ( ( good ) : IItem => ( {
730
767
desc : `${ good . product_id } \t${ good . variant_id } ` ,
731
768
quantity : good . quantity ,
@@ -776,7 +813,10 @@ export class FulfillmentProductService
776
813
contact_point . payload . physical_address_id
777
814
)
778
815
) . then (
779
- address => this . getById ( country_map , address . payload . country_id )
816
+ address => this . getById (
817
+ country_map ,
818
+ address . payload . country_id
819
+ )
780
820
) ;
781
821
782
822
const customer = await this . getById (
@@ -922,7 +962,7 @@ export class FulfillmentProductService
922
962
const solution : PackingSolutionResponse = {
923
963
solutions,
924
964
status : {
925
- id : query . reference . instance_id ,
965
+ id : query . reference ? .instance_id ,
926
966
code : 200 ,
927
967
message : `Best Solution: ${
928
968
Math . min (
@@ -940,7 +980,7 @@ export class FulfillmentProductService
940
980
const solution : PackingSolutionResponse = {
941
981
solutions : [ ] ,
942
982
status : this . catchStatusError (
943
- query . reference . instance_id ,
983
+ query . reference ? .instance_id ,
944
984
e ,
945
985
) ,
946
986
} ;
0 commit comments