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 #88

Merged
merged 4 commits into from
Apr 17, 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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.8
20.11.1
16 changes: 2 additions & 14 deletions cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@
"events": {
"enableEvents": true,
"emitters": {
"FAILED": "orderFailed",
"INVALID": "orderInvalid",
"PENDING": "orderPending",
"SUBMITTED": "orderSubmitted",
"IN_PROCESS": "orderInProcess",
"DONE": "orderDone",
"COMPLETED": "orderCompleted",
"WITHDRAWN": "orderWithdrawn",
"CANCELLED": "orderCancelled",
"CREATE_FULFILLMENTS": "createFulfillments",
Expand Down Expand Up @@ -168,17 +167,6 @@
"withdrawOrders": "handleWithdrawOrders",
"cancelOrders": "handleCancelOrders"
}
},
"fulfillment.resource": {
"topic": "io.restorecommerce.fulfillment.resource",
"events": {
"fulfillmentSubmitted": "handleFulfillmentSubmitted",
"fulfillmentInvalid": "handleFulfillmentInvalid",
"fulfillmentFailed": "handleFulfillmentFailed",
"fulfillmentFulfilled": "handleFulfillmentFulfilled",
"fulfillmentWithdrawn": "handleFulfillmentWithdrawn",
"fulfillmentCancelled": "handleFulfillmentCancelled"
}
}
}
}
Expand Down
61 changes: 21 additions & 40 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ import {
VAT
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/amount.js';
import { Subject } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/auth.js';
import { COUNTRY_CODES_EU } from './utils.js';

export type RatioedTax = Tax & {
tax_ratio?: number;
Expand Down Expand Up @@ -1142,8 +1141,8 @@ export class OrderingService
product_map,
order.id,
);
const shipping_address = await this.getById(
order.shipping_address?.address?.country_id,
const billing_address = await this.getById(
order.billing_address?.address?.country_id,
country_map,
order.id,
);
Expand All @@ -1156,9 +1155,10 @@ export class OrderingService
t => (
t.country_id === country?.id &&
!!customer?.payload!.private?.user_id &&
country?.country_code! in COUNTRY_CODES_EU &&
shipping_address?.payload!.country_code! in COUNTRY_CODES_EU &&
(variant?.tax_ids?.length && t.id! in variant.tax_ids)
country?.economic_areas?.some(
ea => billing_address?.payload?.economic_areas?.includes(ea)
) &&
(variant?.tax_ids?.length && variant.tax_ids?.includes(t.id!))
)
).map(
t => ({
Expand Down Expand Up @@ -1231,9 +1231,6 @@ export class OrderingService
} as OrderResponse;
}
catch (e: any) {
if (order) {
order.order_state = OrderState.INVALID;
};
return {
payload: order,
status: {
Expand Down Expand Up @@ -1298,14 +1295,11 @@ export class OrderingService
response.items?.forEach(
(item: OrderResponse) => {
responseMap[item.payload?.id ?? item.status?.id!] = item;
if (item.status?.code === 200 && item?.payload?.order_state! in this.emitters) {
switch (item.payload?.order_state) {
case OrderState.INVALID, OrderState.FAILED:
this.topic.emit(this.emitters[item.payload.order_state], item);
default:
this.topic.emit(this.emitters[item.payload?.order_state!], item.payload!);
break;
}
if (item.status?.code !== 200 && 'INVALID' in this.emitters) {
this.topic.emit(this.emitters['INVALID'], item.payload!);
}
else if (item?.payload?.order_state! in this.emitters) {
this.topic.emit(this.emitters[item.payload?.order_state!], item.payload!);
}
}
);
Expand Down Expand Up @@ -1356,7 +1350,7 @@ export class OrderingService
request?.items?.forEach(
item => {
if (!item.order_state || item.order_state === OrderState.UNRECOGNIZED) {
item.order_state = OrderState.CREATED;
item.order_state = OrderState.PENDING;
}
}
);
Expand Down Expand Up @@ -1394,7 +1388,7 @@ export class OrderingService
request?.items?.forEach(
item => {
if (!item.order_state || item.order_state === OrderState.UNRECOGNIZED) {
item.order_state = OrderState.CREATED;
item.order_state = OrderState.PENDING;
}
}
);
Expand Down Expand Up @@ -1564,10 +1558,6 @@ export class OrderingService
reference => {
const order = responseMap[reference.instance_id!];
if (fulfillment.status?.code !== 200 && order) {
order.payload = {
...order.payload,
order_state: OrderState.INVALID
};
order.status = {
...fulfillment.status,
id: order.payload?.id ?? order.status?.id,
Expand Down Expand Up @@ -1617,10 +1607,6 @@ export class OrderingService
reference => {
const order = responseMap[reference.instance_id!];
if (invoice.status?.code !== 200 && order) {
order.payload = {
...order.payload,
order_state: OrderState.INVALID
};
order.status = {
...invoice.status,
id: order.payload?.id ?? order.status?.id,
Expand All @@ -1640,8 +1626,8 @@ export class OrderingService

if (this.cleanup_fulfillments_post_submit) {
const failed_fulfillment_ids = response.fulfillments?.filter(
fulfillment => fulfillment.payload?.references?.find(
reference => reference.instance_id! in failed_order_ids
fulfillment => fulfillment.payload?.references?.some(
reference => failed_order_ids.includes(reference.instance_id!)
)
).map(
fulfillment => fulfillment.payload?.id!
Expand All @@ -1661,7 +1647,7 @@ export class OrderingService
if (this.cleanup_invoices_post_submit) {
const failed_invoice_ids = response.invoices?.filter(
invoice => invoice.payload?.references?.find(
reference => reference.instance_id! in failed_order_ids
reference => failed_order_ids.includes(reference.instance_id!)
)
).map(
invoice => invoice.payload?.id!
Expand Down Expand Up @@ -1689,16 +1675,11 @@ export class OrderingService

Object.values(responseMap).forEach(
item => {
if (
item.payload?.order_state! in this.emitters
) {
switch (item.payload?.order_state) {
case OrderState.INVALID, OrderState.FAILED:
this.topic.emit(this.emitters[item.payload.order_state], item);
default:
this.topic.emit(this.emitters[item.payload?.order_state!], item.payload!);
break;
}
if (item.status?.code !== 200 && 'INVALID' in this.emitters) {
this.topic.emit(this.emitters['INVALID'], item.payload!);
}
else if (item.payload?.order_state! in this.emitters) {
this.topic.emit(this.emitters[item.payload?.order_state!], item.payload!);
}
}
);
Expand Down
33 changes: 0 additions & 33 deletions src/utils.ts

This file was deleted.

62 changes: 0 additions & 62 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import { Logger } from 'winston';
import { BindConfig } from '@restorecommerce/chassis-srv/lib/microservice/transport/provider/grpc/index.js';
import { HealthDefinition } from '@restorecommerce/rc-grpc-clients/dist/generated-server/grpc/health/v1/health.js';
import { ServerReflectionService } from 'nice-grpc-server-reflection';
import { Fulfillment } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/fulfillment.js';
import { Subject } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/auth.js';
import { OrderingCommandInterface } from './command_interface.js';
import { initAuthZ } from '@restorecommerce/acs-client';

Expand Down Expand Up @@ -170,66 +168,6 @@ export class Worker {
error => this.logger?.error(`Error while handling event ${eventName}: `, { error })
);
},
handleFulfillmentSubmitted: (msg: Fulfillment, context: any, config: any, eventName: string) => {
const refs = msg?.references?.filter(ref => ref.instance_type === this.orderingService?.instanceType);
if (!refs?.length) return;
const ids = refs!.map(ref => ref.instance_id!);
const subject = {} as Subject; // System Admin?
return this.orderingService?.updateState(ids, OrderState.IN_PROCESS, subject, context).then(
() => this.logger?.info(`Event ${eventName} handled.`),
error => this.logger?.error(`Error while handling event ${eventName}: `, { error })
);
},
handleFulfillmentInvalid: (msg: Fulfillment, context: any, config: any, eventName: string) => {
const refs = msg?.references?.filter(ref => ref.instance_type === this.orderingService?.instanceType);
if (!refs?.length) return;
const ids = refs!.map(ref => ref.instance_id!);
const subject = {} as Subject; // System Admin?
return this.orderingService?.updateState(ids, OrderState.INVALID, subject, context).then(
() => this.logger?.info(`Event ${eventName} handled.`),
error => this.logger?.error(`Error while handling event ${eventName}: `, { error })
);
},
handleFulfillmentFulfilled: (msg: Fulfillment, context: any, config: any, eventName: string) => {
const refs = msg?.references?.filter(ref => ref.instance_type === this.orderingService?.instanceType);
if (!refs?.length) return;
const ids = refs!.map(ref => ref.instance_id!);
const subject = {} as Subject; // System Admin?
return this.orderingService?.updateState(ids, OrderState.DONE, subject, context).then(
() => this.logger?.info(`Event ${eventName} handled.`),
error => this.logger?.error(`Error while handling event ${eventName}: `, { error })
);
},
handleFulfillmentFailed: (msg: Fulfillment, context: any, config: any, eventName: string) => {
const refs = msg?.references?.filter(ref => ref.instance_type === this.orderingService?.instanceType);
if (!refs?.length) return;
const ids = refs!.map(ref => ref.instance_id!);
const subject = {} as Subject; // System Admin?
return this.orderingService?.updateState(ids, OrderState.FAILED, subject, context).then(
() => this.logger?.info(`Event ${eventName} handled.`),
error => this.logger?.error(`Error while handling event ${eventName}: `, { error })
);
},
handleFulfillmentWithdrawn: (msg: Fulfillment, context: any, config: any, eventName: string) => {
const refs = msg?.references?.filter(ref => ref.instance_type === this.orderingService?.instanceType);
if (!refs?.length) return;
const ids = refs!.map(ref => ref.instance_id!);
const subject = {} as Subject; // System Admin?
return this.orderingService?.updateState(ids, OrderState.CANCELLED, subject, context).then(
() => this.logger?.info(`Event ${eventName} handled.`),
error => this.logger?.error(`Error while handling event ${eventName}: `, { error })
);
},
handleFulfillmentCancelled: (msg: Fulfillment, context: any, config: any, eventName: string) => {
const refs = msg?.references?.filter(ref => ref.instance_type === this.orderingService?.instanceType);
if (!refs?.length) return;
const ids = refs!.map(ref => ref.instance_id!);
const subject = {} as Subject; // System Admin?
return this.orderingService?.updateState(ids, OrderState.CANCELLED, subject, context).then(
() => this.logger?.info(`Event ${eventName} handled.`),
error => this.logger?.error(`Error while handling event ${eventName}: `, { error })
);
},
handleQueuedJob: (msg: any, context: any, config: any, eventName: string) => {
return this.serviceActions?.get(msg?.type)?.(msg?.data?.payload, context, config, msg?.type).then(
() => this.logger?.info(`Job ${msg?.type} done.`),
Expand Down
10 changes: 5 additions & 5 deletions test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ const validOrders: { [key: string]: OrderList } = {
quantity: 4,
}
],
orderState: OrderState.CREATED,
orderState: OrderState.PENDING,
totalAmounts: [],
notificationEmail: '[email protected]',
packagingPreferences: {
Expand Down Expand Up @@ -376,7 +376,7 @@ const validOrders: { [key: string]: OrderList } = {
userId: 'user_1',
customerId: 'customer_1',
shopId: 'shop_1',
orderState: OrderState.CREATED,
orderState: OrderState.PENDING,
totalAmounts: [],
notificationEmail: '[email protected]',
packagingPreferences: {
Expand Down Expand Up @@ -423,7 +423,7 @@ const invalidOrders: { [key: string]: OrderList } = {
totalAmounts: [],
billingAddress: residentialAddresses[0],
shippingAddress: residentialAddresses[0],
orderState: OrderState.CREATED,
orderState: OrderState.PENDING,
},
{
id: 'invalidOrder_2',
Expand All @@ -441,7 +441,7 @@ const invalidOrders: { [key: string]: OrderList } = {
options: [],
},
totalAmounts: [],
orderState: OrderState.CREATED,
orderState: OrderState.PENDING,
}
],
totalCount: 2,
Expand Down Expand Up @@ -473,7 +473,7 @@ const invalidOrders: { [key: string]: OrderList } = {
totalAmounts: [],
billingAddress: residentialAddresses[0],
shippingAddress: residentialAddresses[0],
orderState: OrderState.CREATED,
orderState: OrderState.PENDING,
}
],
totalCount: 1,
Expand Down
2 changes: 1 addition & 1 deletion test/ordering-srv.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('The Ordering Service:', () => {
const orderSubmittedSemaphore = new Semaphore(0);

const onOrderCreated = (msg: Order_, context?:any): void => {
should.equal(msg?.order_state, Order_State.CREATED);
should.equal(msg?.order_state, Order_State.PENDING);
orderCreatedSemaphore.release(1);
};

Expand Down
Loading