Skip to content

Commit

Permalink
fix(credentials): fulfillment courier now supports credentials as sep…
Browse files Browse the repository at this point in the history
…erate resource
  • Loading branch information
Gerald Baulig committed May 10, 2024
1 parent 729da43 commit ead1d31
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 131 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ WORKDIR $APP_HOME

COPY --chown=node:node ./cfg $APP_HOME/cfg
COPY --chown=node:node ./queries $APP_HOME/queries
COPY --chown=node:node ./wsdl $APP_HOME/wsdl
COPY --chown=node:node --from=build $APP_HOME/lib $APP_HOME/lib

EXPOSE 50051
Expand Down
3 changes: 3 additions & 0 deletions cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
"tax": {
"address": "localhost:50063"
},
"credential": {
"address": "localhost:50063"
},
"product": {
"address": "localhost:50068"
}
Expand Down
13 changes: 13 additions & 0 deletions cfg/config_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@
]
}
}
},
"credential": {
"address": "localhost:50173",
"mock": {
"protoPath": "io/restorecommerce/credential.proto",
"packageName": "io.restorecommerce.credential",
"serviceName": "CredentialService",
"protoLoadOptions": {
"includeDirs": [
"node_modules/@restorecommerce/protos/"
]
}
}
}
}
}
30 changes: 15 additions & 15 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
},
"type": "module",
"dependencies": {
"@restorecommerce/acs-client": "^1.6.6",
"@restorecommerce/acs-client": "^1.6.7",
"@restorecommerce/cart": "^1.0.5",
"@restorecommerce/chassis-srv": "^1.6.0",
"@restorecommerce/cluster-service": "^1.0.3",
"@restorecommerce/grpc-client": "^2.2.1",
"@restorecommerce/kafka-client": "^1.2.1",
"@restorecommerce/kafka-client": "^1.2.6",
"@restorecommerce/logger": "^1.2.10",
"@restorecommerce/rc-grpc-clients": "^5.1.27",
"@restorecommerce/rc-grpc-clients": "^5.1.28",
"@restorecommerce/resource-base-interface": "^1.6.0",
"@restorecommerce/service-config": "^1.0.12",
"@types/soap": "^0.21.0",
Expand Down
86 changes: 66 additions & 20 deletions src/services/fulfillment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ import {
Customer,
CustomerServiceDefinition
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/customer.js';
import {
Credential,
CredentialServiceDefinition
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/credential.js';
import {
Shop,
ShopServiceDefinition
Expand Down Expand Up @@ -173,6 +177,7 @@ export class FulfillmentService
protected readonly address_service: Client<AddressServiceDefinition>;
protected readonly country_service: Client<CountryServiceDefinition>;
protected readonly tax_service: Client<TaxServiceDefinition>;
protected readonly credential_service: Client<CredentialServiceDefinition>;

constructor(
readonly fulfillmentCourierSrv: FulfillmentCourierService,
Expand Down Expand Up @@ -272,6 +277,15 @@ export class FulfillmentService
TaxServiceDefinition,
createChannel(cfg.get('client:tax:address'))
);

this.credential_service = createClient(
{
...cfg.get('client:credential'),
logger
} as GrpcClientConfig,
CredentialServiceDefinition,
createChannel(cfg.get('client:credential:address'))
);
}

protected handleStatusError<T>(id: string, e: any, payload?: any): T {
Expand Down Expand Up @@ -405,11 +419,10 @@ export class FulfillmentService
context?: any,
): Promise<ResponseMap<T>> {
ids = [...new Set(ids)];
const entity = ({} as new() => T).name;

if (ids.length > 1000) {
throwOperationStatusCode(
entity,
service.constructor?.name,
this.operation_status_codes.LIMIT_EXHAUSTED,
);
}
Expand Down Expand Up @@ -450,22 +463,22 @@ export class FulfillmentService
);
}

async getById<T>(map: { [id: string]: T }, id: string): Promise<T> {
if (id in map) {
async getById<T>(map: { [id: string]: T }, id: string, name: string): Promise<T> {
if (map && id in map) {
return map[id];
}
else {
throwStatusCode<T>(
({} as new() => T).name,
name,
id,
this.status_codes.NOT_FOUND
);
}
}

async getByIds<T>(map: { [id: string]: T }, ids: string[]): Promise<T[]> {
async getByIds<T>(map: { [id: string]: T }, ids: string[], name: string): Promise<T[]> {
return Promise.all(ids.map(
id => this.getById(map, id)
id => this.getById(map, id, name)
));
}

Expand Down Expand Up @@ -579,6 +592,15 @@ export class FulfillmentService
context,
);

const credential_map = await this.get<Credential>(
Object.values(courier_map).map(
c => c.payload?.credential_id
),
this.credential_service,
subject,
context,
)

const tax_map = await this.get<Tax>(
Object.values(product_map).flatMap(
p => p.payload?.tax_ids
Expand All @@ -593,26 +615,30 @@ export class FulfillmentService
const sender_country = await this.getById(
country_map,
item.packaging.sender?.address?.country_id,
'Country'
);

const recipient_country = await this.getById(
country_map,
item.packaging.recipient?.address?.country_id,
'Country'
);

const products = await this.getByIds(
product_map,
item.packaging.parcels.map(
parcel => parcel?.product_id
),
'Product'
);

const couriers = await this.getByIds(
courier_map,
products.map(
product => product.payload?.courier_id
)
)
),
'Courier'
);

couriers.every(
courier => courier.payload?.shop_ids?.includes(
Expand All @@ -624,6 +650,16 @@ export class FulfillmentService
this.status_codes.SHOP_ID_NOT_IDENTICAL,
);

const credentials = await this.getByIds(
credential_map,
couriers.map(
c => c.payload?.credential_id
).filter(
id => id
),
'Credential'
);

const status: Status[] = [
sender_country?.status,
recipient_country?.status,
Expand All @@ -633,16 +669,19 @@ export class FulfillmentService

const shop_country = await this.getById(
shop_map,
item.shop_id
item.shop_id,
'Shop'
).then(
shop => this.getById(
orga_map,
shop.payload?.organization_id
shop.payload!.organization_id,
'Organization'
)
).then(
orga => this.getByIds(
contact_point_map,
orga.payload?.contact_point_ids
orga.payload!.contact_point_ids,
'ContactPoint'
)
).then(
cpts => cpts.find(
Expand All @@ -657,18 +696,21 @@ export class FulfillmentService
).then(
contact_point => this.getById(
address_map,
contact_point.payload?.physical_address_id,
contact_point.payload!.physical_address_id,
'ContactPoint'
)
).then(
address => this.getById(
country_map,
address.payload?.country_id
address.payload!.country_id,
'Country'
)
);

const customer = await this.getById(
customer_map,
item.customer_id
item.customer_id,
'Customer'
);

const customer_country = await this.getByIds(
Expand All @@ -677,7 +719,8 @@ export class FulfillmentService
customer.payload.private?.contact_point_ids,
orga_map[customer.payload.commercial?.organization_id]?.payload.contact_point_ids,
orga_map[customer.payload.public_sector?.organization_id]?.payload.contact_point_ids,
].flatMap(id => id).filter(id => id)
].flatMap(id => id).filter(id => id),
'Country'
).then(
cps => cps.find(
cp => cp.payload?.contact_point_type_ids.indexOf(
Expand All @@ -692,11 +735,13 @@ export class FulfillmentService
cp => this.getById(
address_map,
cp.payload.physical_address_id,
'Address'
)
).then(
address => this.getById(
country_map,
address.payload.country_id
address.payload.country_id,
'Country'
)
);

Expand Down Expand Up @@ -740,6 +785,7 @@ export class FulfillmentService
payload: item,
products,
couriers,
credentials,
sender_country,
recipient_country,
options: null,
Expand Down Expand Up @@ -921,7 +967,7 @@ export class FulfillmentService
}, context);

upsert_results.items.forEach(item => {
if (item.payload.fulfillment_state in this.emitters) {
if (this.emitters && item.payload.fulfillment_state in this.emitters) {
switch (item.payload.fulfillment_state) {
case FulfillmentState.INVALID:
case FulfillmentState.FAILED:
Expand Down Expand Up @@ -1069,7 +1115,7 @@ export class FulfillmentService
updates => updates.items.forEach(
item => {
response_map[item.payload?.id ?? item.status?.id] = item as FulfillmentResponse;
if (item.payload.fulfillment_state in this.emitters) {
if (this.emitters && item.payload.fulfillment_state in this.emitters) {
switch (item.payload.fulfillment_state) {
case FulfillmentState.INVALID:
case FulfillmentState.FAILED:
Expand Down Expand Up @@ -1208,7 +1254,7 @@ export class FulfillmentService
}, context);

update_results.items.forEach(item => {
if (item.payload.fulfillment_state in this.emitters) {
if (this.emitters && item.payload.fulfillment_state in this.emitters) {
switch (item.payload.fulfillment_state) {
case FulfillmentState.INVALID:
case FulfillmentState.FAILED:
Expand Down
Loading

0 comments on commit ead1d31

Please sign in to comment.