refactor: mail services to nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-01-13 16:06:55 +02:00
parent 72818759a5
commit 4ab20ac76a
62 changed files with 1635 additions and 1304 deletions

View File

@@ -2,6 +2,7 @@ import {
IPaymentReceivedCreateDTO,
IPaymentReceivedEditDTO,
IPaymentReceivedSmsDetails,
IPaymentsReceivedFilter,
// IPaymentsReceivedFilter,
// ISystemUser,
// PaymentReceiveMailOptsDTO,

View File

@@ -78,11 +78,10 @@ export class PaymentReceivesController {
@Get(':id/pdf')
public getPaymentReceivePdf(
@Param('id', ParseIntPipe) paymentReceiveId: number,
@Param('id', ParseIntPipe) paymentReceivedId: number,
) {
return this.paymentReceivesApplication.getPaymentReceivePdf(
1,
paymentReceiveId,
paymentReceivedId,
);
}
}

View File

@@ -26,6 +26,7 @@ import { PaymentReceivedSyncInvoicesSubscriber } from './subscribers/PaymentRece
import { PaymentReceivedInvoiceSync } from './commands/PaymentReceivedInvoiceSync.service';
import { LedgerModule } from '../Ledger/Ledger.module';
import { AccountsModule } from '../Accounts/Accounts.module';
import { SendPaymentReceiveMailNotification } from './commands/PaymentReceivedMailNotification';
@Module({
controllers: [PaymentReceivesController],
@@ -48,6 +49,7 @@ import { AccountsModule } from '../Accounts/Accounts.module';
PaymentReceivedAutoIncrementSubscriber,
PaymentReceivedGLEntriesSubscriber,
PaymentReceivedSyncInvoicesSubscriber,
SendPaymentReceiveMailNotification
],
exports: [PaymentReceivesApplication, CreatePaymentReceivedService],
imports: [

View File

@@ -1,162 +1,159 @@
// import { Inject, Injectable } from '@nestjs/common';
// import {
// PaymentReceiveMailOpts,
// PaymentReceiveMailOptsDTO,
// PaymentReceiveMailPresendEvent,
// SendInvoiceMailDTO,
// } from './types/PaymentReceived.types';
// import Mail from '@/lib/Mail';
// import {
// DEFAULT_PAYMENT_MAIL_CONTENT,
// DEFAULT_PAYMENT_MAIL_SUBJECT,
// } from './constants';
// import { GetPaymentReceived } from './queries/GetPaymentReceived.service';
// import { transformPaymentReceivedToMailDataArgs } from './utils';
// import { PaymentReceived } from './models/PaymentReceived';
// import { EventEmitter2 } from '@nestjs/event-emitter';
// import { events } from '@/common/events/events';
import { Inject, Injectable } from '@nestjs/common';
import {
DEFAULT_PAYMENT_MAIL_CONTENT,
DEFAULT_PAYMENT_MAIL_SUBJECT,
} from '../constants';
import { transformPaymentReceivedToMailDataArgs } from './utils';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
import { ContactMailNotification } from '@/modules/MailNotification/ContactMailNotification';
import { PaymentReceived } from '../models/PaymentReceived';
import { GetPaymentReceivedService } from '../queries/GetPaymentReceived.service';
import { mergeAndValidateMailOptions } from '@/modules/MailNotification/utils';
import { PaymentReceiveMailOptsDTO } from '../types/PaymentReceived.types';
import { PaymentReceiveMailOpts } from '../types/PaymentReceived.types';
import { PaymentReceiveMailPresendEvent } from '../types/PaymentReceived.types';
import { SendInvoiceMailDTO } from '@/modules/SaleInvoices/SaleInvoice.types';
// @Injectable()
// export class SendPaymentReceiveMailNotification {
// constructor(
// private getPaymentService: GetPaymentReceived,
// private contactMailNotification: ContactMailNotification,
@Injectable()
export class SendPaymentReceiveMailNotification {
constructor(
private getPaymentService: GetPaymentReceivedService,
private contactMailNotification: ContactMailNotification,
private eventEmitter: EventEmitter2,
// @Inject('agenda') private agenda: any,
// private eventPublisher: EventEmitter2,
@Inject(PaymentReceived.name)
private paymentReceiveModel: typeof PaymentReceived,
) {}
// @Inject(PaymentReceived.name)
// private paymentReceiveModel: typeof PaymentReceived,
// ) {}
/**
* Sends the mail of the given payment receive.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {PaymentReceiveMailOptsDTO} messageDTO
* @returns {Promise<void>}
*/
public async triggerMail(
paymentReceiveId: number,
messageDTO: PaymentReceiveMailOptsDTO,
): Promise<void> {
const payload = {
paymentReceiveId,
messageDTO,
};
// await this.agenda.now('payment-receive-mail-send', payload);
// /**
// * Sends the mail of the given payment receive.
// * @param {number} tenantId
// * @param {number} paymentReceiveId
// * @param {PaymentReceiveMailOptsDTO} messageDTO
// * @returns {Promise<void>}
// */
// public async triggerMail(
// paymentReceiveId: number,
// messageDTO: PaymentReceiveMailOptsDTO,
// ): Promise<void> {
// const payload = {
// paymentReceiveId,
// messageDTO,
// };
// await this.agenda.now('payment-receive-mail-send', payload);
// Triggers `onPaymentReceivePreMailSend` event.
await this.eventEmitter.emitAsync(events.paymentReceive.onPreMailSend, {
paymentReceiveId,
messageOptions: messageDTO,
} as PaymentReceiveMailPresendEvent);
}
// // Triggers `onPaymentReceivePreMailSend` event.
// await this.eventPublisher.emitAsync(events.paymentReceive.onPreMailSend, {
// paymentReceiveId,
// messageOptions: messageDTO,
// } as PaymentReceiveMailPresendEvent);
// }
/**
* Retrieves the default payment mail options.
* @param {number} paymentReceiveId - Payment receive id.
* @returns {Promise<PaymentReceiveMailOpts>}
*/
public getMailOptions = async (
paymentId: number,
): Promise<PaymentReceiveMailOpts> => {
const paymentReceive = await this.paymentReceiveModel
.query()
.findById(paymentId)
.throwIfNotFound();
// /**
// * Retrieves the default payment mail options.
// * @param {number} paymentReceiveId - Payment receive id.
// * @returns {Promise<PaymentReceiveMailOpts>}
// */
// public getMailOptions = async (
// paymentId: number,
// ): Promise<PaymentReceiveMailOpts> => {
// const paymentReceive = await this.paymentReceiveModel
// .query()
// .findById(paymentId)
// .throwIfNotFound();
const formatArgs = await this.textFormatter(paymentId);
// const formatArgs = await this.textFormatter(paymentId);
const mailOptions =
await this.contactMailNotification.getDefaultMailOptions(
paymentReceive.customerId,
);
return {
...mailOptions,
subject: DEFAULT_PAYMENT_MAIL_SUBJECT,
message: DEFAULT_PAYMENT_MAIL_CONTENT,
...formatArgs,
};
};
// const mailOptions =
// await this.contactMailNotification.getDefaultMailOptions(
// paymentReceive.customerId,
// );
// return {
// ...mailOptions,
// subject: DEFAULT_PAYMENT_MAIL_SUBJECT,
// message: DEFAULT_PAYMENT_MAIL_CONTENT,
// ...formatArgs,
// };
// };
/**
* Retrieves the formatted text of the given sale invoice.
* @param {number} invoiceId - Sale invoice id.
* @returns {Promise<Record<string, string>>}
*/
public textFormatter = async (
invoiceId: number,
): Promise<Record<string, string>> => {
const payment = await this.getPaymentService.getPaymentReceive(invoiceId);
return transformPaymentReceivedToMailDataArgs(payment);
};
// /**
// * Retrieves the formatted text of the given sale invoice.
// * @param {number} invoiceId - Sale invoice id.
// * @returns {Promise<Record<string, string>>}
// */
// public textFormatter = async (
// invoiceId: number,
// ): Promise<Record<string, string>> => {
// const payment = await this.getPaymentService.getPaymentReceive(invoiceId);
// return transformPaymentReceivedToMailDataArgs(payment);
// };
/**
* Retrieves the formatted mail options of the given payment receive.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {SendInvoiceMailDTO} messageDTO
* @returns {Promise<PaymentReceiveMailOpts>}
*/
public getFormattedMailOptions = async (
paymentReceiveId: number,
messageDTO: SendInvoiceMailDTO,
) => {
const formatterArgs = await this.textFormatter(paymentReceiveId);
// /**
// * Retrieves the formatted mail options of the given payment receive.
// * @param {number} tenantId
// * @param {number} paymentReceiveId
// * @param {SendInvoiceMailDTO} messageDTO
// * @returns {Promise<PaymentReceiveMailOpts>}
// */
// public getFormattedMailOptions = async (
// paymentReceiveId: number,
// messageDTO: SendInvoiceMailDTO,
// ) => {
// const formatterArgs = await this.textFormatter(paymentReceiveId);
// Default message options.
const defaultMessageOpts = await this.getMailOptions(paymentReceiveId);
// Parsed message opts with default options.
const parsedMessageOpts = mergeAndValidateMailOptions(
defaultMessageOpts,
messageDTO,
);
// Formats the message options.
return this.contactMailNotification.formatMailOptions(
parsedMessageOpts,
formatterArgs,
);
};
// // Default message options.
// const defaultMessageOpts = await this.getMailOptions(paymentReceiveId);
// // Parsed message opts with default options.
// const parsedMessageOpts = mergeAndValidateMailOptions(
// defaultMessageOpts,
// messageDTO,
// );
// // Formats the message options.
// return this.contactMailNotification.formatMailOptions(
// parsedMessageOpts,
// formatterArgs,
// );
// };
/**
* Triggers the mail invoice.
* @param {number} tenantId
* @param {number} saleInvoiceId - Invoice id.
* @param {SendInvoiceMailDTO} messageDTO - Message options.
* @returns {Promise<void>}
*/
public async sendMail(
paymentReceiveId: number,
messageDTO: PaymentReceiveMailOptsDTO,
): Promise<void> {
// Retrieves the formatted mail options.
const formattedMessageOptions = await this.getFormattedMailOptions(
paymentReceiveId,
messageDTO,
);
const mail = new Mail()
.setSubject(formattedMessageOptions.subject)
.setTo(formattedMessageOptions.to)
.setCC(formattedMessageOptions.cc)
.setBCC(formattedMessageOptions.bcc)
.setContent(formattedMessageOptions.message);
// /**
// * Triggers the mail invoice.
// * @param {number} tenantId
// * @param {number} saleInvoiceId - Invoice id.
// * @param {SendInvoiceMailDTO} messageDTO - Message options.
// * @returns {Promise<void>}
// */
// public async sendMail(
// paymentReceiveId: number,
// messageDTO: SendInvoiceMailDTO,
// ): Promise<void> {
// // Retrieves the formatted mail options.
// const formattedMessageOptions = await this.getFormattedMailOptions(
// paymentReceiveId,
// messageDTO,
// );
// const mail = new Mail()
// .setSubject(formattedMessageOptions.subject)
// .setTo(formattedMessageOptions.to)
// .setCC(formattedMessageOptions.cc)
// .setBCC(formattedMessageOptions.bcc)
// .setContent(formattedMessageOptions.message);
const eventPayload = {
paymentReceiveId,
messageOptions: formattedMessageOptions,
};
// Triggers `onPaymentReceiveMailSend` event.
await this.eventEmitter.emitAsync(
events.paymentReceive.onMailSend,
eventPayload,
);
await mail.send();
// const eventPayload = {
// paymentReceiveId,
// messageOptions: formattedMessageOptions,
// };
// // Triggers `onPaymentReceiveMailSend` event.
// await this.eventPublisher.emitAsync(
// events.paymentReceive.onMailSend,
// eventPayload,
// );
// await mail.send();
// // Triggers `onPaymentReceiveMailSent` event.
// await this.eventPublisher.emitAsync(
// events.paymentReceive.onMailSent,
// eventPayload,
// );
// }
// }
// Triggers `onPaymentReceiveMailSent` event.
await this.eventEmitter.emitAsync(
events.paymentReceive.onMailSent,
eventPayload,
);
}
}

View File

@@ -2,6 +2,7 @@ import { AttachmentLinkDTO } from '@/modules/Attachments/Attachments.types';
import { Knex } from 'knex';
import { PaymentReceived } from '../models/PaymentReceived';
import { IDynamicListFilter } from '@/modules/DynamicListing/DynamicFilter/DynamicFilter.types';
import { CommonMailOptions, CommonMailOptionsDTO } from '@/modules/MailNotification/MailNotification.types';
export interface IPaymentReceivedCreateDTO {
customerId: number;
@@ -144,15 +145,12 @@ export enum PaymentReceiveAction {
// | 'branchId'
// >;
// export interface PaymentReceiveMailOpts extends CommonMailOptions {}
// export interface PaymentReceiveMailOptsDTO extends CommonMailOptionsDTO {}
// export interface PaymentReceiveMailPresendEvent {
// tenantId: number;
// paymentReceiveId: number;
// messageOptions: PaymentReceiveMailOptsDTO;
// }
export interface PaymentReceiveMailOpts extends CommonMailOptions {}
export interface PaymentReceiveMailOptsDTO extends CommonMailOptionsDTO {}
export interface PaymentReceiveMailPresendEvent {
paymentReceiveId: number;
messageOptions: PaymentReceiveMailOptsDTO;
}
export interface PaymentReceivedPdfLineItem {
item: string;