mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
feat: send invoice through mail
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
|||||||
ISaleInvoiceCreateDTO,
|
ISaleInvoiceCreateDTO,
|
||||||
SaleInvoiceAction,
|
SaleInvoiceAction,
|
||||||
AbilitySubject,
|
AbilitySubject,
|
||||||
|
SendInvoiceMailDTO,
|
||||||
} from '@/interfaces';
|
} from '@/interfaces';
|
||||||
import CheckPolicies from '@/api/middleware/CheckPolicies';
|
import CheckPolicies from '@/api/middleware/CheckPolicies';
|
||||||
import { SaleInvoiceApplication } from '@/services/Sales/Invoices/SaleInvoicesApplication';
|
import { SaleInvoiceApplication } from '@/services/Sales/Invoices/SaleInvoicesApplication';
|
||||||
@@ -169,10 +170,10 @@ export default class SaleInvoicesController extends BaseController {
|
|||||||
'/:id/mail',
|
'/:id/mail',
|
||||||
[
|
[
|
||||||
...this.specificSaleInvoiceValidation,
|
...this.specificSaleInvoiceValidation,
|
||||||
body('from').isString().exists(),
|
body('from').isString().optional(),
|
||||||
body('to').isString().exists(),
|
body('to').isString().optional(),
|
||||||
body('body').isString().exists(),
|
body('body').isString().optional(),
|
||||||
body('attach_invoice').exists().isBoolean().toBoolean(),
|
body('attach_invoice').optional().isBoolean().toBoolean(),
|
||||||
],
|
],
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.sendSaleInvoiceMail.bind(this)),
|
asyncMiddleware(this.sendSaleInvoiceMail.bind(this)),
|
||||||
@@ -664,7 +665,7 @@ export default class SaleInvoicesController extends BaseController {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Sends mail invoice of the given sale invoice.
|
||||||
* @param {Request} req
|
* @param {Request} req
|
||||||
* @param {Response} res
|
* @param {Response} res
|
||||||
* @param {NextFunction} next
|
* @param {NextFunction} next
|
||||||
@@ -676,11 +677,13 @@ export default class SaleInvoicesController extends BaseController {
|
|||||||
) {
|
) {
|
||||||
const { tenantId } = req;
|
const { tenantId } = req;
|
||||||
const { id: invoiceId } = req.params;
|
const { id: invoiceId } = req.params;
|
||||||
|
const invoiceMailDTO: SendInvoiceMailDTO = this.matchedBodyData(req);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.saleInvoiceApplication.sendSaleInvoiceMail(
|
await this.saleInvoiceApplication.sendSaleInvoiceMail(
|
||||||
tenantId,
|
tenantId,
|
||||||
invoiceId
|
invoiceId,
|
||||||
|
invoiceMailDTO
|
||||||
);
|
);
|
||||||
return res.status(200).send({});
|
return res.status(200).send({});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -726,11 +729,13 @@ export default class SaleInvoicesController extends BaseController {
|
|||||||
) {
|
) {
|
||||||
const { tenantId } = req;
|
const { tenantId } = req;
|
||||||
const { id: invoiceId } = req.params;
|
const { id: invoiceId } = req.params;
|
||||||
|
const invoiceMailDTO: SendInvoiceMailDTO = this.matchedBodyData(req);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.saleInvoiceApplication.sendSaleInvoiceMailReminder(
|
await this.saleInvoiceApplication.sendSaleInvoiceMailReminder(
|
||||||
tenantId,
|
tenantId,
|
||||||
invoiceId
|
invoiceId,
|
||||||
|
invoiceMailDTO
|
||||||
);
|
);
|
||||||
return res.status(200).send({});
|
return res.status(200).send({});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -194,3 +194,9 @@ export interface SendInvoiceMailDTO {
|
|||||||
body: string;
|
body: string;
|
||||||
attachInvoice?: boolean;
|
attachInvoice?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ISaleInvoiceNotifyPayload {
|
||||||
|
tenantId: number;
|
||||||
|
saleInvoiceId: number;
|
||||||
|
messageDTO: SendInvoiceMailDTO;
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import RewriteInvoicesJournalEntries from 'jobs/WriteInvoicesJEntries';
|
|||||||
import UserInviteMailJob from 'jobs/UserInviteMail';
|
import UserInviteMailJob from 'jobs/UserInviteMail';
|
||||||
import OrganizationSetupJob from 'jobs/OrganizationSetup';
|
import OrganizationSetupJob from 'jobs/OrganizationSetup';
|
||||||
import OrganizationUpgrade from 'jobs/OrganizationUpgrade';
|
import OrganizationUpgrade from 'jobs/OrganizationUpgrade';
|
||||||
|
import { SendSaleInvoiceMailJob } from '@/services/Sales/Invoices/SendSaleInvoiceMailJob';
|
||||||
|
import { SendSaleInvoiceReminderMailJob } from '@/services/Sales/Invoices/SendSaleInvoiceMailReminderJob';
|
||||||
|
|
||||||
export default ({ agenda }: { agenda: Agenda }) => {
|
export default ({ agenda }: { agenda: Agenda }) => {
|
||||||
new ResetPasswordMailJob(agenda);
|
new ResetPasswordMailJob(agenda);
|
||||||
@@ -13,6 +15,8 @@ export default ({ agenda }: { agenda: Agenda }) => {
|
|||||||
new RewriteInvoicesJournalEntries(agenda);
|
new RewriteInvoicesJournalEntries(agenda);
|
||||||
new OrganizationSetupJob(agenda);
|
new OrganizationSetupJob(agenda);
|
||||||
new OrganizationUpgrade(agenda);
|
new OrganizationUpgrade(agenda);
|
||||||
|
new SendSaleInvoiceMailJob(agenda);
|
||||||
|
new SendSaleInvoiceReminderMailJob(agenda);
|
||||||
|
|
||||||
agenda.start();
|
agenda.start();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -312,15 +312,20 @@ export class SaleInvoiceApplication {
|
|||||||
* @param {number} saleInvoiceId
|
* @param {number} saleInvoiceId
|
||||||
* @returns {}
|
* @returns {}
|
||||||
*/
|
*/
|
||||||
public sendSaleInvoiceMailReminder(tenantId: number, saleInvoiceId: number) {
|
public sendSaleInvoiceMailReminder(
|
||||||
return this.sendInvoiceReminderService.sendInvoiceMailReminder(
|
tenantId: number,
|
||||||
|
saleInvoiceId: number,
|
||||||
|
messageDTO: SendInvoiceMailDTO
|
||||||
|
) {
|
||||||
|
return this.sendInvoiceReminderService.triggerMail(
|
||||||
tenantId,
|
tenantId,
|
||||||
saleInvoiceId
|
saleInvoiceId,
|
||||||
|
messageDTO
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Sends the invoice mail of the given sale invoice.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {number} saleInvoiceId
|
* @param {number} saleInvoiceId
|
||||||
* @param {SendInvoiceMailDTO} messageDTO
|
* @param {SendInvoiceMailDTO} messageDTO
|
||||||
@@ -331,7 +336,7 @@ export class SaleInvoiceApplication {
|
|||||||
saleInvoiceId: number,
|
saleInvoiceId: number,
|
||||||
messageDTO: SendInvoiceMailDTO
|
messageDTO: SendInvoiceMailDTO
|
||||||
) {
|
) {
|
||||||
return this.sendSaleInvoiceMailService.sendSaleInvoiceMail(
|
return this.sendSaleInvoiceMailService.sendMail(
|
||||||
tenantId,
|
tenantId,
|
||||||
saleInvoiceId,
|
saleInvoiceId,
|
||||||
messageDTO
|
messageDTO
|
||||||
|
|||||||
@@ -1,12 +1,67 @@
|
|||||||
import { Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import { SendInvoiceMailDTO } from '@/interfaces';
|
import { ISaleInvoiceNotifyPayload, 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';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class SendSaleInvoiceMail {
|
export class SendSaleInvoiceMail {
|
||||||
public sendSaleInvoiceMail(
|
@Inject()
|
||||||
|
private tenancy: HasTenancyService;
|
||||||
|
|
||||||
|
@Inject('agenda')
|
||||||
|
private agenda: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the invoice mail of the given sale invoice.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {number} saleInvoiceId
|
||||||
|
* @param {SendInvoiceMailDTO} messageDTO
|
||||||
|
*/
|
||||||
|
public async sendMail(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
saleInvoiceId: number,
|
saleInvoiceId: number,
|
||||||
messageDTO: SendInvoiceMailDTO
|
messageDTO: SendInvoiceMailDTO
|
||||||
) {}
|
) {
|
||||||
|
const payload = {
|
||||||
|
tenantId,
|
||||||
|
saleInvoiceId,
|
||||||
|
messageDTO,
|
||||||
|
};
|
||||||
|
await this.agenda.now('sale-invoice-mail-send', payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers the mail invoice.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {number} saleInvoiceId
|
||||||
|
* @param {SendInvoiceMailDTO} messageDTO
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
public async triggerMail(
|
||||||
|
tenantId: number,
|
||||||
|
saleInvoiceId: number,
|
||||||
|
messageDTO: SendInvoiceMailDTO
|
||||||
|
) {
|
||||||
|
const { SaleInvoice } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
const saleInvoice = await SaleInvoice.query()
|
||||||
|
.findById(saleInvoiceId)
|
||||||
|
.withGraphFetched('customer');
|
||||||
|
|
||||||
|
const toEmail = messageDTO.to || saleInvoice.customer.email;
|
||||||
|
const subject = messageDTO.subject || saleInvoice.invoiceNo;
|
||||||
|
|
||||||
|
if (!toEmail) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const mail = new Mail()
|
||||||
|
.setSubject(subject)
|
||||||
|
.setView('mail/UserInvite.html')
|
||||||
|
.setTo(toEmail)
|
||||||
|
.setData({});
|
||||||
|
|
||||||
|
await mail.send();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import Container, { Service } from 'typedi';
|
||||||
|
import events from '@/subscribers/events';
|
||||||
|
import { SendSaleInvoiceMail } from './SendSaleInvoiceMail';
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export class SendSaleInvoiceMailJob {
|
||||||
|
/**
|
||||||
|
* Constructor method.
|
||||||
|
*/
|
||||||
|
constructor(agenda) {
|
||||||
|
agenda.define(
|
||||||
|
'sale-invoice-mail-send',
|
||||||
|
{ priority: 'high', concurrency: 1 },
|
||||||
|
this.handler
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers sending invoice mail.
|
||||||
|
*/
|
||||||
|
private handler = async (job, done: Function) => {
|
||||||
|
const { tenantId, saleInvoiceId, messageDTO } = job.attrs.data;
|
||||||
|
const sendInvoiceMail = Container.get(SendSaleInvoiceMail);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sendInvoiceMail.triggerMail(tenantId, saleInvoiceId, messageDTO);
|
||||||
|
done();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
done(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,11 +1,64 @@
|
|||||||
import { Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
|
import { SendInvoiceMailDTO } from '@/interfaces';
|
||||||
|
import Mail from '@/lib/Mail';
|
||||||
|
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class SendInvoiceMailReminder {
|
export class SendInvoiceMailReminder {
|
||||||
|
@Inject()
|
||||||
|
private tenancy: HasTenancyService;
|
||||||
|
|
||||||
|
@Inject('agenda')
|
||||||
|
private agenda: any;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Triggers the reminder mail of the given sale invoice.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {number} saleInvoiceId
|
* @param {number} saleInvoiceId
|
||||||
*/
|
*/
|
||||||
public sendInvoiceMailReminder(tenantId: number, saleInvoiceId: number) {}
|
public async triggerMail(
|
||||||
|
tenantId: number,
|
||||||
|
saleInvoiceId: number,
|
||||||
|
messageDTO: SendInvoiceMailDTO
|
||||||
|
) {
|
||||||
|
const payload = {
|
||||||
|
tenantId,
|
||||||
|
saleInvoiceId,
|
||||||
|
messageDTO,
|
||||||
|
};
|
||||||
|
await this.agenda.now('sale-invoice-reminder-mail-send', payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers the mail invoice.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {number} saleInvoiceId
|
||||||
|
* @param {SendInvoiceMailDTO} messageDTO
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
public async sendMail(
|
||||||
|
tenantId: number,
|
||||||
|
saleInvoiceId: number,
|
||||||
|
messageDTO: SendInvoiceMailDTO
|
||||||
|
) {
|
||||||
|
const { SaleInvoice } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
const saleInvoice = await SaleInvoice.query()
|
||||||
|
.findById(saleInvoiceId)
|
||||||
|
.withGraphFetched('customer');
|
||||||
|
|
||||||
|
const toEmail = messageDTO.to || saleInvoice.customer.email;
|
||||||
|
const subject = messageDTO.subject || saleInvoice.invoiceNo;
|
||||||
|
|
||||||
|
if (!toEmail) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const mail = new Mail()
|
||||||
|
.setSubject(subject)
|
||||||
|
.setView('mail/UserInvite.html')
|
||||||
|
.setTo(toEmail)
|
||||||
|
.setData({});
|
||||||
|
|
||||||
|
await mail.send();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import Container, { Service } from 'typedi';
|
||||||
|
import { SendInvoiceMailReminder } from './SendSaleInvoiceMailReminder';
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export class SendSaleInvoiceReminderMailJob {
|
||||||
|
/**
|
||||||
|
* Constructor method.
|
||||||
|
*/
|
||||||
|
constructor(agenda) {
|
||||||
|
agenda.define(
|
||||||
|
'sale-invoice-reminder-mail-send',
|
||||||
|
{ priority: 'high', concurrency: 1 },
|
||||||
|
this.handler
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers sending invoice mail.
|
||||||
|
*/
|
||||||
|
private handler = async (job, done: Function) => {
|
||||||
|
const { tenantId, saleInvoiceId, messageDTO } = job.attrs.data;
|
||||||
|
const sendInvoiceMail = Container.get(SendInvoiceMailReminder);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sendInvoiceMail.sendMail(tenantId, saleInvoiceId, messageDTO);
|
||||||
|
done();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
done(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -129,6 +129,9 @@ export default {
|
|||||||
|
|
||||||
onNotifySms: 'onSaleInvoiceNotifySms',
|
onNotifySms: 'onSaleInvoiceNotifySms',
|
||||||
onNotifiedSms: 'onSaleInvoiceNotifiedSms',
|
onNotifiedSms: 'onSaleInvoiceNotifiedSms',
|
||||||
|
|
||||||
|
onNotifyMail: 'onSaleInvoiceNotifyMail',
|
||||||
|
onNotifyReminderMail: 'onSaleInvoiceNotifyReminderMail'
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -160,6 +163,8 @@ export default {
|
|||||||
|
|
||||||
onRejecting: 'onSaleEstimateRejecting',
|
onRejecting: 'onSaleEstimateRejecting',
|
||||||
onRejected: 'onSaleEstimateRejected',
|
onRejected: 'onSaleEstimateRejected',
|
||||||
|
|
||||||
|
onNotifyMail: 'onSaleEstimateNotifyMail'
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user