feat: send invoices and reminder notifications to the customers

This commit is contained in:
Ahmed Bouhuolia
2023-12-21 22:57:17 +02:00
parent ef52eaf91a
commit d2c63878ed
10 changed files with 227 additions and 45 deletions

View File

@@ -1,9 +1,17 @@
import { Inject, Service } from 'typedi';
import { ISaleInvoiceNotifyPayload, SendInvoiceMailDTO } from '@/interfaces';
import * as R from 'ramda';
import { SendInvoiceMailDTO } from '@/interfaces';
import Mail from '@/lib/Mail';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import { SaleInvoicePdf } from './SaleInvoicePdf';
import { SaleInvoiceMailFormatter } from './SaleInvoiceMailFormatter';
import {
DEFAULT_INVOICE_MAIL_CONTENT,
DEFAULT_INVOICE_MAIL_SUBJECT,
ERRORS,
} from './constants';
import { CommandSaleInvoiceValidators } from './CommandSaleInvoiceValidators';
import { ServiceError } from '@/exceptions';
@Service()
export class SendSaleInvoiceMail {
@@ -13,13 +21,22 @@ export class SendSaleInvoiceMail {
@Inject('agenda')
private agenda: any;
@Inject()
private invoicePdf: SaleInvoicePdf;
@Inject()
private invoiceFormatter: SaleInvoiceMailFormatter;
@Inject()
private commandInvoiceValidator: CommandSaleInvoiceValidators;
/**
* Sends the invoice mail of the given sale invoice.
* @param {number} tenantId
* @param {number} saleInvoiceId
* @param {SendInvoiceMailDTO} messageDTO
*/
public async sendMail(
public async triggerMail(
tenantId: number,
saleInvoiceId: number,
messageDTO: SendInvoiceMailDTO
@@ -39,7 +56,7 @@ export class SendSaleInvoiceMail {
* @param {SendInvoiceMailDTO} messageDTO
* @returns {Promise<void>}
*/
public async triggerMail(
public async sendMail(
tenantId: number,
saleInvoiceId: number,
messageDTO: SendInvoiceMailDTO
@@ -50,17 +67,45 @@ export class SendSaleInvoiceMail {
.findById(saleInvoiceId)
.withGraphFetched('customer');
const toEmail = messageDTO.to || saleInvoice.customer.email;
const subject = messageDTO.subject || saleInvoice.invoiceNo;
this.commandInvoiceValidator.validateInvoiceExistance(saleInvoice);
if (!toEmail) {
return null;
// Parsed message opts with default options.
const parsedMessageOpts = {
attachInvoice: true,
subject: DEFAULT_INVOICE_MAIL_SUBJECT,
body: DEFAULT_INVOICE_MAIL_CONTENT,
to: saleInvoice.customer.email,
...messageDTO,
};
// In case there is no email address from the customer or from options, throw an error.
if (!parsedMessageOpts.to) {
throw new ServiceError(ERRORS.NO_INVOICE_CUSTOMER_EMAIL_ADDR);
}
const formatter = R.curry(this.invoiceFormatter.formatText)(
tenantId,
saleInvoiceId
);
const toEmail = parsedMessageOpts.to;
const subject = await formatter(parsedMessageOpts.subject);
const body = await formatter(parsedMessageOpts.body);
const attachments = [];
if (parsedMessageOpts.attachInvoice) {
// Retrieves document buffer of the invoice pdf document.
const invoicePdfBuffer = await this.invoicePdf.saleInvoicePdf(
tenantId,
saleInvoiceId
);
attachments.push({
filename: 'invoice.pdf',
content: invoicePdfBuffer,
});
}
const mail = new Mail()
.setSubject(subject)
.setView('mail/UserInvite.html')
.setTo(toEmail)
.setData({});
.setContent(body)
.setAttachments(attachments);
await mail.send();
}