@@ -156,6 +156,11 @@ export class FulfillmentProductService
156
156
code : 404 ,
157
157
message : '{entity} {id} has no legal address!' ,
158
158
} ,
159
+ NO_SHIPPING_ADDRESS : {
160
+ id : '' ,
161
+ code : 404 ,
162
+ message : '{entity} {id} has no shipping address!' ,
163
+ }
159
164
} ;
160
165
161
166
protected readonly operation_status_codes : { [ key : string ] : OperationStatus } = {
@@ -178,6 +183,7 @@ export class FulfillmentProductService
178
183
} ;
179
184
180
185
protected readonly legal_address_type_id : string ;
186
+ protected readonly shipping_address_type_id : string ;
181
187
protected readonly customer_service : Client < CustomerServiceDefinition > ;
182
188
protected readonly shop_service : Client < ShopServiceDefinition > ;
183
189
protected readonly organization_service : Client < OrganizationServiceDefinition > ;
@@ -216,6 +222,7 @@ export class FulfillmentProductService
216
222
} ;
217
223
218
224
this . legal_address_type_id = this . cfg . get ( 'preDefinedIds:legalAddressTypeId' ) ;
225
+ this . shipping_address_type_id = this . cfg . get ( 'preDefinedIds:shippingAddressTypeId' ) ;
219
226
220
227
this . customer_service = createClient (
221
228
{
@@ -352,7 +359,7 @@ export class FulfillmentProductService
352
359
subject ?: Subject ,
353
360
context ?: any ,
354
361
) : Promise < ResponseMap < T > > {
355
- ids = [ ...new Set ( ids ) ] ;
362
+ ids = [ ...new Set ( ids . filter ( id => id ) ) ] ;
356
363
const entity = ( { } as new ( ) => T ) . name ;
357
364
358
365
if ( ids . length > 1000 ) {
@@ -473,7 +480,7 @@ export class FulfillmentProductService
473
480
} ] ,
474
481
subject,
475
482
} ) ;
476
- return await this . courier_srv . read ( call , context ) . then (
483
+ const response = await this . courier_srv . read ( call , context ) . then (
477
484
resp => {
478
485
if ( resp . operation_status ?. code !== 200 ) {
479
486
throw resp . operation_status ;
@@ -483,6 +490,8 @@ export class FulfillmentProductService
483
490
}
484
491
}
485
492
) ;
493
+ this . logger . debug ( 'Available couriers' , response ) ;
494
+ return response ;
486
495
}
487
496
488
497
protected async findFulfillmentProducts (
@@ -663,9 +672,13 @@ export class FulfillmentProductService
663
672
) ;
664
673
665
674
const country_map = await this . get < Country > (
666
- Object . values ( address_map ) . map (
667
- item => item . payload ?. country_id
668
- ) ,
675
+ [
676
+ ...Object . values ( address_map ) . map (
677
+ item => item . payload ?. country_id
678
+ ) ,
679
+ ...queries . map ( query => query . sender ?. address ?. country_id ) ,
680
+ ...queries . map ( query => query . recipient ?. address ?. country_id )
681
+ ] ,
669
682
this . country_service ,
670
683
request . subject ,
671
684
context ,
@@ -701,9 +714,9 @@ export class FulfillmentProductService
701
714
name : `${ product . payload ?. id } \t${ variant . id } ` ,
702
715
price : variant . price . sale ? variant . price . sale_price : variant . price . regular_price ,
703
716
maxWeight : variant . max_weight ,
704
- width : variant . max_size . width ,
705
- height : variant . max_size . height ,
706
- depth : variant . max_size . length ,
717
+ width : variant . max_size ? .width ,
718
+ height : variant . max_size ? .height ,
719
+ depth : variant . max_size ? .length ,
707
720
type : 'parcel'
708
721
}
709
722
)
@@ -728,71 +741,81 @@ export class FulfillmentProductService
728
741
shipping : null
729
742
} ) ;
730
743
731
- const shop_country = await this . getById (
732
- shop_map ,
733
- query . shop_id
734
- ) . then (
735
- shop => this . getById (
736
- orga_map ,
737
- shop . payload . organization_id
738
- )
739
- ) . then (
740
- orga => this . getByIds (
741
- contact_point_map ,
742
- orga . payload . contact_point_ids
744
+ const shop_country = query . sender ?. address ?. country_id
745
+ ? await this . getById (
746
+ country_map ,
747
+ query . sender . address . country_id
743
748
)
744
- ) . then (
745
- cpts => cpts . find (
746
- cpt => cpt . payload ?. contact_point_type_ids . includes (
747
- this . legal_address_type_id
749
+ : await this . getById (
750
+ shop_map ,
751
+ query . shop_id
752
+ ) . then (
753
+ shop => this . getById (
754
+ orga_map ,
755
+ shop . payload . organization_id
748
756
)
749
- ) ?? this . throwStatusCode < ContactPointResponse > (
750
- query . reference . instance_type ,
751
- query . reference . instance_id ,
752
- this . status_codes . NO_LEGAL_ADDRESS ,
753
- )
754
- ) . then (
755
- contact_point => this . getById (
756
- address_map ,
757
- contact_point . payload . physical_address_id
758
- )
759
- ) . then (
760
- address => this . getById ( country_map , address . payload . country_id )
761
- ) ;
757
+ ) . then (
758
+ orga => this . getByIds (
759
+ contact_point_map ,
760
+ orga . payload . contact_point_ids
761
+ )
762
+ ) . then (
763
+ cpts => cpts . find (
764
+ cpt => cpt . payload ?. contact_point_type_ids . includes (
765
+ this . legal_address_type_id
766
+ )
767
+ ) ?? this . throwStatusCode < ContactPointResponse > (
768
+ 'Shop' ,
769
+ query . shop_id ,
770
+ this . status_codes . NO_LEGAL_ADDRESS ,
771
+ )
772
+ ) . then (
773
+ contact_point => this . getById (
774
+ address_map ,
775
+ contact_point . payload . physical_address_id
776
+ )
777
+ ) . then (
778
+ address => this . getById ( country_map , address . payload . country_id )
779
+ ) ;
762
780
763
781
const customer = await this . getById (
764
782
customer_map ,
765
783
query . customer_id
766
784
) ;
767
785
768
- const customer_country = await this . getByIds (
769
- contact_point_map ,
770
- [
771
- customer . payload . private ?. contact_point_ids ,
772
- orga_map [ customer . payload . commercial ?. organization_id ] ?. payload . contact_point_ids ,
773
- orga_map [ customer . payload . public_sector ?. organization_id ] ?. payload . contact_point_ids ,
774
- ] . flatMap ( id => id ) . filter ( id => id )
775
- ) . then (
776
- cps => cps . find (
777
- cp => cp . payload ?. contact_point_type_ids . includes (
778
- this . legal_address_type_id
779
- )
780
- ) ?? this . throwStatusCode < ContactPointResponse > (
781
- query . reference . instance_type ,
782
- query . reference . instance_id ,
783
- this . status_codes . NO_LEGAL_ADDRESS ,
784
- )
785
- ) . then (
786
- cp => this . getById (
787
- address_map ,
788
- cp . payload . physical_address_id ,
789
- )
790
- ) . then (
791
- address => this . getById (
786
+ const customer_country = query . recipient ?. address ?. country_id
787
+ ? await this . getById (
792
788
country_map ,
793
- address . payload . country_id
789
+ query . recipient . address . country_id
794
790
)
795
- ) ;
791
+ : await this . getByIds (
792
+ contact_point_map ,
793
+ [
794
+ customer . payload . private ?. contact_point_ids ,
795
+ orga_map [ customer . payload . commercial ?. organization_id ] ?. payload . contact_point_ids ,
796
+ orga_map [ customer . payload . public_sector ?. organization_id ] ?. payload . contact_point_ids ,
797
+ ] . flatMap ( id => id ) . filter ( id => id )
798
+ ) . then (
799
+ cps => cps . find (
800
+ cp => cp . payload ?. contact_point_type_ids . includes (
801
+ this . shipping_address_type_id
802
+ )
803
+ ) ?? this . throwStatusCode < ContactPointResponse > (
804
+ 'Customer' ,
805
+ customer . payload . id ,
806
+ this . status_codes . NO_SHIPPING_ADDRESS ,
807
+ )
808
+ ) . then (
809
+ cp => this . getById (
810
+ address_map ,
811
+ cp . payload . physical_address_id ,
812
+ )
813
+ ) . then (
814
+ address => this . getById (
815
+ country_map ,
816
+ address . payload . country_id
817
+ )
818
+ ) ;
796
819
797
820
const solutions : PackingSolution [ ] = offer_lists . map (
798
821
offers => packer . canFit ( offers , goods )
@@ -887,6 +910,12 @@ export class FulfillmentProductService
887
910
reference : query . reference ,
888
911
} ;
889
912
}
913
+ ) . sort (
914
+ ( a , b ) => Math . min (
915
+ ...a . amounts ?. map ( am => am . net )
916
+ ) - Math . min (
917
+ ...b . amounts ?. map ( am => am . net )
918
+ )
890
919
) ;
891
920
892
921
const solution : PackingSolutionResponse = {
0 commit comments