chore: add notification service for all user action

This commit is contained in:
mahyargdz
2025-02-28 19:43:53 +03:30
parent 5b1b574d41
commit e65334150c
19 changed files with 709 additions and 74 deletions
@@ -9,6 +9,7 @@ import { DataSource, Not, QueryRunner } from "typeorm";
import { PaginationDto } from "../../../common/DTO/pagination.dto";
import { CommonMessage, PaymentMessage, UserMessage, WalletMessage } from "../../../common/enums/message.enum";
import { NotificationsService } from "../../notifications/providers/notifications.service";
import { User } from "../../users/entities/user.entity";
import { PaginationUtils } from "../../utils/providers/pagination.utils";
import { WalletsService } from "../../wallets/providers/wallets.service";
@@ -37,6 +38,7 @@ import { GatewayType } from "../types/gateway.type";
export class PaymentsService {
constructor(
@InjectQueue(PAYMENT.PAYMENT_QUEUE_NAME) private readonly paymentQueue: Queue,
private readonly notificationsService: NotificationsService,
private readonly configService: ConfigService,
private readonly gatewayFactory: PaymentGatewayFactory,
private readonly paymentGatewaysRepository: PaymentGatewaysRepository,
@@ -159,12 +161,31 @@ export class PaymentsService {
const verifyData = await paymentGateway.verifyPayment({ reference: queryDto.Authority, amount: payment.amount });
if (verifyData.code === 100) {
await this.handleSuccessfulPayment(payment.id, payment.user.id, payment.amount, `${verifyData.ref_id}`, queryRunner);
const { wallet } = await this.handleSuccessfulPayment(
payment.id,
payment.user.id,
payment.amount,
`${verifyData.ref_id}`,
queryRunner,
);
await queryRunner.commitTransaction();
frontUrl.searchParams.append("status", PaymentStatus.COMPLETED);
frontUrl.searchParams.append("id", payment.id);
frontUrl.searchParams.append("date", payment.createdAt.toISOString());
frontUrl.searchParams.append("amount", payment.amount.toString());
//
await this.notificationsService.createWalletChargeNotification(
payment.user.id,
{
amount: new Decimal(payment.amount).toNumber(),
balance: new Decimal(wallet.balance).toNumber(),
date: payment.createdAt,
reason: WalletMessage.DEPOSIT_WALLET_IPG,
userPhone: payment.user.phone,
userEmail: payment.user.email,
},
queryRunner,
);
} else if (verifyData.code === 101) {
await queryRunner.commitTransaction();
frontUrl.searchParams.append("status", PaymentStatus.PENDING);
@@ -229,8 +250,7 @@ export class PaymentsService {
//*********************************** */
async handleSuccessfulPayment(paymentId: string, userId: string, amount: Decimal, transactionId: string, queryRunner: QueryRunner) {
await queryRunner.manager.update(Payment, { id: paymentId }, { status: PaymentStatus.COMPLETED, transactionId });
const transaction = await this.walletsService.createPaymentTransaction(userId, amount, queryRunner);
return transaction;
return this.walletsService.createPaymentTransaction(userId, amount, queryRunner);
}
//*********************************** */