update: add new field for subscription of user when purchased

This commit is contained in:
mahyargdz
2025-02-28 12:08:46 +03:30
parent 93c5ad49d3
commit 76feef7645
29 changed files with 220 additions and 302 deletions
@@ -1,5 +1,6 @@
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";
@@ -115,6 +116,7 @@ export class WalletsService {
.take(limit)
.getManyAndCount();
// Current month totals
const totals = await this.walletsTransactionRepo
.createQueryBuilder("transaction")
.leftJoin("transaction.wallet", "wallet")
@@ -125,14 +127,64 @@ export class WalletsService {
])
.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"`,
])
.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: wallet?.balance,
balance: currentBalance,
balancePercentChange,
transactions,
count,
totalDeposits: decimal(totals.totalDeposits) || 0,
totalWithdrawals: decimal(totals.totalWithdrawals) || 0,
totalDeposits: currentDeposits.toNumber(),
totalDepositsPercentChange: depositsPercentChange,
totalWithdrawals: currentWithdrawals.toNumber(),
totalWithdrawalsPercentChange: withdrawalsPercentChange,
paginate: true,
};
}