From b0ecc6ac0432cf88fece3fc333699e2207fb32f2 Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Thu, 28 Sep 2023 18:22:16 +0100 Subject: [PATCH 1/3] Change invoice amount from integer to varchar --- src/database/entities/Invoice.ts | 4 ++-- src/database/invoice.service.ts | 2 +- .../1695920334202-ChangeInvoiceAmountType.ts | 13 ++++++++++++ src/modules/payment/index.ts | 21 ++++++++++--------- src/modules/telegram_payment/index.ts | 6 +++--- 5 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts diff --git a/src/database/entities/Invoice.ts b/src/database/entities/Invoice.ts index be5c0ac7..df200671 100644 --- a/src/database/entities/Invoice.ts +++ b/src/database/entities/Invoice.ts @@ -17,8 +17,8 @@ export class Invoice { @Column() itemId!: string - @Column() - amount!: number + @Column({ type: 'varchar' }) + amount!: string @Column({ nullable: true }) telegramPaymentChargeId!: string diff --git a/src/database/invoice.service.ts b/src/database/invoice.service.ts index 1ad82424..35f1e606 100644 --- a/src/database/invoice.service.ts +++ b/src/database/invoice.service.ts @@ -6,7 +6,7 @@ const invoiceRepository = AppDataSource.getRepository(Invoice) export interface InvoiceParams { tgUserId: number accountId: number - amount: number + amount: string itemId: string currency?: string } diff --git a/src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts b/src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts new file mode 100644 index 00000000..5c4de7de --- /dev/null +++ b/src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts @@ -0,0 +1,13 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class ChangeInvoiceAmountType1695920334202 implements MigrationInterface { + name = 'ChangeInvoiceAmountType1695920334202' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE varchar'); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE integer'); + } +} diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index 4cd82a29..0612a843 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -4,13 +4,14 @@ import { type Account, type TransactionReceipt } from 'web3-core' import axios from 'axios' import bn, { BigNumber } from 'bignumber.js' import config from '../../config' -import { chatService, statsService } from '../../database/services' +import {chatService, invoiceService, statsService} from '../../database/services' import { type OnCallBackQueryData, type OnMessageContext } from '../types' import { LRUCache } from 'lru-cache' import { freeCreditsFeeCounter } from '../../metrics/prometheus' import { type BotPaymentLog } from '../../database/stats.service' import { sendMessage } from '../open-ai/helpers' import * as Sentry from '@sentry/node' +import {InvoiceParams} from "../../database/invoice.service"; interface CoinGeckoResponse { harmony: { @@ -64,17 +65,17 @@ export class BotPayments { userAccount: Account, amount: BigNumber ): Promise { - // const invoiceData: InvoiceParams = { - // tgUserId: accountId, - // accountId, - // amount: this.convertBigNumber(amount), - // itemId: 'deposit_one', - // currency: 'ONE' - // } - // const invoice = await invoiceService.create(invoiceData) + const invoiceData: InvoiceParams = { + tgUserId: accountId, + accountId, + amount: amount.toFixed(), + itemId: 'deposit_one', + currency: 'ONE' + } + const invoice = await invoiceService.create(invoiceData) await this.transferFunds(userAccount, this.holderAddress, amount) await chatService.depositOneCredits(accountId, amount.toFixed()) - // await invoiceService.setSuccessStatus({ uuid: invoice.uuid, providerPaymentChargeId: '', telegramPaymentChargeId: '' }) + await invoiceService.setSuccessStatus({ uuid: invoice.uuid, providerPaymentChargeId: '', telegramPaymentChargeId: '' }) } private async runHotWalletsTask (): Promise { diff --git a/src/modules/telegram_payment/index.ts b/src/modules/telegram_payment/index.ts index abfd2677..1012b9c5 100644 --- a/src/modules/telegram_payment/index.ts +++ b/src/modules/telegram_payment/index.ts @@ -65,11 +65,11 @@ export class TelegramPayments { const providerPaymentChargeId = provider_payment_charge_id const invoice = await invoiceService.setSuccessStatus({ uuid, telegramPaymentChargeId, providerPaymentChargeId }) - const fiatCredits = await this.payments.getPriceInONE(invoice.amount) + const fiatCredits = await this.payments.getPriceInONE(+invoice.amount) await chatService.depositFiatCredits(invoice.accountId, fiatCredits.toString()) - this.logger.info(`Payment from @${ctx.message.from.username} $${invoice.amount / 100} was completed!`) + this.logger.info(`Payment from @${ctx.message.from.username} $${+invoice.amount / 100} was completed!`) } public async createPaymentInvoice (ctx: OnMessageContext | OnCallBackQueryData): Promise { @@ -133,7 +133,7 @@ export class TelegramPayments { amount }] - const invoice = await invoiceService.create({ tgUserId, accountId, itemId, amount }) + const invoice = await invoiceService.create({ tgUserId, accountId, itemId, amount: amount.toString() }) const payload = JSON.stringify({ uuid: invoice.uuid }) this.logger.info(`Send invoice: ${JSON.stringify({ tgUserId, accountId, itemId, amount })}`) // const photoUrl = 'https://pbs.twimg.com/media/F5SofMsbgAApd2Y?format=png&name=small' From 8c96257d2f65e319fccee859bad7196f71c7978c Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Fri, 29 Sep 2023 17:05:20 +0100 Subject: [PATCH 2/3] Update last interacted users query --- src/database/stats.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/database/stats.service.ts b/src/database/stats.service.ts index 8b18c88b..5dba5954 100644 --- a/src/database/stats.service.ts +++ b/src/database/stats.service.ts @@ -129,6 +129,7 @@ export class StatsService { const queryBuilder = logRepository.createQueryBuilder('logs') .select('distinct(logs.accountId)') .where(`logs.createdAt >= TO_TIMESTAMP(${dateStart})`) + .orderBy('logs.createdAt desc') .limit(20) return await queryBuilder.execute() From 79c0e0d64cce43ee29058aac1f9f65f2ad08db61 Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Fri, 29 Sep 2023 17:16:20 +0100 Subject: [PATCH 3/3] Linter fixes --- .../1695920334202-ChangeInvoiceAmountType.ts | 16 ++++++++-------- src/modules/payment/index.ts | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts b/src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts index 5c4de7de..63b6afbe 100644 --- a/src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts +++ b/src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts @@ -1,13 +1,13 @@ -import { MigrationInterface, QueryRunner } from "typeorm" +import { type MigrationInterface, type QueryRunner } from 'typeorm' export class ChangeInvoiceAmountType1695920334202 implements MigrationInterface { - name = 'ChangeInvoiceAmountType1695920334202' + name = 'ChangeInvoiceAmountType1695920334202' - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE varchar'); - } + public async up (queryRunner: QueryRunner): Promise { + await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE varchar') + } - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE integer'); - } + public async down (queryRunner: QueryRunner): Promise { + await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE integer') + } } diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index 0612a843..45da040b 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -4,14 +4,14 @@ import { type Account, type TransactionReceipt } from 'web3-core' import axios from 'axios' import bn, { BigNumber } from 'bignumber.js' import config from '../../config' -import {chatService, invoiceService, statsService} from '../../database/services' +import { chatService, invoiceService, statsService } from '../../database/services' import { type OnCallBackQueryData, type OnMessageContext } from '../types' import { LRUCache } from 'lru-cache' import { freeCreditsFeeCounter } from '../../metrics/prometheus' import { type BotPaymentLog } from '../../database/stats.service' import { sendMessage } from '../open-ai/helpers' import * as Sentry from '@sentry/node' -import {InvoiceParams} from "../../database/invoice.service"; +import { type InvoiceParams } from '../../database/invoice.service' interface CoinGeckoResponse { harmony: {