Skip to content

Commit 8d3d2e0

Browse files
author
Gerald Baulig
committed
fix(fulfillment_product): more tolerant product.find, imporve error response, user sender and recip
1 parent df5216b commit 8d3d2e0

File tree

9 files changed

+119
-77
lines changed

9 files changed

+119
-77
lines changed

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### Build
2-
FROM node:22.2.0-alpine3.20 as build
2+
FROM node:22.2.0-alpine3.20 AS build
33
ENV NO_UPDATE_NOTIFIER=true
44

55
USER node
@@ -13,7 +13,7 @@ RUN npm run build
1313

1414

1515
### Deployment
16-
FROM node:22.2.0-alpine3.20 as deployment
16+
FROM node:22.2.0-alpine3.20 AS deployment
1717

1818
ENV NO_UPDATE_NOTIFIER=true
1919

cfg/config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@
341341
}
342342
},
343343
"preDefinedIds": {
344-
"legalAddressTypeId": "legal_address"
344+
"legalAddressTypeId": "legal",
345+
"shippingAddressTypeId": "shipping"
345346
},
346347
"stubs": {
347348
"DHLSoap": {

src/services/fulfillment_product.ts

+92-63
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ export class FulfillmentProductService
156156
code: 404,
157157
message: '{entity} {id} has no legal address!',
158158
},
159+
NO_SHIPPING_ADDRESS: {
160+
id: '',
161+
code: 404,
162+
message: '{entity} {id} has no shipping address!',
163+
}
159164
};
160165

161166
protected readonly operation_status_codes: { [key: string]: OperationStatus } = {
@@ -178,6 +183,7 @@ export class FulfillmentProductService
178183
};
179184

180185
protected readonly legal_address_type_id: string;
186+
protected readonly shipping_address_type_id: string;
181187
protected readonly customer_service: Client<CustomerServiceDefinition>;
182188
protected readonly shop_service: Client<ShopServiceDefinition>;
183189
protected readonly organization_service: Client<OrganizationServiceDefinition>;
@@ -216,6 +222,7 @@ export class FulfillmentProductService
216222
};
217223

218224
this.legal_address_type_id = this.cfg.get('preDefinedIds:legalAddressTypeId');
225+
this.shipping_address_type_id = this.cfg.get('preDefinedIds:shippingAddressTypeId');
219226

220227
this.customer_service = createClient(
221228
{
@@ -352,7 +359,7 @@ export class FulfillmentProductService
352359
subject?: Subject,
353360
context?: any,
354361
): Promise<ResponseMap<T>> {
355-
ids = [...new Set(ids)];
362+
ids = [...new Set(ids.filter(id => id))];
356363
const entity = ({} as new() => T).name;
357364

358365
if (ids.length > 1000) {
@@ -473,7 +480,7 @@ export class FulfillmentProductService
473480
}],
474481
subject,
475482
});
476-
return await this.courier_srv.read(call, context).then(
483+
const response = await this.courier_srv.read(call, context).then(
477484
resp => {
478485
if (resp.operation_status?.code !== 200) {
479486
throw resp.operation_status;
@@ -483,6 +490,8 @@ export class FulfillmentProductService
483490
}
484491
}
485492
);
493+
this.logger.debug('Available couriers', response);
494+
return response;
486495
}
487496

488497
protected async findFulfillmentProducts(
@@ -663,9 +672,13 @@ export class FulfillmentProductService
663672
);
664673

665674
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+
],
669682
this.country_service,
670683
request.subject,
671684
context,
@@ -701,9 +714,9 @@ export class FulfillmentProductService
701714
name: `${product.payload?.id}\t${variant.id}`,
702715
price: variant.price.sale ? variant.price.sale_price : variant.price.regular_price,
703716
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,
707720
type: 'parcel'
708721
}
709722
)
@@ -728,71 +741,81 @@ export class FulfillmentProductService
728741
shipping: null
729742
});
730743

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
743748
)
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
748756
)
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+
);
762780

763781
const customer = await this.getById(
764782
customer_map,
765783
query.customer_id
766784
);
767785

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(
792788
country_map,
793-
address.payload.country_id
789+
query.recipient.address.country_id
794790
)
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+
);
796819

