feat(server): contact mail notification service

This commit is contained in:
Ahmed Bouhuolia
2023-12-29 17:35:34 +02:00
parent 2a85fe2f3c
commit 0d15c16d40
17 changed files with 441 additions and 310 deletions

View File

@@ -300,10 +300,7 @@ export class SaleInvoiceApplication {
* @returns {}
*/
public getSaleInvoiceMailReminder(tenantId: number, saleInvoiceId: number) {
return this.getSaleInvoiceReminderService.getInvoiceMailReminder(
tenantId,
saleInvoiceId
);
return this.sendInvoiceReminderService.getMailOpts(tenantId, saleInvoiceId);
}
/**
@@ -345,14 +342,11 @@ export class SaleInvoiceApplication {
/**
* Retrieves the default mail options of the given sale invoice.
* @param {number} tenantId
* @param {number} saleInvoiceid
* @param {number} tenantId
* @param {number} saleInvoiceid
* @returns {Promise<SendInvoiceMailDTO>}
*/
public getSaleInvoiceMail(tenantId: number, saleInvoiceid: number) {
return this.sendInvoiceReminderService.getDefaultMailOpts(
tenantId,
saleInvoiceid
);
return this.sendSaleInvoiceMailService.getMailOpts(tenantId, saleInvoiceid);
}
}

View File

@@ -0,0 +1,115 @@
import { Inject, Service } from 'typedi';
import { isEmpty } from 'lodash';
import { SaleInvoiceMailOptions } from '@/interfaces';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import {
DEFAULT_INVOICE_MAIL_CONTENT,
DEFAULT_INVOICE_MAIL_SUBJECT,
} from './constants';
import { GetSaleInvoice } from './GetSaleInvoice';
import { Tenant } from '@/system/models';
import { ServiceError } from '@/exceptions';
import { ContactMailNotification } from '@/services/MailNotification/ContactMailNotification';
@Service()
export class SendSaleInvoiceMailCommon {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private getSaleInvoiceService: GetSaleInvoice;
@Inject()
private contactMailNotification: ContactMailNotification;
/**
* Retrieves the mail options.
* @param {number} tenantId - Tenant id.
* @param {number} invoiceId - Invoice id.
* @param {string} defaultSubject - Subject text.
* @param {string} defaultBody - Subject body.
* @returns {}
*/
public async getMailOpts(
tenantId: number,
invoiceId: number,
defaultSubject: string = DEFAULT_INVOICE_MAIL_SUBJECT,
defaultBody: string = DEFAULT_INVOICE_MAIL_CONTENT
): Promise<SaleInvoiceMailOptions> {
const { SaleInvoice } = this.tenancy.models(tenantId);
const saleInvoice = await SaleInvoice.query()
.findById(invoiceId)
.throwIfNotFound();
const formatterData = await this.formatText(tenantId, invoiceId);
return this.contactMailNotification.getMailOptions(
tenantId,
saleInvoice.customerId,
defaultSubject,
defaultBody,
formatterData
);
}
/**
* Retrieves the formatted text of the given sale invoice.
* @param {number} tenantId - Tenant id.
* @param {number} invoiceId - Sale invoice id.
* @param {string} text - The given text.
* @returns {Promise<string>}
*/
public formatText = async (
tenantId: number,
invoiceId: number
): Promise<Record<string, string | number>> => {
const invoice = await this.getSaleInvoiceService.getSaleInvoice(
tenantId,
invoiceId
);
const organization = await Tenant.query()
.findById(tenantId)
.withGraphFetched('metadata');
return {
CompanyName: organization.metadata.name,
CustomerName: invoice.customer.displayName,
InvoiceNumber: invoice.invoiceNo,
InvoiceDueAmount: invoice.dueAmountFormatted,
InvoiceDueDate: invoice.dueDateFormatted,
InvoiceDate: invoice.invoiceDateFormatted,
InvoiceAmount: invoice.totalFormatted,
OverdueDays: invoice.overdueDays,
};
};
/**
* Validates the mail notification options before sending it.
* @param {Partial<SaleInvoiceMailOptions>} mailNotificationOpts
* @throws {ServiceError}
*/
public validateMailNotification(
mailNotificationOpts: Partial<SaleInvoiceMailOptions>
) {
if (isEmpty(mailNotificationOpts.from)) {
throw new ServiceError(ERRORS.MAIL_FROM_NOT_FOUND);
}
if (isEmpty(mailNotificationOpts.to)) {
throw new ServiceError(ERRORS.MAIL_TO_NOT_FOUND);
}
if (isEmpty(mailNotificationOpts.subject)) {
throw new ServiceError(ERRORS.MAIL_SUBJECT_NOT_FOUND);
}
if (isEmpty(mailNotificationOpts.body)) {
throw new ServiceError(ERRORS.MAIL_BODY_NOT_FOUND);
}
}
}
const ERRORS = {
MAIL_FROM_NOT_FOUND: 'Mail from address not found',
MAIL_TO_NOT_FOUND: 'Mail to address not found',
MAIL_SUBJECT_NOT_FOUND: 'Mail subject not found',
MAIL_BODY_NOT_FOUND: 'Mail body not found',
};

