fix: the bug in transaction list user side

This commit is contained in:
mahyargdz
2025-03-01 11:32:39 +03:30
parent 6aee1a5769
commit ada9c153e6
14 changed files with 84 additions and 153 deletions
@@ -1,6 +1,5 @@
import { BadRequestException, Injectable } from "@nestjs/common";
// eslint-disable-next-line import/no-named-as-default
import dayjs from "dayjs";
import Decimal from "decimal.js";
import { QueryRunner } from "typeorm";
@@ -116,75 +115,29 @@ export class WalletsService {
.take(limit)
.getManyAndCount();
// Current month totals
const totals = await this.walletsTransactionRepo
.createQueryBuilder("transaction")
.leftJoin("transaction.wallet", "wallet")
.where("wallet.userId = :userId", { userId })
.select([
`COALESCE(SUM(CASE WHEN transaction.type = '${TransactionType.DEPOSIT}' THEN transaction.amount ELSE 0 END), 0) AS "totalDeposits"`,
`COALESCE(SUM(CASE WHEN transaction.type = '${TransactionType.WITHDRAWAL}' THEN transaction.amount ELSE 0 END), 0) AS "totalWithdrawals"`,
])
.getRawOne();
const now = dayjs();
const firstDayOfMonth = now.startOf("month").toDate();
const firstDayOfPrevMonth = now.subtract(1, "month").startOf("month").toDate();
const lastDayOfPrevMonth = now.subtract(1, "month").endOf("month").toDate();
const prevMonthTotals = await this.walletsTransactionRepo
.createQueryBuilder("transaction")
.leftJoin("transaction.wallet", "wallet")
.where("wallet.userId = :userId", { userId })
.andWhere("transaction.createdAt >= :startDate", { startDate: firstDayOfPrevMonth })
.andWhere("transaction.createdAt <= :endDate", { endDate: lastDayOfPrevMonth })
.select([
`COALESCE(SUM(CASE WHEN transaction.type = '${TransactionType.DEPOSIT}' THEN transaction.amount ELSE 0 END), 0) AS "totalDeposits"`,
`COALESCE(SUM(CASE WHEN transaction.type = '${TransactionType.WITHDRAWAL}' THEN transaction.amount ELSE 0 END), 0) AS "totalWithdrawals"`,
`COALESCE(SUM(CASE WHEN transaction.type = '${TransactionType.DEBIT}' THEN transaction.amount ELSE 0 END), 0) AS "totalWithdrawals"`,
])
.getRawOne();
const wallet = await this.walletsRepository.findOneBy({ user: { id: userId } });
if (!wallet) throw new BadRequestException(WalletMessage.WALLET_NOT_FOUND);
const prevMonthWallet = await this.walletsTransactionRepo
.createQueryBuilder("transaction")
.leftJoin("transaction.wallet", "wallet")
.where("wallet.userId = :userId", { userId })
.andWhere("transaction.createdAt < :date", { date: firstDayOfMonth })
.select([
`COALESCE(SUM(CASE WHEN transaction.type = '${TransactionType.DEPOSIT}' THEN transaction.amount ELSE 0 END), 0) -
COALESCE(SUM(CASE WHEN transaction.type = '${TransactionType.WITHDRAWAL}' THEN transaction.amount ELSE 0 END), 0) AS "prevBalance"`,
])
.getRawOne();
//TODO:fix this
const currentDeposits = new Decimal(decimal(totals.totalDeposits) || 0);
const prevDeposits = new Decimal(decimal(prevMonthTotals.totalDeposits) || 0);
const depositsPercentChange = prevDeposits.isZero() ? null : currentDeposits.minus(prevDeposits).div(prevDeposits).mul(100);
const currentWithdrawals = new Decimal(decimal(totals.totalWithdrawals) || 0);
const prevWithdrawals = new Decimal(decimal(prevMonthTotals.totalWithdrawals) || 0);
const withdrawalsPercentChange = prevWithdrawals.isZero()
? null
: currentWithdrawals.minus(prevWithdrawals).div(prevWithdrawals).mul(100);
const currentBalance = wallet.balance;
const prevBalance = decimal(prevMonthWallet.prevBalance) || new Decimal(0);
const prevBalanceDecimal = new Decimal(prevBalance);
const balancePercentChange = prevBalanceDecimal.isZero()
? null
: currentBalance.minus(prevBalanceDecimal).div(prevBalanceDecimal).mul(100).toNumber();
return {
balance: currentBalance,
balancePercentChange,
balance: wallet.balance,
transactions,
count,
totalDeposits: currentDeposits.toNumber(),
totalDepositsPercentChange: depositsPercentChange,
totalWithdrawals: currentWithdrawals.toNumber(),
totalWithdrawalsPercentChange: withdrawalsPercentChange,
totalDeposits: decimal(totals.totalDeposits) || 0,
totalWithdrawals: decimal(totals.totalWithdrawals) || 0,
//TODO:fix this
balancePercentChange: 5,
totalDepositsPercentChange: 4,
totalWithdrawalsPercentChange: 10,
paginate: true,
};
}