797820
const solutions: PackingSolution[] = offer_lists.map(
798821
offers => packer.canFit(offers, goods)
@@ -887,6 +910,12 @@ export class FulfillmentProductService
887910
reference: query.reference,
888911
};
889912
}
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+
)
890919
);
891920

892921
const solution: PackingSolutionResponse = {

src/stub.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,17 @@ export abstract class Stub
187187
}
188188

189189
public static register<T extends Stub>(
190-
typeName: string,
190+
type_name: string,
191191
type: (new (courier: Courier, kwargs?: { [key: string]: any }) => T)
192192
) {
193-
Stub.STUB_TYPES[typeName] = type;
193+
Stub.STUB_TYPES[type_name] = type;
194+
Stub.logger?.info(
195+
'Courier Stub registered:',
196+
{
197+
name: type_name,
198+
'type': type,
199+
}
200+
);
194201
}
195202

196203
public static getInstance(courier: Courier, kwargs?: { [key: string]: any }): Stub

src/stubs/dhl_soap.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ const DHLShipmentCancelResponse2AggregatedFulfillment = (
300300
});
301301
};
302302

303-
class DHLSoap extends Stub {
303+
export class DHLSoap extends Stub {
304304
protected static _clients: ClientMap = {};
305305
protected readonly stub_defaults: any;
306306
public readonly version: number[];
@@ -761,4 +761,4 @@ class DHLSoap extends Stub {
761761
}
762762
};
763763

764-
Stub.register(DHLSoap.name, DHLSoap);
764+
Stub.register('DHLSoap', DHLSoap);

src/stubs/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Register Stubs here!
2-
import './dhl_soap.js';
3-
import './dummy.js';
2+
export { DHLSoap } from './dhl_soap.js';
3+
//export { DHLRest } from './dhl_rest.js';

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const filterTax = (
9494
(
9595
!target.economic_areas ||
9696
origin.economic_areas?.some(
97-
e => e in target.economic_areas
97+
e => target.economic_areas.includes(e)
9898
)
9999
)
100100
);

src/worker.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ import { FulfillmentService } from './services/fulfillment.js';
3838
import { FulfillmentCourierService } from './services/fulfillment_courier.js';
3939
import { FulfillmentProductService } from './services/fulfillment_product.js';
4040
import { FulfillmentCommandInterface } from './services/fulfillment_command_interface.js';
41-
import './stubs/index.js';
41+
import { Stub } from './stub.js';
42+
import { DHLSoap } from './stubs/index.js';
4243

4344
registerProtoMeta(
4445
FulfillmentMeta,
@@ -165,11 +166,15 @@ export class Worker {
165166
async start(cfg?: any, logger?: any): Promise<any> {
166167
// Load config
167168
this._cfg = cfg = cfg ?? createServiceConfig(process.cwd());
168-
const logger_cfg = cfg.get('logger') ?? {};
169+
169170
// create server
170171
this.logger = logger = logger ?? createLogger(cfg.get('logger'));
171172
this.server = new Server(cfg.get('server'), logger);
172173

174+
// register api stubs
175+
Stub.logger = this.logger;
176+
Stub.register('DHLSoap', DHLSoap);
177+
173178
// get database connection
174179
const db = await database.get(cfg.get('database:main'), logger);
175180

test/mocks.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ const contactPoints = [
308308
payload: {
309309
id: 'contactPoint_1',
310310
contactPointTypeIds: [
311-
'legal_address'
311+
'legal'
312312
],
313313
name: 'Contact Point 1',
314314
description: 'A mocked Contact Point for testing',
@@ -350,7 +350,7 @@ const shops: ShopResponse[] = [
350350
id: 'shop_1',
351351
name: 'Shop1',
352352
description: 'a mocked shop for unit tests',
353-
domain: 'www.shop.com',
353+
domains: ['www.shop.com'],
354354
organizationId: organizations[0].payload?.id,
355355
shopNumber: '0000000001',
356356
},

0 commit comments

Comments
 (0)