refactor: the whole paymanet service

This commit is contained in:
mahyargdz
2025-05-03 16:30:22 +03:30
parent 4f54774400
commit 2e2154d745
30 changed files with 1231 additions and 396 deletions
@@ -45,3 +45,8 @@ export interface ISubscriptionNotificationData extends IBaseNotificationData {
planName: string;
invoiceId: string;
}
export interface IPaymentNotificationData extends IBaseNotificationData {
amount: number;
paymentId: string;
}
@@ -1,10 +1,10 @@
import { BadRequestException, Injectable, Logger, NotFoundException } from "@nestjs/common";
import { InjectDataSource } from "@nestjs/typeorm";
import { DataSource, QueryRunner } from "typeorm";
import { CommonMessage, NotificationMessage } from "../../../common/enums/message.enum";
import { NotifType } from "../../settings/enums/notif-settings.enum";
import { UserSettingsService } from "../../settings/providers/user-settings.service";
import { EmailService } from "../../utils/providers/email.service";
import { dateFormat, numberFormat } from "../../utils/providers/international.utils";
import { SmsService } from "../../utils/providers/sms.service";
import { CreateNotificationDto } from "../DTO/create-notification.dto";
@@ -14,6 +14,7 @@ import {
IAnnouncementNotificationData,
IBaseNotificationData,
IInvoiceNotificationData,
IPaymentNotificationData,
ISubscriptionNotificationData,
ITicketNotificationData,
IWalletNotificationData,
@@ -24,10 +25,11 @@ import { NotificationRepository } from "../repositories/notifications.repository
export class NotificationsService {
private readonly logger = new Logger(NotificationsService.name);
constructor(
private readonly dataSource: DataSource,
private readonly notificationRepository: NotificationRepository,
private readonly smsService: SmsService,
private readonly userSettingsService: UserSettingsService,
@InjectDataSource() private readonly dataSource: DataSource,
private readonly emailService: EmailService,
) {}
//************************ */
@@ -116,14 +118,14 @@ export class NotificationsService {
//************************ */
async createLoginNotification(recipientId: string, sendNotifData: IBaseNotificationData) {
async createLoginNotification(recipientId: string, data: IBaseNotificationData) {
const loginDate = new Date().toLocaleString("fa-IR");
const message = NotificationMessage.LOGIN_MESSAGE.replace("[loginDate]", loginDate);
//sent notification based on the userSettings
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.USER_LOGIN, recipientId);
if (isActive) {
await this.smsService.sendLoginSms(sendNotifData.userPhone, loginDate);
//send email too
await this.smsService.sendLoginSms(data.userPhone, loginDate);
if (data.userEmail) await this.emailService.sendLoginEmail(data);
}
return this.createNotification({ title: NotificationMessage.LOGIN, type: NotifType.USER_LOGIN, message, recipientId });
}
@@ -146,7 +148,7 @@ export class NotificationsService {
dateFormat(data.createDate),
dateFormat(data.dueDate),
);
//send email too
if (data.userEmail) await this.emailService.sendInvoiceEmail(data);
}
return this.createNotification(
{ title: NotificationMessage.INVOICE_CREATION, type: NotifType.CREATE_INVOICE, message, recipientId },
@@ -165,7 +167,7 @@ export class NotificationsService {
);
if (isActive) {
await this.smsService.sendAnnouncementSms(data.userPhone, data.title, data.description, dateFormat(data.date));
//send email too
if (data.userEmail) await this.emailService.sendAnnouncementEmail(data);
}
return this.createNotification({ title: NotificationMessage.ANNOUNCEMENT, type: NotifType.ANNOUNCEMENT, message, recipientId });
}
@@ -187,7 +189,7 @@ export class NotificationsService {
numberFormat(data.balance),
dateFormat(data.date),
);
//send email too
if (data.userEmail) await this.emailService.sendWalletEmail(data);
}
return this.createNotification({ title: NotificationMessage.WALLET_CHARGE, type: NotifType.WALLET_CHARGE, message, recipientId });
}
@@ -210,7 +212,7 @@ export class NotificationsService {
data.reason,
dateFormat(data.date),
);
//send email too
if (data.userEmail) await this.emailService.sendWalletEmail(data);
}
return this.createNotification({ title: NotificationMessage.WALLET_DEDUCTION, type: NotifType.WALLET_DEDUCTION, message, recipientId });
}
@@ -227,7 +229,7 @@ export class NotificationsService {
);
if (isActive) {
await this.smsService.sendAnswerTicketSms(data.userPhone, data.ticketId, data.subject);
//send email too
if (data.userEmail) await this.emailService.sendTicketEmail(data);
}
return this.createNotification({ title: NotificationMessage.ANSWER_TICKET, type: NotifType.ANSWER_TICKET, message, recipientId });
}
@@ -244,24 +246,23 @@ export class NotificationsService {
);
if (isActive) {
await this.smsService.sendCreateTicketSms(data.userPhone, data.ticketId, data.subject, dateFormat(data.date));
//send email too
if (data.userEmail) await this.emailService.sendTicketEmail(data);
}
return this.createNotification({ title: NotificationMessage.CREATE_TICKET, type: NotifType.CREATE_TICKET, message, recipientId });
}
//************************ */
//TODO: fix this cause this for admin
async createAssignTicketNotificationForAdmin(recipientId: string, data: ITicketNotificationData, queryRunner: QueryRunner) {
const message = NotificationMessage.ASSIGN_TICKET_MESSAGE.replace("[ticketId]", data.ticketId);
//sent notification based on the userSettings
// const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(
// NotifType.ANSWER_TICKET,
// recipientId,
// queryRunner,
// );
// if (isActive) {
await this.smsService.sendTicketAssignedToAdminSms(data.userPhone, data.ticketId, data.subject, data.user!);
//send email too
// }
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(
NotifType.ASSIGN_TICKET,
recipientId,
queryRunner,
);
if (isActive) {
await this.smsService.sendTicketAssignedToAdminSms(data.userPhone, data.ticketId, data.subject, data.user!);
if (data.userEmail) await this.emailService.sendTicketEmail(data);
}
return this.createNotification(
{ title: NotificationMessage.ASSIGN_TICKET, type: NotifType.ASSIGN_TICKET, message, recipientId },
queryRunner,
@@ -276,7 +277,7 @@ export class NotificationsService {
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.BILL_INVOICE_REMINDER, recipientId);
if (isActive) {
await this.smsService.sendInvoicePaymentReminderSms(data.userPhone, data.invoiceId, data.price, dateFormat(data.dueDate));
//send email too
if (data.userEmail) await this.emailService.sendInvoiceEmail(data);
}
return this.createNotification(
{ title: NotificationMessage.BILL_INVOICE_REMINDER, type: NotifType.BILL_INVOICE_REMINDER, message, recipientId },
@@ -292,7 +293,7 @@ export class NotificationsService {
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.BILL_INVOICE, recipientId);
if (isActive) {
await this.smsService.sendInvoicePaidSms(data.userPhone, data.invoiceId, data.price, dateFormat(data.paidAt!));
//send email too
if (data.userEmail) await this.emailService.sendInvoiceEmail(data);
}
return this.createNotification(
{ title: NotificationMessage.BILL_INVOICE, type: NotifType.BILL_INVOICE, message, recipientId },
@@ -307,7 +308,7 @@ export class NotificationsService {
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.APPROVE_INVOICE, recipientId);
if (isActive) {
await this.smsService.sendInvoiceApprovedSms(data.userPhone, data.invoiceId, data.price, dateFormat(data.dueDate));
//send email too
if (data.userEmail) await this.emailService.sendInvoiceEmail(data);
}
return this.createNotification(
{ title: NotificationMessage.APPROVED_INVOICE, type: NotifType.BILL_INVOICE, message, recipientId },
@@ -327,7 +328,7 @@ export class NotificationsService {
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.INVOICE_OVERDUE, recipientId);
if (isActive) {
await this.smsService.sendInvoiceOverdueSms(data.userPhone, data.invoiceId, data.price, data.lateFee || 0, dateFormat(data.dueDate));
//send email too
if (data.userEmail) await this.emailService.sendInvoiceEmail(data);
}
return this.createNotification(
{ title: NotificationMessage.INVOICE_OVERDUE, type: NotifType.INVOICE_OVERDUE, message, recipientId },
@@ -343,13 +344,14 @@ export class NotificationsService {
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.RECURRING_INVOICE, recipientId);
if (isActive) {
await this.smsService.sendRecurringInvoiceDraftSms(data.userPhone, data.invoiceId, data.price);
//send email too
if (data.userEmail) await this.emailService.sendInvoiceEmail(data);
}
return this.createNotification(
{ title: NotificationMessage.RECURRING_INVOICE, type: NotifType.RECURRING_INVOICE, message, recipientId },
queryRunner,
);
}
// //************************ */
async createBlockServiceNotification(recipientId: string, data: ISubscriptionNotificationData, queryRunner: QueryRunner) {
const message = NotificationMessage.BLOCK_SERVICE_MESSAGE.replace("[serviceName]", data.planName);
@@ -357,7 +359,14 @@ export class NotificationsService {
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.BLOCK_SERVICE, recipientId);
if (isActive) {
await this.smsService.sendBlockServiceSms(data.userPhone, data.invoiceId, data.planName);
//send email too
if (data.userEmail)
await this.emailService.sendAnnouncementEmail({
userEmail: data.userEmail,
userPhone: data.userPhone,
title: NotificationMessage.BLOCK_SERVICE,
description: message,
date: new Date(),
});
}
return this.createNotification(
{ title: NotificationMessage.BLOCK_SERVICE, type: NotifType.BLOCK_SERVICE, message, recipientId },
@@ -365,29 +374,56 @@ export class NotificationsService {
);
}
// async createServiceNotification(recipientId: string, sendNotifData: ISendNotificationData) {
// const message = NotificationMessage.CREATE_SERVICE_MESSAGE.replace("[serviceName]", sendNotifData.serviceName);
// //sent notification based on the userSettings
// const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.CREATE_SERVICE, recipientId);
// if (isActive) {
// await this.smsService.sendCreateServiceSms(sendNotifData.userPhone, sendNotifData.serviceName);
// //send email too
// }
// return this.createNotification({ title: NotificationMessage.CREATE_SERVICE, message, recipientId });
// }
// //************************ */
// async createUnblockServiceNotification(recipientId: string, sendNotifData: ISendNotificationData) {
// const message = NotificationMessage.UNBLOCK_SERVICE_MESSAGE.replace("[serviceName]", sendNotifData.serviceName);
// //sent notification based on the userSettings
// const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(NotifType.UNBLOCK_SERVICE, recipientId);
// if (isActive) {
// await this.smsService.sendUnblockServiceSms(sendNotifData.userPhone, sendNotifData.serviceName);
// //send email too
// }
// return this.createNotification({ title: NotificationMessage.UNBLOCK_SERVICE, message, recipientId });
// }
async createPaymentReminderNotification(recipientId: string, data: IPaymentNotificationData, queryRunner: QueryRunner) {
const message = NotificationMessage.PAYMENT_REMINDER_MESSAGE.replace("[amount]", numberFormat(data.amount));
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(
NotifType.PAYMENT_REMINDER,
recipientId,
queryRunner,
);
if (isActive) {
await this.smsService.sendPaymentReminderSms(data.userPhone, numberFormat(data.amount));
if (data.userEmail) await this.emailService.sendPaymentReminderEmail(data);
}
return this.createNotification(
{
title: NotificationMessage.PAYMENT_REMINDER,
type: NotifType.PAYMENT_REMINDER,
message,
recipientId,
},
queryRunner,
);
}
// //************************ */
async createPaymentCancellationNotification(recipientId: string, data: IPaymentNotificationData, queryRunner: QueryRunner) {
const message = NotificationMessage.PAYMENT_CANCELLATION_MESSAGE.replace("[amount]", numberFormat(data.amount));
const { isActive } = await this.userSettingsService.getUserNotificationSettingWithType(
NotifType.PAYMENT_CANCELLATION,
recipientId,
queryRunner,
);
if (isActive) {
await this.smsService.sendPaymentCancellationSms(data.userPhone, numberFormat(data.amount));
if (data.userEmail) await this.emailService.sendPaymentCancellationEmail(data);
}
return this.createNotification(
{
title: NotificationMessage.PAYMENT_CANCELLATION,
type: NotifType.PAYMENT_CANCELLATION,
message,
recipientId,
},
queryRunner,
);
}
}