@@ -61,6 +61,10 @@ import {
61
61
Customer ,
62
62
CustomerServiceDefinition
63
63
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/customer.js' ;
64
+ import {
65
+ Credential ,
66
+ CredentialServiceDefinition
67
+ } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/credential.js' ;
64
68
import {
65
69
Shop ,
66
70
ShopServiceDefinition
@@ -173,6 +177,7 @@ export class FulfillmentService
173
177
protected readonly address_service : Client < AddressServiceDefinition > ;
174
178
protected readonly country_service : Client < CountryServiceDefinition > ;
175
179
protected readonly tax_service : Client < TaxServiceDefinition > ;
180
+ protected readonly credential_service : Client < CredentialServiceDefinition > ;
176
181
177
182
constructor (
178
183
readonly fulfillmentCourierSrv : FulfillmentCourierService ,
@@ -272,6 +277,15 @@ export class FulfillmentService
272
277
TaxServiceDefinition ,
273
278
createChannel ( cfg . get ( 'client:tax:address' ) )
274
279
) ;
280
+
281
+ this . credential_service = createClient (
282
+ {
283
+ ...cfg . get ( 'client:credential' ) ,
284
+ logger
285
+ } as GrpcClientConfig ,
286
+ CredentialServiceDefinition ,
287
+ createChannel ( cfg . get ( 'client:credential:address' ) )
288
+ ) ;
275
289
}
276
290
277
291
protected handleStatusError < T > ( id : string , e : any , payload ?: any ) : T {
@@ -405,11 +419,10 @@ export class FulfillmentService
405
419
context ?: any ,
406
420
) : Promise < ResponseMap < T > > {
407
421
ids = [ ...new Set ( ids ) ] ;
408
- const entity = ( { } as new ( ) => T ) . name ;
409
422
410
423
if ( ids . length > 1000 ) {
411
424
throwOperationStatusCode (
412
- entity ,
425
+ service . constructor ?. name ,
413
426
this . operation_status_codes . LIMIT_EXHAUSTED ,
414
427
) ;
415
428
}
@@ -450,22 +463,22 @@ export class FulfillmentService
450
463
) ;
451
464
}
452
465
453
- async getById < T > ( map : { [ id : string ] : T } , id : string ) : Promise < T > {
454
- if ( id in map ) {
466
+ async getById < T > ( map : { [ id : string ] : T } , id : string , name : string ) : Promise < T > {
467
+ if ( map && id in map ) {
455
468
return map [ id ] ;
456
469
}
457
470
else {
458
471
throwStatusCode < T > (
459
- ( { } as new ( ) => T ) . name ,
472
+ name ,
460
473
id ,
461
474
this . status_codes . NOT_FOUND
462
475
) ;
463
476
}
464
477
}
465
478
466
- async getByIds < T > ( map : { [ id : string ] : T } , ids : string [ ] ) : Promise < T [ ] > {
479
+ async getByIds < T > ( map : { [ id : string ] : T } , ids : string [ ] , name : string ) : Promise < T [ ] > {
467
480
return Promise . all ( ids . map (
468
- id => this . getById ( map , id )
481
+ id => this . getById ( map , id , name )
469
482
) ) ;
470
483
}
471
484
@@ -579,6 +592,15 @@ export class FulfillmentService
579
592
context ,
580
593
) ;
581
594
595
+ const credential_map = await this . get < Credential > (
596
+ Object . values ( courier_map ) . map (
597
+ c => c . payload ?. credential_id
598
+ ) ,
599
+ this . credential_service ,
600
+ subject ,
601
+ context ,
602
+ )
603
+
582
604
const tax_map = await this . get < Tax > (
583
605
Object . values ( product_map ) . flatMap (
584
606
p => p . payload ?. tax_ids
@@ -593,26 +615,30 @@ export class FulfillmentService
593
615
const sender_country = await this . getById (
594
616
country_map ,
595
617
item . packaging . sender ?. address ?. country_id ,
618
+ 'Country'
596
619
) ;
597
620
598
621
const recipient_country = await this . getById (
599
622
country_map ,
600
623
item . packaging . recipient ?. address ?. country_id ,
624
+ 'Country'
601
625
) ;
602
626
603
627
const products = await this . getByIds (
604
628
product_map ,
605
629
item . packaging . parcels . map (
606
630
parcel => parcel ?. product_id
607
631
) ,
632
+ 'Product'
608
633
) ;
609
634
610
635
const couriers = await this . getByIds (
611
636
courier_map ,
612
637
products . map (
613
638
product => product . payload ?. courier_id
614
- )
615
- )
639
+ ) ,
640
+ 'Courier'
641
+ ) ;
616
642
617
643
couriers . every (
618
644
courier => courier . payload ?. shop_ids ?. includes (
@@ -624,6 +650,16 @@ export class FulfillmentService
624
650
this . status_codes . SHOP_ID_NOT_IDENTICAL ,
625
651
) ;
626
652
653
+ const credentials = await this . getByIds (
654
+ credential_map ,
655
+ couriers . map (
656
+ c => c . payload ?. credential_id
657
+ ) . filter (
658
+ id => id
659
+ ) ,
660
+ 'Credential'
661
+ ) ;
662
+
627
663
const status : Status [ ] = [
628
664
sender_country ?. status ,
629
665
recipient_country ?. status ,
@@ -633,16 +669,19 @@ export class FulfillmentService
633
669
634
670
const shop_country = await this . getById (
635
671
shop_map ,
636
- item . shop_id
672
+ item . shop_id ,
673
+ 'Shop'
637
674
) . then (
638
675
shop => this . getById (
639
676
orga_map ,
640
- shop . payload ?. organization_id
677
+ shop . payload ! . organization_id ,
678
+ 'Organization'
641
679
)
642
680
) . then (
643
681
orga => this . getByIds (
644
682
contact_point_map ,
645
- orga . payload ?. contact_point_ids
683
+ orga . payload ! . contact_point_ids ,
684
+ 'ContactPoint'
646
685
)
647
686
) . then (
648
687
cpts => cpts . find (
@@ -657,18 +696,21 @@ export class FulfillmentService
657
696
) . then (
658
697
contact_point => this . getById (
659
698
address_map ,
660
- contact_point . payload ?. physical_address_id ,
699
+ contact_point . payload ! . physical_address_id ,
700
+ 'ContactPoint'
661
701
)
662
702
) . then (
663
703
address => this . getById (
664
704
country_map ,
665
- address . payload ?. country_id
705
+ address . payload ! . country_id ,
706
+ 'Country'
666
707
)
667
708
) ;
668
709
669
710
const customer = await this . getById (
670
711
customer_map ,
671
- item . customer_id
712
+ item . customer_id ,
713
+ 'Customer'
672
714
) ;
673
715
674
716
const customer_country = await this . getByIds (
@@ -677,7 +719,8 @@ export class FulfillmentService
677
719
customer . payload . private ?. contact_point_ids ,
678
720
orga_map [ customer . payload . commercial ?. organization_id ] ?. payload . contact_point_ids ,
679
721
orga_map [ customer . payload . public_sector ?. organization_id ] ?. payload . contact_point_ids ,
680
- ] . flatMap ( id => id ) . filter ( id => id )
722
+ ] . flatMap ( id => id ) . filter ( id => id ) ,
723
+ 'Country'
681
724
) . then (
682
725
cps => cps . find (
683
726
cp => cp . payload ?. contact_point_type_ids . indexOf (
@@ -692,11 +735,13 @@ export class FulfillmentService
692
735
cp => this . getById (
693
736
address_map ,
694
737
cp . payload . physical_address_id ,
738
+ 'Address'
695
739
)
696
740
) . then (
697
741
address => this . getById (
698
742
country_map ,
699
- address . payload . country_id
743
+ address . payload . country_id ,
744
+ 'Country'
700
745
)
701
746
) ;
702
747
@@ -740,6 +785,7 @@ export class FulfillmentService
740
785
payload : item ,
741
786
products,
742
787
couriers,
788
+ credentials,
743
789
sender_country,
744
790
recipient_country,
745
791
options : null ,
@@ -921,7 +967,7 @@ export class FulfillmentService
921
967
} , context ) ;
922
968
923
969
upsert_results . items . forEach ( item => {
924
- if ( item . payload . fulfillment_state in this . emitters ) {
970
+ if ( this . emitters && item . payload . fulfillment_state in this . emitters ) {
925
971
switch ( item . payload . fulfillment_state ) {
926
972
case FulfillmentState . INVALID :
927
973
case FulfillmentState . FAILED :
@@ -1069,7 +1115,7 @@ export class FulfillmentService
1069
1115
updates => updates . items . forEach (
1070
1116
item => {
1071
1117
response_map [ item . payload ?. id ?? item . status ?. id ] = item as FulfillmentResponse ;
1072
- if ( item . payload . fulfillment_state in this . emitters ) {
1118
+ if ( this . emitters && item . payload . fulfillment_state in this . emitters ) {
1073
1119
switch ( item . payload . fulfillment_state ) {
1074
1120
case FulfillmentState . INVALID :
1075
1121
case FulfillmentState . FAILED :
@@ -1208,7 +1254,7 @@ export class FulfillmentService
1208
1254
} , context ) ;
1209
1255
1210
1256
update_results . items . forEach ( item => {
1211
- if ( item . payload . fulfillment_state in this . emitters ) {
1257
+ if ( this . emitters && item . payload . fulfillment_state in this . emitters ) {
1212
1258
switch ( item . payload . fulfillment_state ) {
1213
1259
case FulfillmentState . INVALID :
1214
1260
case FulfillmentState . FAILED :
0 commit comments