Skip to content

Commit

Permalink
fix(fulfillment_product): more tolerant product.find, imporve error r…
Browse files Browse the repository at this point in the history
…esponse, user sender and recip
  • Loading branch information
Gerald Baulig committed Oct 2, 2024
1 parent df5216b commit 8d3d2e0
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 77 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Build
FROM node:22.2.0-alpine3.20 as build
FROM node:22.2.0-alpine3.20 AS build
ENV NO_UPDATE_NOTIFIER=true

USER node
Expand All @@ -13,7 +13,7 @@ RUN npm run build


### Deployment
FROM node:22.2.0-alpine3.20 as deployment
FROM node:22.2.0-alpine3.20 AS deployment

ENV NO_UPDATE_NOTIFIER=true

Expand Down
3 changes: 2 additions & 1 deletion cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@
}
},
"preDefinedIds": {
"legalAddressTypeId": "legal_address"
"legalAddressTypeId": "legal",
"shippingAddressTypeId": "shipping"
},
"stubs": {
"DHLSoap": {
Expand Down
155 changes: 92 additions & 63 deletions src/services/fulfillment_product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export class FulfillmentProductService
code: 404,
message: '{entity} {id} has no legal address!',
},
NO_SHIPPING_ADDRESS: {
id: '',
code: 404,
message: '{entity} {id} has no shipping address!',
}
};

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

protected readonly legal_address_type_id: string;
protected readonly shipping_address_type_id: string;
protected readonly customer_service: Client<CustomerServiceDefinition>;
protected readonly shop_service: Client<ShopServiceDefinition>;
protected readonly organization_service: Client<OrganizationServiceDefinition>;
Expand Down Expand Up @@ -216,6 +222,7 @@ export class FulfillmentProductService
};

this.legal_address_type_id = this.cfg.get('preDefinedIds:legalAddressTypeId');
this.shipping_address_type_id = this.cfg.get('preDefinedIds:shippingAddressTypeId');

this.customer_service = createClient(
{
Expand Down Expand Up @@ -352,7 +359,7 @@ export class FulfillmentProductService
subject?: Subject,
context?: any,
): Promise<ResponseMap<T>> {
ids = [...new Set(ids)];
ids = [...new Set(ids.filter(id => id))];
const entity = ({} as new() => T).name;

if (ids.length > 1000) {
Expand Down Expand Up @@ -473,7 +480,7 @@ export class FulfillmentProductService
}],
subject,
});
return await this.courier_srv.read(call, context).then(
const response = await this.courier_srv.read(call, context).then(
resp => {
if (resp.operation_status?.code !== 200) {
throw resp.operation_status;
Expand All @@ -483,6 +490,8 @@ export class FulfillmentProductService
}
}
);
this.logger.debug('Available couriers', response);
return response;
}

protected async findFulfillmentProducts(
Expand Down Expand Up @@ -663,9 +672,13 @@ export class FulfillmentProductService
);

const country_map = await this.get<Country>(
Object.values(address_map).map(
item => item.payload?.country_id
),
[
...Object.values(address_map).map(
item => item.payload?.country_id
),
...queries.map(query => query.sender?.address?.country_id),
...queries.map(query => query.recipient?.address?.country_id)
],
this.country_service,
request.subject,
context,
Expand Down Expand Up @@ -701,9 +714,9 @@ export class FulfillmentProductService
name: `${product.payload?.id}\t${variant.id}`,
price: variant.price.sale ? variant.price.sale_price : variant.price.regular_price,
maxWeight: variant.max_weight,
width: variant.max_size.width,
height: variant.max_size.height,
depth: variant.max_size.length,
width: variant.max_size?.width,
height: variant.max_size?.height,
depth: variant.max_size?.length,
type: 'parcel'
}
)
Expand All @@ -728,71 +741,81 @@ export class FulfillmentProductService
shipping: null
});

