mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 07:10:33 +00:00
feat: send invoice notifications
This commit is contained in:
@@ -1,43 +0,0 @@
|
|||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { GetSaleInvoice } from './GetSaleInvoice';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { Tenant } from '@/system/models';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class SaleInvoiceMailFormatter {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getSaleInvoiceService: GetSaleInvoice;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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(
|
|
||||||
tenantId,
|
|
||||||
invoiceId
|
|
||||||
);
|
|
||||||
const organization = await Tenant.query()
|
|
||||||
.findById(tenantId)
|
|
||||||
.withGraphFetched('metadata');
|
|
||||||
|
|
||||||
return text
|
|
||||||
.replace('{CompanyName}', organization.metadata.name)
|
|
||||||
.replace('{CustomerName}', invoice.customer.displayName)
|
|
||||||
.replace('{InvoiceNumber}', invoice.invoiceNo)
|
|
||||||
.replace('{InvoiceDueAmount}', invoice.dueAmountFormatted)
|
|
||||||
.replace('{InvoiceDueDate}', invoice.dueDateFormatted)
|
|
||||||
.replace('{InvoiceDate}', invoice.invoiceDateFormatted)
|
|
||||||
.replace('{InvoiceAmount}', invoice.totalFormatted);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -4,31 +4,29 @@ import { SendInvoiceMailDTO } from '@/interfaces';
|
|||||||
import Mail from '@/lib/Mail';
|
import Mail from '@/lib/Mail';
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||||
import { SaleInvoicePdf } from './SaleInvoicePdf';
|
import { SaleInvoicePdf } from './SaleInvoicePdf';
|
||||||
import { SaleInvoiceMailFormatter } from './SaleInvoiceMailFormatter';
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_INVOICE_MAIL_CONTENT,
|
DEFAULT_INVOICE_MAIL_CONTENT,
|
||||||
DEFAULT_INVOICE_MAIL_SUBJECT,
|
DEFAULT_INVOICE_MAIL_SUBJECT,
|
||||||
ERRORS,
|
ERRORS,
|
||||||
} from './constants';
|
} from './constants';
|
||||||
import { CommandSaleInvoiceValidators } from './CommandSaleInvoiceValidators';
|
|
||||||
import { ServiceError } from '@/exceptions';
|
import { ServiceError } from '@/exceptions';
|
||||||
|
import { formatSmsMessage } from '@/utils';
|
||||||
|
import { GetSaleInvoice } from './GetSaleInvoice';
|
||||||
|
import { Tenant } from '@/system/models';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class SendSaleInvoiceMail {
|
export class SendSaleInvoiceMail {
|
||||||
@Inject()
|
@Inject()
|
||||||
private tenancy: HasTenancyService;
|
private tenancy: HasTenancyService;
|
||||||
|
|
||||||
@Inject('agenda')
|
@Inject()
|
||||||
private agenda: any;
|
private getSaleInvoiceService: GetSaleInvoice;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
private invoicePdf: SaleInvoicePdf;
|
private invoicePdf: SaleInvoicePdf;
|
||||||
|
|
||||||
@Inject()
|
@Inject('agenda')
|
||||||
private invoiceFormatter: SaleInvoiceMailFormatter;
|
private agenda: any;
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private commandInvoiceValidator: CommandSaleInvoiceValidators;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends the invoice mail of the given sale invoice.
|
* Sends the invoice mail of the given sale invoice.
|
||||||
@@ -49,6 +47,58 @@ export class SendSaleInvoiceMail {
|
|||||||
await this.agenda.now('sale-invoice-mail-send', payload);
|
await this.agenda.now('sale-invoice-mail-send', payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the default invoice mail options.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {number} invoiceId
|
||||||
|
* @returns {Promise<SendInvoiceMailDTO>}
|
||||||
|
*/
|
||||||
|
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(
|
||||||
|
tenantId,
|
||||||
|
invoiceId
|
||||||
|
);
|
||||||
|
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.
|
* Triggers the mail invoice.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
@@ -61,31 +111,20 @@ export class SendSaleInvoiceMail {
|
|||||||
saleInvoiceId: number,
|
saleInvoiceId: number,
|
||||||
messageDTO: SendInvoiceMailDTO
|
messageDTO: SendInvoiceMailDTO
|
||||||
) {
|
) {
|
||||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
const defaultMessageOpts = await this.getDefaultMailOpts(
|
||||||
|
tenantId,
|
||||||
const saleInvoice = await SaleInvoice.query()
|
saleInvoiceId
|
||||||
.findById(saleInvoiceId)
|
);
|
||||||
.withGraphFetched('customer');
|
|
||||||
|
|
||||||
this.commandInvoiceValidator.validateInvoiceExistance(saleInvoice);
|
|
||||||
|
|
||||||
// Parsed message opts with default options.
|
// Parsed message opts with default options.
|
||||||
const parsedMessageOpts = {
|
const parsedMessageOpts = {
|
||||||
attachInvoice: true,
|
...defaultMessageOpts,
|
||||||
subject: DEFAULT_INVOICE_MAIL_SUBJECT,
|
|
||||||
body: DEFAULT_INVOICE_MAIL_CONTENT,
|
|
||||||
to: saleInvoice.customer.email,
|
|
||||||
...messageDTO,
|
...messageDTO,
|
||||||
};
|
};
|
||||||
// In case there is no email address from the customer or from options, throw an error.
|
// In case there is no email address from the customer or from options, throw an error.
|
||||||
if (!parsedMessageOpts.to) {
|
if (!parsedMessageOpts.to) {
|
||||||
throw new ServiceError(ERRORS.NO_INVOICE_CUSTOMER_EMAIL_ADDR);
|
throw new ServiceError(ERRORS.NO_INVOICE_CUSTOMER_EMAIL_ADDR);
|
||||||
}
|
}
|
||||||
const formatter = R.curry(this.invoiceFormatter.formatText)(
|
const formatter = R.curry(this.textFormatter)(tenantId, saleInvoiceId);
|
||||||
tenantId,
|
|
||||||
saleInvoiceId
|
|
||||||
);
|
|
||||||
const toEmail = parsedMessageOpts.to;
|
|
||||||
const subject = await formatter(parsedMessageOpts.subject);
|
const subject = await formatter(parsedMessageOpts.subject);
|
||||||
const body = await formatter(parsedMessageOpts.body);
|
const body = await formatter(parsedMessageOpts.body);
|
||||||
const attachments = [];
|
const attachments = [];
|
||||||
@@ -96,17 +135,13 @@ export class SendSaleInvoiceMail {
|
|||||||
tenantId,
|
tenantId,
|
||||||
saleInvoiceId
|
saleInvoiceId
|
||||||
);
|
);
|
||||||
attachments.push({
|
attachments.push({ filename: 'invoice.pdf', content: invoicePdfBuffer });
|
||||||
filename: 'invoice.pdf',
|
|
||||||
content: invoicePdfBuffer,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
const mail = new Mail()
|
await new Mail()
|
||||||
.setSubject(subject)
|
.setSubject(subject)
|
||||||
.setTo(toEmail)
|
.setTo(parsedMessageOpts.to)
|
||||||
.setContent(body)
|
.setContent(body)
|
||||||
.setAttachments(attachments);
|
.setAttachments(attachments)
|
||||||
|
.send();
|
||||||
await mail.send();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import * as R from 'ramda';
|
import * as R from 'ramda';
|
||||||
import { assign } from 'lodash';
|
|
||||||
import { SendInvoiceMailDTO } from '@/interfaces';
|
import { SendInvoiceMailDTO } from '@/interfaces';
|
||||||
import Mail from '@/lib/Mail';
|
import Mail from '@/lib/Mail';
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||||
import { CommandSaleInvoiceValidators } from './CommandSaleInvoiceValidators';
|
|
||||||
import { SaleInvoiceMailFormatter } from './SaleInvoiceMailFormatter';
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_INVOICE_REMINDER_MAIL_CONTENT,
|
DEFAULT_INVOICE_REMINDER_MAIL_CONTENT,
|
||||||
DEFAULT_INVOICE_REMINDER_MAIL_SUBJECT,
|
DEFAULT_INVOICE_REMINDER_MAIL_SUBJECT,
|
||||||
@@ -13,6 +10,9 @@ import {
|
|||||||
} from './constants';
|
} from './constants';
|
||||||
import { SaleInvoicePdf } from './SaleInvoicePdf';
|
import { SaleInvoicePdf } from './SaleInvoicePdf';
|
||||||
import { ServiceError } from '@/exceptions';
|
import { ServiceError } from '@/exceptions';
|
||||||
|
import { GetSaleInvoice } from './GetSaleInvoice';
|
||||||
|
import { Tenant } from '@/system/models';
|
||||||
|
import { formatSmsMessage } from '@/utils';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class SendInvoiceMailReminder {
|
export class SendInvoiceMailReminder {
|
||||||
@@ -22,15 +22,12 @@ export class SendInvoiceMailReminder {
|
|||||||
@Inject('agenda')
|
@Inject('agenda')
|
||||||
private agenda: any;
|
private agenda: any;
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private commandInvoiceValidator: CommandSaleInvoiceValidators;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private invoiceFormatter: SaleInvoiceMailFormatter;
|
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
private invoicePdf: SaleInvoicePdf;
|
private invoicePdf: SaleInvoicePdf;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
private getSaleInvoiceService: GetSaleInvoice;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggers the reminder mail of the given sale invoice.
|
* Triggers the reminder mail of the given sale invoice.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
@@ -49,6 +46,59 @@ export class SendInvoiceMailReminder {
|
|||||||
await this.agenda.now('sale-invoice-reminder-mail-send', payload);
|
await this.agenda.now('sale-invoice-reminder-mail-send', payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the default message options.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {number} invoiceId
|
||||||
|
* @returns {Promise<SendInvoiceMailDTO>}
|
||||||
|
*/
|
||||||
|
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(
|
||||||
|
tenantId,
|
||||||
|
invoiceId
|
||||||
|
);
|
||||||
|
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.
|
* Triggers the mail invoice.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
@@ -61,31 +111,19 @@ export class SendInvoiceMailReminder {
|
|||||||
saleInvoiceId: number,
|
saleInvoiceId: number,
|
||||||
messageOptions: SendInvoiceMailDTO
|
messageOptions: SendInvoiceMailDTO
|
||||||
) {
|
) {
|
||||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
const defaultMessageOpts = await this.getDefaultMailOpts(
|
||||||
|
tenantId,
|
||||||
const saleInvoice = await SaleInvoice.query()
|
saleInvoiceId
|
||||||
.findById(saleInvoiceId)
|
);
|
||||||
.withGraphFetched('customer');
|
|
||||||
|
|
||||||
// Validates the invoice existance.
|
|
||||||
this.commandInvoiceValidator.validateInvoiceExistance(saleInvoice);
|
|
||||||
|
|
||||||
const parsedMessageOptions = {
|
const parsedMessageOptions = {
|
||||||
attachInvoice: true,
|
...defaultMessageOpts,
|
||||||
subject: DEFAULT_INVOICE_REMINDER_MAIL_SUBJECT,
|
|
||||||
body: DEFAULT_INVOICE_REMINDER_MAIL_CONTENT,
|
|
||||||
to: saleInvoice.customer.email,
|
|
||||||
...messageOptions,
|
...messageOptions,
|
||||||
};
|
};
|
||||||
// In case there is no email address from the customer or from options, throw an error.
|
// In case there is no email address from the customer or from options, throw an error.
|
||||||
if (!parsedMessageOptions.to) {
|
if (!parsedMessageOptions.to) {
|
||||||
throw new ServiceError(ERRORS.NO_INVOICE_CUSTOMER_EMAIL_ADDR);
|
throw new ServiceError(ERRORS.NO_INVOICE_CUSTOMER_EMAIL_ADDR);
|
||||||
}
|
}
|
||||||
const formatter = R.curry(this.invoiceFormatter.formatText)(
|
const formatter = R.curry(this.formatText)(tenantId, saleInvoiceId);
|
||||||
tenantId,
|
|
||||||
saleInvoiceId
|
|
||||||
);
|
|
||||||
const toEmail = parsedMessageOptions.to;
|
|
||||||
const subject = await formatter(parsedMessageOptions.subject);
|
const subject = await formatter(parsedMessageOptions.subject);
|
||||||
const body = await formatter(parsedMessageOptions.body);
|
const body = await formatter(parsedMessageOptions.body);
|
||||||
const attachments = [];
|
const attachments = [];
|
||||||
@@ -96,14 +134,11 @@ export class SendInvoiceMailReminder {
|
|||||||
tenantId,
|
tenantId,
|
||||||
saleInvoiceId
|
saleInvoiceId
|
||||||
);
|
);
|
||||||
attachments.push({
|
attachments.push({ filename: 'invoice.pdf', content: invoicePdfBuffer });
|
||||||
filename: 'invoice.pdf',
|
|
||||||
content: invoicePdfBuffer,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
const mail = new Mail()
|
const mail = new Mail()
|
||||||
.setSubject(subject)
|
.setSubject(subject)
|
||||||
.setTo(toEmail)
|
.setTo(parsedMessageOptions.to)
|
||||||
.setContent(body)
|
.setContent(body)
|
||||||
.setAttachments(attachments);
|
.setAttachments(attachments);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user