View File

@@ -1,29 +1,20 @@
import { Inject, Service } from 'typedi';
import * as R from 'ramda';
import { SendInvoiceMailDTO } from '@/interfaces';
import Mail from '@/lib/Mail';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { SendInvoiceMailDTO } from '@/interfaces';
import { SaleInvoicePdf } from './SaleInvoicePdf';
import { SendSaleInvoiceMailCommon } from './SendInvoiceInvoiceMailCommon';
import {
DEFAULT_INVOICE_MAIL_CONTENT,
DEFAULT_INVOICE_MAIL_SUBJECT,
ERRORS,
} from './constants';
import { ServiceError } from '@/exceptions';
import { formatSmsMessage } from '@/utils';
import { GetSaleInvoice } from './GetSaleInvoice';
import { Tenant } from '@/system/models';
@Service()
export class SendSaleInvoiceMail {
@Inject()
private tenancy: HasTenancyService;
private invoicePdf: SaleInvoicePdf;
@Inject()
private getSaleInvoiceService: GetSaleInvoice;
@Inject()
private invoicePdf: SaleInvoicePdf;
private invoiceMail: SendSaleInvoiceMailCommon;
@Inject('agenda')
private agenda: any;
@@ -48,56 +39,19 @@ export class SendSaleInvoiceMail {
}
/**
* Retrieves the default invoice mail options.
* Retrieves the mail options of the given sale invoice.
* @param {number} tenantId
* @param {number} invoiceId
* @returns {Promise<SendInvoiceMailDTO>}
* @param {number} saleInvoiceId
* @returns {Promise<SaleInvoiceMailOptions>}
*/
public getDefaultMailOpts = async (tenantId: number, invoiceId: number) => {
const { SaleInvoice } = this.tenancy.models(tenantId);
const saleInvoice = await SaleInvoice.query()
.findById(invoiceId)
.withGraphFetched('customer')
.throwIfNotFound();
return {
attachInvoice: true,
subject: DEFAULT_INVOICE_MAIL_SUBJECT,
body: DEFAULT_INVOICE_MAIL_CONTENT,
to: saleInvoice.customer.email,
};
};
/**
* Retrieves the formatted text of the given sale invoice.
* @param {number} tenantId - Tenant id.
* @param {number} invoiceId - Sale invoice id.
* @param {string} text - The given text.
* @returns {Promise<string>}
*/
public textFormatter = async (
tenantId: number,
invoiceId: number,
text: string
): Promise<string> => {
const invoice = await this.getSaleInvoiceService.getSaleInvoice(
public async getMailOpts(tenantId: number, saleInvoiceId: number) {
return this.invoiceMail.getMailOpts(
tenantId,
invoiceId
saleInvoiceId,
DEFAULT_INVOICE_MAIL_SUBJECT,
DEFAULT_INVOICE_MAIL_CONTENT
);
const organization = await Tenant.query()
.findById(tenantId)
.withGraphFetched('metadata');
return formatSmsMessage(text, {
CompanyName: organization.metadata.name,
CustomerName: invoice.customer.displayName,
InvoiceNumber: invoice.invoiceNo,
InvoiceDueAmount: invoice.dueAmountFormatted,
InvoiceDueDate: invoice.dueDateFormatted,
InvoiceDate: invoice.invoiceDateFormatted,
InvoiceAmount: invoice.totalFormatted,
});
};
}
/**
* Triggers the mail invoice.
@@ -111,37 +65,30 @@ export class SendSaleInvoiceMail {
saleInvoiceId: number,
messageDTO: SendInvoiceMailDTO
) {
const defaultMessageOpts = await this.getDefaultMailOpts(
tenantId,
saleInvoiceId
);
const defaultMessageOpts = await this.getMailOpts(tenantId, saleInvoiceId);
// Parsed message opts with default options.
const parsedMessageOpts = {
const messageOpts = {
...defaultMessageOpts,
...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.textFormatter)(tenantId, saleInvoiceId);
const subject = await formatter(parsedMessageOpts.subject);
const body = await formatter(parsedMessageOpts.body);
const attachments = [];
this.invoiceMail.validateMailNotification(messageOpts);
if (parsedMessageOpts.attachInvoice) {
const mail = new Mail()
.setSubject(messageOpts.subject)
.setTo(messageOpts.to)
.setContent(messageOpts.body);
if (messageOpts.attachInvoice) {
// Retrieves document buffer of the invoice pdf document.
const invoicePdfBuffer = await this.invoicePdf.saleInvoicePdf(
tenantId,
saleInvoiceId
);
attachments.push({ filename: 'invoice.pdf', content: invoicePdfBuffer });
mail.setAttachments([
{ filename: 'invoice.pdf', content: invoicePdfBuffer },
]);
}
await new Mail()
.setSubject(subject)
.setTo(parsedMessageOpts.to)
.setContent(body)
.setAttachments(attachments)
.send();
await mail.send();
}
}

View File

@@ -1,24 +1,15 @@
import { Inject, Service } from 'typedi';
import * as R from 'ramda';
import { SendInvoiceMailDTO } from '@/interfaces';
import Mail from '@/lib/Mail';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { SaleInvoicePdf } from './SaleInvoicePdf';
import { SendSaleInvoiceMailCommon } from './SendInvoiceInvoiceMailCommon';
import {
DEFAULT_INVOICE_REMINDER_MAIL_CONTENT,
DEFAULT_INVOICE_REMINDER_MAIL_SUBJECT,
ERRORS,
} from './constants';
import { SaleInvoicePdf } from './SaleInvoicePdf';
import { ServiceError } from '@/exceptions';
import { GetSaleInvoice } from './GetSaleInvoice';
import { Tenant } from '@/system/models';
import { formatSmsMessage } from '@/utils';
@Service()
export class SendInvoiceMailReminder {
@Inject()
private tenancy: HasTenancyService;
@Inject('agenda')
private agenda: any;
@@ -26,7 +17,7 @@ export class SendInvoiceMailReminder {
private invoicePdf: SaleInvoicePdf;
@Inject()
private getSaleInvoiceService: GetSaleInvoice;
private invoiceCommonMail: SendSaleInvoiceMailCommon;
/**
* Triggers the reminder mail of the given sale invoice.
@@ -47,57 +38,19 @@ export class SendInvoiceMailReminder {
}
/**
* Parses the default message options.
* Retrieves the mail options of the given sale invoice.
* @param {number} tenantId
* @param {number} invoiceId
* @returns {Promise<SendInvoiceMailDTO>}
* @param {number} saleInvoiceId
* @returns {Promise<SaleInvoiceMailOptions>}
*/
public async getDefaultMailOpts(tenantId: number, invoiceId: number) {
const { SaleInvoice } = this.tenancy.models(tenantId);
const saleInvoice = await SaleInvoice.query()
.findById(invoiceId)
.withGraphFetched('customer')
.throwIfNotFound();
return {
attachInvoice: true,
subject: DEFAULT_INVOICE_REMINDER_MAIL_SUBJECT,
body: DEFAULT_INVOICE_REMINDER_MAIL_CONTENT,
to: saleInvoice.customer.email,
};
}
/**
* Retrieves the formatted text of the given sale invoice.
* @param {number} tenantId - Tenant id.
* @param {number} invoiceId - Sale invoice id.
* @param {string} text - The given text.
* @returns {Promise<string>}
*/
public formatText = async (
tenantId: number,
invoiceId: number,
text: string
): Promise<string> => {
const invoice = await this.getSaleInvoiceService.getSaleInvoice(
public async getMailOpts(tenantId: number, saleInvoiceId: number) {
return this.invoiceCommonMail.getMailOpts(
tenantId,
invoiceId
saleInvoiceId,
DEFAULT_INVOICE_REMINDER_MAIL_SUBJECT,
DEFAULT_INVOICE_REMINDER_MAIL_CONTENT
);
const organization = await Tenant.query()
.findById(tenantId)
.withGraphFetched('metadata');
return formatSmsMessage(text, {
CompanyName: organization.metadata.name,
CustomerName: invoice.customer.displayName,
InvoiceNumber: invoice.invoiceNo,
InvoiceDueAmount: invoice.dueAmountFormatted,
InvoiceDueDate: invoice.dueDateFormatted,
InvoiceDate: invoice.invoiceDateFormatted,
InvoiceAmount: invoice.totalFormatted,
});
};
}
/**
* Triggers the mail invoice.
@@ -111,37 +64,27 @@ export class SendInvoiceMailReminder {
saleInvoiceId: number,
messageOptions: SendInvoiceMailDTO
) {
const defaultMessageOpts = await this.getDefaultMailOpts(
tenantId,
saleInvoiceId
);
const parsedMessageOptions = {
...defaultMessageOpts,
const localMessageOpts = await this.getMailOpts(tenantId, saleInvoiceId);
const messageOpts = {
...localMessageOpts,
...messageOptions,
};
// In case there is no email address from the customer or from options, throw an error.
if (!parsedMessageOptions.to) {
throw new ServiceError(ERRORS.NO_INVOICE_CUSTOMER_EMAIL_ADDR);
}
const formatter = R.curry(this.formatText)(tenantId, saleInvoiceId);
const subject = await formatter(parsedMessageOptions.subject);
const body = await formatter(parsedMessageOptions.body);
const attachments = [];
const mail = new Mail()
.setSubject(messageOpts.subject)
.setTo(messageOpts.to)
.setContent(messageOpts.body);
if (parsedMessageOptions.attachInvoice) {
if (messageOpts.attachInvoice) {
// Retrieves document buffer of the invoice pdf document.
const invoicePdfBuffer = await this.invoicePdf.saleInvoicePdf(
tenantId,
saleInvoiceId
);
attachments.push({ filename: 'invoice.pdf', content: invoicePdfBuffer });
mail.setAttachments([
{ filename: 'invoice.pdf', content: invoicePdfBuffer },
]);
}
const mail = new Mail()
.setSubject(subject)
.setTo(parsedMessageOptions.to)
.setContent(body)
.setAttachments(attachments);
await mail.send();
}
}

View File

@@ -8,6 +8,11 @@ Invoice <strong>#{InvoiceNumber}</strong><br />
Due Date : <strong>{InvoiceDueDate}</strong><br />
Amount : <strong>{InvoiceAmount}</strong></br />
</p>
<p>
<i>Regards</i><br />
<i>{CompanyName}</i>
</p>
`;
export const DEFAULT_INVOICE_REMINDER_MAIL_SUBJECT =
@@ -18,6 +23,11 @@ export const DEFAULT_INVOICE_REMINDER_MAIL_CONTENT = `
<p>Invoice <strong>#{InvoiceNumber}</strong><br />
Due Date : <strong>{InvoiceDueDate}</strong><br />
Amount : <strong>{InvoiceAmount}</strong></p>
<p>
<i>Regards</i><br />
<i>{CompanyName}</i>
</p>
`;
export const ERRORS = {