Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: release #96

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 16 additions & 2 deletions cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@
"address": "localhost:50053"
},
"fulfillment": {
"disabled": false,
"disabled": true,
"address": "localhost:50067",
"createOnSubmit": true,
"cleanupPostSubmit": true
},
"fulfillment_product": {
"disabled": false,
"disabled": true,
"address": "localhost:50067"
},
"invoice": {
Expand Down Expand Up @@ -328,6 +328,20 @@
"message": "Query limit 1000 for {entity} exhausted!"
}
},
"errors": {
"INVALID_CREDENTIALS": {
"code": "401",
"message": "Invalid credentials"
},
"USER_NOT_LOGGED_IN": {
"code": "401",
"message": "Invalid authentication context, please log in first"
},
"ACTION_NOT_ALLOWED": {
"code": "403",
"message": "Action not allowed on this resource"
}
},
"preDefinedIds": {
"legalAddressTypeId": "legal"
}
Expand Down
3 changes: 0 additions & 3 deletions cfg/config_production.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,12 @@
"address": "resource-srv:50051"
},
"fulfillment": {
"disabled": false,
"address": "fulfillment-srv:50051"
},
"fulfillment_product": {
"disabled": false,
"address": "fulfillment-srv:50051"
},
"invoice": {
"disabled": true,
"address": "invoice-srv:50051"
}
},
Expand Down
114 changes: 75 additions & 39 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ export class OrderingService
code: 404,
message: '{entity} not found!',
},
ITEMS_MISSING: {
code: 404,
message: 'Some {entity} are missing in: {id}!',
},
};

protected readonly emitters: any;
Expand Down Expand Up @@ -498,13 +502,16 @@ export class OrderingService
}

private createOperationStatusCode(
entity: string,
status: OperationStatus,
status?: OperationStatus,
entity?: string,
id?: string,
): OperationStatus {
return {
code: status?.code ?? 500,
message: status?.message?.replace(
'{entity}', entity
'{entity}', entity ?? 'undefined'
).replace(
'{id}', id ?? 'undefined'
) ?? 'Unknown status',
};
}
Expand All @@ -531,8 +538,8 @@ export class OrderingService

if (order_ids.length > 1000) {
throw this.createOperationStatusCode(
this.name,
this.operation_status_codes.LIMIT_EXHAUSTED,
this.name,
);
}

Expand Down Expand Up @@ -671,14 +678,14 @@ export class OrderingService

if (!product_ids.length) {
throw this.createOperationStatusCode(
'product',
this.operation_status_codes.NO_ITEM,
'products',
);
}
else if (product_ids.length > 1000) {
throw this.createOperationStatusCode(
'product',
this.operation_status_codes.LIMIT_EXHAUSTED,
'products',
);
}