const shop_country = await this.getById(
shop_map,
query.shop_id
).then(
shop => this.getById(
orga_map,
shop.payload.organization_id
)
).then(
orga => this.getByIds(
contact_point_map,
orga.payload.contact_point_ids
const shop_country = query.sender?.address?.country_id
? await this.getById(
country_map,
query.sender.address.country_id
)
).then(
cpts => cpts.find(
cpt => cpt.payload?.contact_point_type_ids.includes(
this.legal_address_type_id
: await this.getById(
shop_map,
query.shop_id
).then(
shop => this.getById(
orga_map,
shop.payload.organization_id
)
) ?? this.throwStatusCode<ContactPointResponse>(
query.reference.instance_type,
query.reference.instance_id,
this.status_codes.NO_LEGAL_ADDRESS,
)
).then(
contact_point => this.getById(
address_map,
contact_point.payload.physical_address_id
)
).then(
address => this.getById(country_map, address.payload.country_id)
);
).then(
orga => this.getByIds(
contact_point_map,
orga.payload.contact_point_ids
)
).then(
cpts => cpts.find(
cpt => cpt.payload?.contact_point_type_ids.includes(
this.legal_address_type_id
)
) ?? this.throwStatusCode<ContactPointResponse>(
'Shop',
query.shop_id,
this.status_codes.NO_LEGAL_ADDRESS,
)
).then(
contact_point => this.getById(
address_map,
contact_point.payload.physical_address_id
)
).then(
address => this.getById(country_map, address.payload.country_id)
);

const customer = await this.getById(
customer_map,
query.customer_id
);

const customer_country = await this.getByIds(
contact_point_map,
[
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)
).then(
cps => cps.find(
cp => cp.payload?.contact_point_type_ids.includes(
this.legal_address_type_id
)
) ?? this.throwStatusCode<ContactPointResponse>(
query.reference.instance_type,
query.reference.instance_id,
this.status_codes.NO_LEGAL_ADDRESS,
)
).then(
cp => this.getById(
address_map,
cp.payload.physical_address_id,
)
).then(
address => this.getById(
const customer_country = query.recipient?.address?.country_id
? await this.getById(
country_map,
address.payload.country_id
query.recipient.address.country_id
)
);
: await this.getByIds(
contact_point_map,
[
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)
).then(
cps => cps.find(
cp => cp.payload?.contact_point_type_ids.includes(
this.shipping_address_type_id
)
) ?? this.throwStatusCode<ContactPointResponse>(
'Customer',
customer.payload.id,
this.status_codes.NO_SHIPPING_ADDRESS,
)
).then(
cp => this.getById(
address_map,
cp.payload.physical_address_id,
)
).then(
address => this.getById(
country_map,
address.payload.country_id
)
);

const solutions: PackingSolution[] = offer_lists.map(
offers => packer.canFit(offers, goods)
Expand Down Expand Up @@ -887,6 +910,12 @@ export class FulfillmentProductService
reference: query.reference,
};
}
).sort(
(a, b) => Math.min(
...a.amounts?.map(am => am.net)
) - Math.min(
...b.amounts?.map(am => am.net)
)
);

const solution: PackingSolutionResponse = {
Expand Down
11 changes: 9 additions & 2 deletions src/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,17 @@ export abstract class Stub
}

public static register<T extends Stub>(
typeName: string,
type_name: string,
type: (new (courier: Courier, kwargs?: { [key: string]: any }) => T)
) {
Stub.STUB_TYPES[typeName] = type;
Stub.STUB_TYPES[type_name] = type;
Stub.logger?.info(
'Courier Stub registered:',
{
name: type_name,
'type': type,
}
);
}

public static getInstance(courier: Courier, kwargs?: { [key: string]: any }): Stub
Expand Down
4 changes: 2 additions & 2 deletions src/stubs/dhl_soap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ const DHLShipmentCancelResponse2AggregatedFulfillment = (
});
};

class DHLSoap extends Stub {
export class DHLSoap extends Stub {
protected static _clients: ClientMap = {};
protected readonly stub_defaults: any;
public readonly version: number[];
Expand Down Expand Up @@ -761,4 +761,4 @@ class DHLSoap extends Stub {
}
};

Stub.register(DHLSoap.name, DHLSoap);
Stub.register('DHLSoap', DHLSoap);
4 changes: 2 additions & 2 deletions src/stubs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Register Stubs here!
import './dhl_soap.js';
import './dummy.js';
export { DHLSoap } from './dhl_soap.js';
//export { DHLRest } from './dhl_rest.js';
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const filterTax = (
(
!target.economic_areas ||
origin.economic_areas?.some(
e => e in target.economic_areas
e => target.economic_areas.includes(e)
)
)
);
Expand Down
9 changes: 7 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { FulfillmentService } from './services/fulfillment.js';
import { FulfillmentCourierService } from './services/fulfillment_courier.js';
import { FulfillmentProductService } from './services/fulfillment_product.js';
import { FulfillmentCommandInterface } from './services/fulfillment_command_interface.js';
import './stubs/index.js';
import { Stub } from './stub.js';
import { DHLSoap } from './stubs/index.js';

registerProtoMeta(
FulfillmentMeta,
Expand Down Expand Up @@ -165,11 +166,15 @@ export class Worker {
async start(cfg?: any, logger?: any): Promise<any> {
// Load config
this._cfg = cfg = cfg ?? createServiceConfig(process.cwd());
const logger_cfg = cfg.get('logger') ?? {};

// create server
this.logger = logger = logger ?? createLogger(cfg.get('logger'));
this.server = new Server(cfg.get('server'), logger);

// register api stubs
Stub.logger = this.logger;
Stub.register('DHLSoap', DHLSoap);

// get database connection
const db = await database.get(cfg.get('database:main'), logger);

Expand Down
4 changes: 2 additions & 2 deletions test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const contactPoints = [
payload: {
id: 'contactPoint_1',
contactPointTypeIds: [
'legal_address'
'legal'
],
name: 'Contact Point 1',
description: 'A mocked Contact Point for testing',
Expand Down Expand Up @@ -350,7 +350,7 @@ const shops: ShopResponse[] = [
id: 'shop_1',
name: 'Shop1',
description: 'a mocked shop for unit tests',
domain: 'www.shop.com',
domains: ['www.shop.com'],
organizationId: organizations[0].payload?.id,
shopNumber: '0000000001',
},
Expand Down

0 comments on commit 8d3d2e0

Please sign in to comment.