Skip to content

Commit

Permalink
fix: update transaction hash from blockchain (#214)
Browse files Browse the repository at this point in the history
* update transaction hash from blockchain

* update transaction loggin and test

* update genetic analysis order and order

* fix format

* fix test unit
  • Loading branch information
rubenkristian authored Nov 21, 2022
1 parent 9633380 commit ee8c28d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class OrderPaidHandler
created_at: order.createdAt,
updated_at: order.updatedAt,
order_flow: order.orderFlow,
transaction_hash: command.blockMetaData.blockHash,
blockMetaData: command.blockMetaData,
},
},
Expand Down
13 changes: 0 additions & 13 deletions src/listeners/ethereum-listener/ethereum-listener.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,6 @@ export class EthereumListenerHandler implements OnModuleInit {
if (event?.transactionHash) {
this.logger.log(`transaction Hash: ${event.transactionHash}`);
}
//Update transaction_hash to DB
const loggingFulfilled =
await this.transactioLoggingService.getLoggingByHashAndStatus(
order.orderId,
3,
);

if (loggingFulfilled) {
await this.transactioLoggingService.updateHash(
loggingFulfilled,
event.transactionHash,
);
}
});

escrowContract.on('OrderRefunded', async (order, event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export class GeneticAnalysisOrderFulfilledHandler
) {}

async execute(command: GeneticAnalysisOrderFulfilledCommand) {
const geneticAnalysisOrder = command.geneticAnalysisOrders;
const blockNumber = command.blockMetaData.blockNumber.toString();
const { geneticAnalysisOrders, blockMetaData } = command;
const geneticAnalysisOrder = { ...geneticAnalysisOrders };
const blockNumber = blockMetaData.blockNumber.toString();
this.logger.log(
`Genetic Analysis Order Fulfilled! With GA Order ID: ${geneticAnalysisOrder.id}`,
);
Expand All @@ -51,12 +52,14 @@ export class GeneticAnalysisOrderFulfilledHandler
0,
);

const amountToForward = totalPrice + totalAdditionalPrice;
const amountToForward =
(totalPrice + totalAdditionalPrice) /
currencyUnit[geneticAnalysisOrder.currency];

const geneticAnalysisOrderLogging: TransactionLoggingDto = {
address: geneticAnalysisOrder.customerId,
amount: amountToForward / currencyUnit[geneticAnalysisOrder.currency],
created_at: geneticAnalysisOrder.updatedAt,
amount: amountToForward,
created_at: this.convertToDate(geneticAnalysisOrder.updatedAt),
currency: geneticAnalysisOrder.currency.toUpperCase(),
parent_id: BigInt(geneticAnalysisOrderHistory.id),
ref_number: geneticAnalysisOrder.id,
Expand All @@ -66,11 +69,8 @@ export class GeneticAnalysisOrderFulfilledHandler

const serviceChargeLogging: TransactionLoggingDto = {
address: geneticAnalysisOrder.customerId,
amount:
((amountToForward / currencyUnit[geneticAnalysisOrder.currency]) *
5) /
100, //5% prices
created_at: geneticAnalysisOrder.updatedAt,
amount: (amountToForward * 5) / 100, //5% prices
created_at: this.convertToDate(geneticAnalysisOrder.updatedAt),
currency: geneticAnalysisOrder.currency.toUpperCase(),
parent_id: BigInt(geneticAnalysisOrderHistory.id),
ref_number: geneticAnalysisOrder.id,
Expand All @@ -90,11 +90,7 @@ export class GeneticAnalysisOrderFulfilledHandler
entity_type: 'Genetic Analysis Order',
entity: 'Order Fulfilled',
reference_id: geneticAnalysisOrder.id,
description: `You've received ${
amountToForward / currencyUnit[geneticAnalysisOrder.currency]
} ${
geneticAnalysisOrder.currency
} for completing the requested analysis for [].`,
description: `You've received ${amountToForward} ${geneticAnalysisOrder.currency} for completing the requested analysis for [].`,
read: false,
created_at: currDate,
updated_at: currDate,
Expand All @@ -109,4 +105,8 @@ export class GeneticAnalysisOrderFulfilledHandler
this.logger.log(error);
}
}

private convertToDate(date: Date) {
return new Date(Number(date.toString().split(',').join('')));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
TransactionLoggingService,
} from '../../../../../common';
import {
convertToDbioUnitString,
finalizeRequest,
Order,
queryEthAdressByAccountId,
Expand Down Expand Up @@ -67,7 +66,7 @@ export class OrderFulfilledHandler
const orderLogging: TransactionLoggingDto = {
address: order.customerId,
amount: amountToForward / currencyUnit[order.currency],
created_at: order.updatedAt,
created_at: this.convertToDate(order.updatedAt),
currency: order.currency.toUpperCase(),
parent_id: orderHistory?.id ? BigInt(orderHistory.id) : BigInt(0),
ref_number: order.id,
Expand All @@ -77,6 +76,10 @@ export class OrderFulfilledHandler

// Logging transaction
if (isOrderHasBeenInsert) {
await this.loggingService.updateHash(
isOrderHasBeenInsert,
command.blockMetaData.blockHash,
);
return;
}
const labEthAddress: any = await queryEthAdressByAccountId(
Expand Down Expand Up @@ -244,4 +247,8 @@ export class OrderFulfilledHandler
};
await this.loggingService.create(dataLabLoggingInput);
}

private convertToDate(date: Date) {
return new Date(Number(date.toString().split(',').join('')));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import { LabRating } from '../../../../../mock/models/rating/rating.entity';
import { TransactionRequest } from '../../../../../../src/common/transaction-logging/models/transaction-request.entity';
import { dummyCredentials } from '../../../../config';
import { EscrowService } from '../../../../../../src/common/escrow/escrow.service';
import { escrowServiceMockFactory } from '../../../../../unit/mock';
import {
escrowServiceMockFactory,
mailerManagerMockFactory,
} from '../../../../../unit/mock';
import {
DateTimeModule,
MailerManager,
NotificationModule,
ProcessEnvModule,
SubstrateModule,
Expand Down Expand Up @@ -164,6 +168,10 @@ describe('Genetic Analysis Order Created Integration Test', () => {
provide: EscrowService,
useFactory: escrowServiceMockFactory,
},
{
provide: MailerManager,
useFactory: mailerManagerMockFactory,
},
SubstrateListenerHandler,
GeneticAnalysisOrderCreatedHandler,
GeneticAnalysisOrderFulfilledHandler,
Expand Down Expand Up @@ -320,7 +328,7 @@ describe('Genetic Analysis Order Created Integration Test', () => {
expect(notifications[0].entity).toEqual('Order Created');
expect(
notifications[0].description.includes(
`You've successfully submitted your requested test for [].`,
`You've successfully submitted your requested test for ${geneticAnalysisOrder.geneticAnalysisTrackingId}.`,
),
).toBeTruthy();
expect(notifications[0].reference_id).toEqual(geneticAnalysisOrder.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ export const geneticAnalystServiceDataMock = {
name: 'string',
pricesByCurrency: [
{
currency: 'DAI',
totalPrice: BigInt('1000000000000000000'),
currency: 'USDT',
totalPrice: BigInt('10000000'),
priceComponents: [
{
component: 'string',
value: BigInt('900000000000000000'),
value: BigInt('9000000'),
},
],
additionalPrices: [
{
component: 'string',
value: BigInt('100000000000000000'),
value: BigInt('1000000'),
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ describe('Ethereum Listener Handler Unit Test', () => {
expect(smartContractOnEventType).toEqual('OrderRefunded');
expect(
transactionLoggingService.getLoggingByHashAndStatus,
).toHaveBeenCalledTimes(2);
).toHaveBeenCalledTimes(1);
expect(
transactionLoggingService.getLoggingByHashAndStatus,
).toHaveBeenCalledWith(ORDER_ID, 3);
).toHaveBeenCalledWith(ORDER_ID, 4);
expect(
transactionLoggingService.getLoggingByHashAndStatus,
).toHaveBeenCalledWith(ORDER_ID, 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ describe('Order Fulfilled Handler Event', () => {
dbioToDai: '1',
});

debioConversionServiceMock.getExchangeFromTo.mockReturnValue({
conversion: 1,
});

const orderCancelledCommand: OrderCreatedCommand = new OrderCreatedCommand(
[ORDER],
mockBlockNumber(),
Expand Down Expand Up @@ -240,9 +244,6 @@ describe('Order Fulfilled Handler Event', () => {
const convertToDbioUnitStringSpy = jest
.spyOn(globalProviderMethods, 'convertToDbioUnitString')
.mockImplementation();
const conversionFromTo = jest
.spyOn(debioConversionServiceMock, 'getExchangeFromTo')
.mockReturnValue({ conversion: 1 });
const DATE = new Date();
const ORDER = createMockOrder(OrderStatus.Cancelled, DATE);

Expand Down Expand Up @@ -309,6 +310,10 @@ describe('Order Fulfilled Handler Event', () => {
dbioToDai: '1',
});

debioConversionServiceMock.getExchangeFromTo.mockReturnValue({
conversion: 1,
});

const orderCancelledCommand: OrderCreatedCommand = new OrderCreatedCommand(
[ORDER],
mockBlockNumber(),
Expand Down Expand Up @@ -423,6 +428,10 @@ describe('Order Fulfilled Handler Event', () => {
dbioToDai: '1',
});

debioConversionServiceMock.getExchangeFromTo.mockReturnValue({
conversion: 1,
});

const orderCancelledCommand: OrderCreatedCommand = new OrderCreatedCommand(
[ORDER],
mockBlockNumber(),
Expand Down Expand Up @@ -565,6 +574,10 @@ describe('Order Fulfilled Handler Event', () => {
dbioToDai: '1',
});

debioConversionServiceMock.getExchangeFromTo.mockReturnValue({
conversion: 1,
});

const orderCancelledCommand: OrderCreatedCommand = new OrderCreatedCommand(
[ORDER],
mockBlockNumber(),
Expand Down

0 comments on commit ee8c28d

Please sign in to comment.