Expand Down Expand Up @@ -706,8 +713,8 @@ export class OrderingService
}
else if (!response.items?.length) {
throw this.createOperationStatusCode(
'products',
this.operation_status_codes.ITEM_NOT_FOUND,
'products',
);
}
else {
Expand Down Expand Up @@ -783,8 +790,8 @@ export class OrderingService
}
else if (!response.items?.length) {
throw this.createOperationStatusCode(
'taxes',
this.operation_status_codes.ITEM_NOT_FOUND,
'taxes',
);
}
else {
Expand All @@ -810,8 +817,8 @@ export class OrderingService

if (order_ids.length > 1000) {
throw this.createOperationStatusCode(
'fulfillment',
this.operation_status_codes.LIMIT_EXHAUSTED,
'fulfillment',
);
}

Expand Down Expand Up @@ -861,19 +868,25 @@ export class OrderingService
private async get<T>(
ids: (string | undefined)[],
service: CRUDClient,
entity: string,
subject?: Subject,
context?: any,
): Promise<T> {
ids = [...new Set<string | undefined>(ids)];
const entity = typeof ({} as T);
ids = [...new Set<string | undefined>(ids?.filter(
id => id
))];

if (ids.length > 1000) {
if (ids?.length > 1000) {
throw this.createOperationStatusCode(
entity,
this.operation_status_codes.LIMIT_EXHAUSTED,
entity,
);
}

if (!(ids?.length > 0)) {
return {} as T;
}

return await service.read(
{
filters: [{
Expand All @@ -895,10 +908,11 @@ export class OrderingService
if (response.operation_status?.code !== 200) {
throw response.operation_status;
}
else if (!response.items?.length) {
else if (response.items?.length < ids.length) {
throw this.createOperationStatusCode(
this.operation_status_codes.ITEMS_MISSING,
entity,
this.operation_status_codes.ITEM_NOT_FOUND,
`[${ids.join(', ')}]`,
);
}
else {
Expand Down Expand Up @@ -953,8 +967,8 @@ export class OrderingService
if (!order_list?.items?.length) {
return {
operation_status: this.createOperationStatusCode(
'order',
this.operation_status_codes.NO_ITEM,
'order',
)
};
}
Expand All @@ -972,32 +986,51 @@ export class OrderingService
const customer_map = await this.get<CustomerMap>(
order_list.items?.map(item => item.customer_id) ?? [],
this.customer_service,
'customers',
subject,
context,
);
const shop_map = await this.get<ShopMap>(
order_list.items?.map(item => item.shop_id) ?? [],
this.shop_service,
'shops',
subject,
context,
);
const organization_map = await this.get<OrganizationMap>(
Object.values(
shop_map
).map(
item => item.payload?.organization_id
),
[
...Object.values(
shop_map
).map(
item => item.payload?.organization_id
),
...Object.values(
customer_map
).map(
item => item.payload?.commercial?.organization_id
?? item.payload?.public_sector?.organization_id
)
],
this.organization_service,
'organizations',
subject,
context,
);
const contact_point_map = await this.get<ContactPointMap>(
Object.values(
organization_map
).flatMap(
item => item.payload?.contact_point_ids
),
[
...Object.values(
organization_map
).flatMap(
item => item.payload?.contact_point_ids
),
...Object.values(
customer_map
).flatMap(
item => item.payload?.private?.contact_point_ids
),
],
this.contact_point_service,
'contact_points',
subject,
context,
);
Expand All @@ -1008,6 +1041,7 @@ export class OrderingService
item => item.payload?.physical_address_id
),
this.address_service,
'addresses',
subject,
context,
);
Expand All @@ -1025,6 +1059,7 @@ export class OrderingService
)
],
this.country_service,
'countries',
subject,
context,
);
Expand Down Expand Up @@ -1150,22 +1185,23 @@ export class OrderingService
country => country.payload
);
const organization = organization_map[
customer.payload?.commercial?.organization_id
?? customer.payload?.public_sector?.organization_id
customer?.payload?.commercial?.organization_id
?? customer?.payload?.public_sector?.organization_id
];

if (customer.payload?.private) {
if (customer?.payload?.private) {
order.customer_type ??= CustomerType.PRIVATE;
order.user_id ??= customer?.payload?.private?.user_id;
order.user_id ??= customer.payload?.private?.user_id;
}
else if (customer.payload?.commercial) {
else if (customer?.payload?.commercial) {
order.customer_type ??= CustomerType.COMMERCIAL;
order.customer_vat_id ??= organization?.payload?.vat_id;
}
else if (customer.payload?.public_sector) {
else if (customer?.payload?.public_sector) {
order.customer_type ??= CustomerType.PUBLIC_SECTOR;
order.customer_vat_id ??= organization?.payload?.vat_id;
}
order.user_id = subject.id;

if (order.items?.length) {
await Promise.all(order.items?.map(
Expand Down Expand Up @@ -1197,7 +1233,7 @@ export class OrderingService
const vats = taxes.filter(
t => (
t.country_id === country?.id &&
!!customer?.payload!.private?.user_id &&
order.customer_type === CustomerType.PRIVATE &&
country?.economic_areas?.some(
ea => billing_country?.payload?.economic_areas?.includes(ea)
) &&
Expand Down Expand Up @@ -1289,11 +1325,11 @@ export class OrderingService
const operation_status = items.some(
a => a.status?.code !== 200
) ? this.createOperationStatusCode(
this.operation_status_codes.PARTIAL,
'order',
this.operation_status_codes.PARTIAL
) : this.createOperationStatusCode(
this.operation_status_codes.SUCCESS,
'order',
this.operation_status_codes.SUCCESS
);

return {
Expand Down Expand Up @@ -1521,8 +1557,8 @@ export class OrderingService
response => {
if (response.items?.length) {
throw this.createOperationStatusCode(
this.operation_status_codes.CONFLICT,
'order',
this.operation_status_codes.CONFLICT
);
}
}
Expand Down Expand Up @@ -1748,7 +1784,7 @@ export class OrderingService

return response;
}
catch (e) {
catch (e: any) {
return this.catchOperationError(e);
}
}
Expand Down Expand Up @@ -2091,8 +2127,8 @@ export class OrderingService
total_count: response.items?.length! + invalids.length,
operation_status: invalids.length
? this.createOperationStatusCode(
this.operation_status_codes.PARTIAL,
'fulfillment',
this.operation_status_codes.PARTIAL
)
: response.operation_status,
};
Expand Down Expand Up @@ -2410,8 +2446,8 @@ export class OrderingService
total_count: response.items?.length! + invalids.length,
operation_status: invalids.length
? this.createOperationStatusCode(
'Invoice',
this.operation_status_codes.PARTIAL,
'Invoice',
)
: response.operation_status,
};
Expand Down
Loading
Loading