diff --git a/src/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.ts b/src/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.ts index 2ed3e811..2787e09a 100644 --- a/src/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.ts +++ b/src/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.ts @@ -147,11 +147,12 @@ export class OrderFulfilledHandler } catch (err) { this.logger.log(err); this.logger.log(`Forward payment failed | err -> ${err}`); + console.log(err); } } - callbackInsertNotificationLogging(data: NotificationDto) { - this.notificationService.insert(data); + async callbackInsertNotificationLogging(data: NotificationDto) { + await this.notificationService.insert(data); } async callbackSendReward( @@ -202,7 +203,7 @@ export class OrderFulfilledHandler block_number: blockNumber, }; - this.callbackInsertNotificationLogging(customerNotificationInput); + await this.callbackInsertNotificationLogging(customerNotificationInput); // Write Logging Reward Customer Staking Request Service const dataCustomerLoggingInput: TransactionLoggingDto = { @@ -217,6 +218,8 @@ export class OrderFulfilledHandler }; await this.loggingService.create(dataCustomerLoggingInput); + await this.delay(6000); + // Send reward to lab await sendRewards( this.substrateService.api as any, @@ -241,7 +244,7 @@ export class OrderFulfilledHandler block_number: blockNumber, }; - this.callbackInsertNotificationLogging(labNotificationInput); + await this.callbackInsertNotificationLogging(labNotificationInput); // Write Logging Reward Lab const dataLabLoggingInput: TransactionLoggingDto = { @@ -260,4 +263,8 @@ export class OrderFulfilledHandler private convertToDate(date: Date) { return new Date(Number(date.toString().split(',').join(''))); } + + private delay(ms: number) { + return new Promise((resolve) => setTimeout(() => resolve(true), ms)); + } } diff --git a/test/unit/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.spec.ts b/test/unit/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.spec.ts index 49dcf7e7..59152da3 100644 --- a/test/unit/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.spec.ts +++ b/test/unit/listeners/substrate-listener/commands/orders/order-fulfilled/order-fulfilled.handler.spec.ts @@ -45,9 +45,6 @@ describe('Order Fulfilled Handler Event', () => { let dateTimeProxyMock: MockType; beforeEach(async () => { - jest - .useFakeTimers() - .setSystemTime(new Date('1970-01-01T00:00:00.001Z').getTime()); const module: TestingModule = await Test.createTestingModule({ providers: [ { @@ -113,6 +110,7 @@ describe('Order Fulfilled Handler Event', () => { const convertToDbioUnitStringSpy = jest .spyOn(globalProviderMethods, 'convertToDbioUnitString') .mockImplementation(); + const DATE = new Date(1669649548467); const ORDER = createMockOrder(OrderStatus.Cancelled); @@ -205,6 +203,7 @@ describe('Order Fulfilled Handler Event', () => { expect(queryServiceByIdSpy).not.toHaveBeenCalled(); expect(queryServiceRequestById).not.toHaveBeenCalled(); expect(debioConversionServiceMock.getExchange).not.toHaveBeenCalled(); + expect(sendRewardsSpy).not.toHaveBeenCalled(); expect(convertToDbioUnitStringSpy).not.toHaveBeenCalled(); expect(queryServiceRequestById).not.toHaveBeenCalled(); @@ -220,6 +219,7 @@ describe('Order Fulfilled Handler Event', () => { queryServiceByIdSpy.mockClear(); queryServiceRequestById.mockClear(); sendRewardsSpy.mockClear(); + convertToDbioUnitStringSpy.mockClear(); }); @@ -347,7 +347,7 @@ describe('Order Fulfilled Handler Event', () => { queryServiceInvoiceByOrderIdSpy.mockClear(); sendRewardsSpy.mockClear(); convertToDbioUnitStringSpy.mockClear(); - }); + }, 12000); it('when eth address isNone true', async () => { // Arrange @@ -369,6 +369,7 @@ describe('Order Fulfilled Handler Event', () => { const convertToDbioUnitStringSpy = jest .spyOn(globalProviderMethods, 'convertToDbioUnitString') .mockImplementation(); + const DATE = new Date(1669649548467); const ORDER = createMockOrder(OrderStatus.Cancelled); @@ -477,6 +478,7 @@ describe('Order Fulfilled Handler Event', () => { expect(queryOrderDetailByOrderIDSpy).not.toHaveBeenCalled(); expect(queryServiceByIdSpy).not.toHaveBeenCalled(); expect(queryServiceInvoiceByOrderIdSpy).not.toHaveBeenCalled(); + expect(sendRewardsSpy).not.toHaveBeenCalled(); expect(convertToDbioUnitStringSpy).not.toHaveBeenCalled(); expect(queryServiceInvoiceByOrderIdSpy).not.toHaveBeenCalled(); @@ -493,7 +495,7 @@ describe('Order Fulfilled Handler Event', () => { queryServiceInvoiceByOrderIdSpy.mockClear(); sendRewardsSpy.mockClear(); convertToDbioUnitStringSpy.mockClear(); - }); + }, 12000); it('when order and service flow is not StakingRequestService', async () => { // Arrange @@ -515,6 +517,7 @@ describe('Order Fulfilled Handler Event', () => { const convertToDbioUnitStringSpy = jest .spyOn(globalProviderMethods, 'convertToDbioUnitString') .mockImplementation(); + const DATE = new Date(1669649548467); const ORDER = createMockOrder(OrderStatus.Cancelled); @@ -607,6 +610,7 @@ describe('Order Fulfilled Handler Event', () => { expect(queryServiceByIdSpy).not.toHaveBeenCalled(); expect(queryServiceInvoiceByOrderIdSpy).not.toHaveBeenCalled(); expect(debioConversionServiceMock.getExchange).not.toHaveBeenCalled(); + expect(sendRewardsSpy).not.toHaveBeenCalled(); expect(convertToDbioUnitStringSpy).not.toHaveBeenCalled(); expect(queryServiceInvoiceByOrderIdSpy).not.toHaveBeenCalled(); @@ -623,7 +627,7 @@ describe('Order Fulfilled Handler Event', () => { queryServiceInvoiceByOrderIdSpy.mockClear(); sendRewardsSpy.mockClear(); convertToDbioUnitStringSpy.mockClear(); - }); + }, 12000); it('called reward callback', async () => { const sendRewardsSpy = jest @@ -648,5 +652,5 @@ describe('Order Fulfilled Handler Event', () => { expect(transactionLoggingServiceMock.create).toHaveBeenCalled(); expect(sendRewardsSpy).toHaveBeenCalledTimes(2); expect(transactionLoggingServiceMock.create).toHaveBeenCalledTimes(2); - }); + }, 12000); });