refactor: the whole paymanet service
This commit is contained in:
@@ -2,7 +2,16 @@ import { Injectable, InternalServerErrorException, Logger } from "@nestjs/common
|
||||
import { MailerService } from "@nestjs-modules/mailer";
|
||||
import { SentMessageInfo } from "nodemailer";
|
||||
|
||||
import { dateFormat, numberFormat } from "./international.utils";
|
||||
import { EmailMessage } from "../../../common/enums/message.enum";
|
||||
import {
|
||||
IAnnouncementNotificationData,
|
||||
IInvoiceNotificationData,
|
||||
IPaymentNotificationData,
|
||||
ITicketNotificationData,
|
||||
IWalletNotificationData,
|
||||
} from "../../notifications/interfaces/ISendNotificationData";
|
||||
import { IBaseNotificationData } from "../../notifications/interfaces/ISendNotificationData";
|
||||
|
||||
@Injectable()
|
||||
export class EmailService {
|
||||
@@ -28,4 +37,120 @@ export class EmailService {
|
||||
throw new InternalServerErrorException(EmailMessage.EMAIL_SENDING_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
async sendLoginEmail(data: IBaseNotificationData) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: data.userEmail,
|
||||
subject: EmailMessage.LOGIN,
|
||||
template: "login",
|
||||
context: {
|
||||
date: dateFormat(new Date()),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending login email", error);
|
||||
}
|
||||
}
|
||||
|
||||
async sendInvoiceEmail(data: IInvoiceNotificationData) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: data.userEmail,
|
||||
subject: EmailMessage.INVOICE,
|
||||
template: "invoice",
|
||||
context: {
|
||||
price: numberFormat(data.price),
|
||||
createDate: dateFormat(data.createDate),
|
||||
dueDate: dateFormat(data.dueDate),
|
||||
invoiceId: data.invoiceId,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending invoice email", error);
|
||||
}
|
||||
}
|
||||
|
||||
async sendWalletEmail(data: IWalletNotificationData) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: data.userEmail,
|
||||
subject: EmailMessage.WALLET,
|
||||
template: "wallet",
|
||||
context: {
|
||||
amount: numberFormat(data.amount),
|
||||
balance: numberFormat(data.balance),
|
||||
date: dateFormat(data.date),
|
||||
reason: data.reason,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending wallet email", error);
|
||||
}
|
||||
}
|
||||
|
||||
async sendTicketEmail(data: ITicketNotificationData) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: data.userEmail,
|
||||
subject: EmailMessage.TICKET,
|
||||
template: "ticket",
|
||||
context: {
|
||||
ticketId: data.ticketId,
|
||||
subject: data.subject,
|
||||
date: data.date ? dateFormat(data.date) : dateFormat(new Date()),
|
||||
user: data.user,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending ticket email", error);
|
||||
}
|
||||
}
|
||||
|
||||
async sendAnnouncementEmail(data: IAnnouncementNotificationData) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: data.userEmail,
|
||||
subject: EmailMessage.ANNOUNCEMENT,
|
||||
template: "announcement",
|
||||
context: {
|
||||
title: data.title,
|
||||
description: data.description,
|
||||
date: dateFormat(data.date),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending announcement email", error);
|
||||
}
|
||||
}
|
||||
|
||||
async sendPaymentReminderEmail(data: IPaymentNotificationData) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: data.userEmail,
|
||||
subject: EmailMessage.PAYMENT_REMINDER,
|
||||
template: "payment-reminder",
|
||||
context: {
|
||||
amount: numberFormat(data.amount),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending payment reminder email", error);
|
||||
}
|
||||
}
|
||||
|
||||
async sendPaymentCancellationEmail(data: IPaymentNotificationData) {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: data.userEmail,
|
||||
subject: EmailMessage.PAYMENT_CANCELLATION,
|
||||
template: "payment-cancellation",
|
||||
context: {
|
||||
amount: numberFormat(data.amount),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending payment cancellation email", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -527,21 +527,60 @@ export class SmsService {
|
||||
}
|
||||
|
||||
//************************************************* */
|
||||
|
||||
// sendCreateServiceSms(userPhone: string, serviceName: any) {
|
||||
// throw new Error("Method not implemented.");
|
||||
// }
|
||||
//************************************************* */
|
||||
|
||||
// sendUnblockServiceSms(userPhone: string, serviceName: any) {
|
||||
// throw new Error("Method not implemented.");
|
||||
// }
|
||||
async sendPaymentReminderSms(mobile: string, amount: string) {
|
||||
const smsData: ISmsVerifyBody = {
|
||||
Parameters: [{ name: "amount", value: amount }],
|
||||
Mobile: mobile,
|
||||
TemplateId: this.smsConfigs.SMS_PATTERN_PAYMENT_REMINDER,
|
||||
};
|
||||
|
||||
try {
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService
|
||||
.post<ISmsVerifyResponse>(`${this.smsConfigs.API_URL}/send/verify`, smsData, {
|
||||
headers: { "X-API-KEY": this.smsConfigs.API_KEY },
|
||||
})
|
||||
.pipe(
|
||||
catchError((err: AxiosError) => {
|
||||
this.logger.error("error in sending payment reminder sms", err);
|
||||
throw new InternalServerErrorException("error in sending payment reminder sms");
|
||||
}),
|
||||
),
|
||||
);
|
||||
return data;
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending sms", error);
|
||||
}
|
||||
}
|
||||
//************************************************* */
|
||||
|
||||
// sendBlockServiceSms(userPhone: string, serviceName: any) {
|
||||
// throw new Error("Method not implemented.");
|
||||
// }
|
||||
//************************************************* */
|
||||
async sendPaymentCancellationSms(mobile: string, amount: string) {
|
||||
const smsData: ISmsVerifyBody = {
|
||||
Parameters: [{ name: "amount", value: amount }],
|
||||
Mobile: mobile,
|
||||
TemplateId: this.smsConfigs.SMS_PATTERN_PAYMENT_CANCELLATION,
|
||||
};
|
||||
|
||||
try {
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService
|
||||
.post<ISmsVerifyResponse>(`${this.smsConfigs.API_URL}/send/verify`, smsData, {
|
||||
headers: { "X-API-KEY": this.smsConfigs.API_KEY },
|
||||
})
|
||||
.pipe(
|
||||
catchError((err: AxiosError) => {
|
||||
this.logger.error("error in sending payment cancellation sms", err);
|
||||
throw new InternalServerErrorException("error in sending payment cancellation sms");
|
||||
}),
|
||||
),
|
||||
);
|
||||
return data;
|
||||
} catch (error) {
|
||||
this.logger.error("error in sending sms", error);
|
||||
}
|
||||
}
|
||||
|
||||
async getSmsLines() {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user