-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wip send invoice mail payment template
- Loading branch information
Showing
15 changed files
with
396 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,46 @@ | ||
import { isEmpty } from 'lodash'; | ||
import { castArray, isEmpty } from 'lodash'; | ||
import { ServiceError } from '@/exceptions'; | ||
import { CommonMailOptions, CommonMailOptionsDTO } from '@/interfaces'; | ||
import { CommonMailOptions } from '@/interfaces'; | ||
import { ERRORS } from './constants'; | ||
|
||
/** | ||
* Merges the mail options with incoming options. | ||
* @param {Partial<SaleInvoiceMailOptions>} mailOptions | ||
* @param {Partial<SendInvoiceMailDTO>} overridedOptions | ||
* @throws {ServiceError} | ||
*/ | ||
export function parseAndValidateMailOptions( | ||
mailOptions: Partial<CommonMailOptions>, | ||
overridedOptions: Partial<CommonMailOptionsDTO> | ||
) { | ||
export function parseMailOptions( | ||
mailOptions: CommonMailOptions, | ||
overridedOptions: Partial<CommonMailOptions> | ||
): CommonMailOptions { | ||
const mergedMessageOptions = { | ||
...mailOptions, | ||
...overridedOptions, | ||
}; | ||
if (isEmpty(mergedMessageOptions.from)) { | ||
const parsedMessageOptions = { | ||
...mergedMessageOptions, | ||
from: mergedMessageOptions?.from | ||
? castArray(mergedMessageOptions?.from) | ||
: [], | ||
to: mergedMessageOptions?.to ? castArray(mergedMessageOptions?.to) : [], | ||
cc: mergedMessageOptions?.cc ? castArray(mergedMessageOptions?.cc) : [], | ||
bcc: mergedMessageOptions?.bcc ? castArray(mergedMessageOptions?.bcc) : [], | ||
}; | ||
return parsedMessageOptions; | ||
} | ||
|
||
export function validateRequiredMailOptions( | ||
mailOptions: Partial<CommonMailOptions> | ||
) { | ||
if (isEmpty(mailOptions.from)) { | ||
throw new ServiceError(ERRORS.MAIL_FROM_NOT_FOUND); | ||
} | ||
if (isEmpty(mergedMessageOptions.to)) { | ||
if (isEmpty(mailOptions.to)) { | ||
throw new ServiceError(ERRORS.MAIL_TO_NOT_FOUND); | ||
} | ||
if (isEmpty(mergedMessageOptions.subject)) { | ||
if (isEmpty(mailOptions.subject)) { | ||
throw new ServiceError(ERRORS.MAIL_SUBJECT_NOT_FOUND); | ||
} | ||
if (isEmpty(mergedMessageOptions.body)) { | ||
if (isEmpty(mailOptions.message)) { | ||
throw new ServiceError(ERRORS.MAIL_BODY_NOT_FOUND); | ||
} | ||
return mergedMessageOptions; | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/server/src/services/Sales/Invoices/GetInvoicePaymentMail.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Inject, Service } from 'typedi'; | ||
import { GetPdfTemplate } from '@/services/PdfTemplate/GetPdfTemplate'; | ||
import { GetSaleInvoice } from './GetSaleInvoice'; | ||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable'; | ||
import { | ||
InvoicePaymentEmailProps, | ||
renderInvoicePaymentEmail, | ||
} from '@bigcapital/email-components'; | ||
import { GetInvoiceMailTemplateAttributesTransformer } from './GetInvoicePaymentMailAttributesTransformer'; | ||
|
||
@Service() | ||
export class GetInvoicePaymentMail { | ||
@Inject() | ||
private getSaleInvoiceService: GetSaleInvoice; | ||
|
||
@Inject() | ||
private getBrandingTemplate: GetPdfTemplate; | ||
|
||
@Inject() | ||
private transformer: TransformerInjectable; | ||
|
||
/** | ||
* Retrieves the mail template attributes of the given invoice. | ||
* @param {number} tenantId - Tenant id. | ||
* @param {number} invoiceId - Invoice id. | ||
*/ | ||
public async getMailTemplateAttributes(tenantId: number, invoiceId: number) { | ||
const invoice = await this.getSaleInvoiceService.getSaleInvoice( | ||
tenantId, | ||
invoiceId | ||
); | ||
const brandingTemplate = await this.getBrandingTemplate.getPdfTemplate( | ||
tenantId, | ||
invoice.pdfTemplateId | ||
); | ||
const mailTemplateAttributes = await this.transformer.transform( | ||
tenantId, | ||
invoice, | ||
new GetInvoiceMailTemplateAttributesTransformer(), | ||
{ | ||
invoice, | ||
brandingTemplate, | ||
} | ||
); | ||
return mailTemplateAttributes; | ||
} | ||
|
||
/** | ||
* Retrieves the mail template html content. | ||
* @param {number} tenantId - Tenant id. | ||
* @param {number} invoiceId - Invoice id. | ||
*/ | ||
public async getMailTemplate( | ||
tenantId: number, | ||
invoiceId: number, | ||
overrideAttributes?: Partial<InvoicePaymentEmailProps> | ||
): Promise<string> { | ||
const attributes = await this.getMailTemplateAttributes( | ||
tenantId, | ||
invoiceId | ||
); | ||
const mergedAttributes = { ...attributes, ...overrideAttributes }; | ||
|
||
return renderInvoicePaymentEmail(mergedAttributes); | ||
} | ||
} |
Oops, something went wrong.