feat: payment received mail preview

This commit is contained in:
Ahmed Bouhuolia
2024-11-24 13:19:26 +02:00
parent da47418f17
commit 3537a05ea2
19 changed files with 438 additions and 49 deletions

View File

@@ -0,0 +1,75 @@
import { Inject, Service } from 'typedi';
import {
renderEstimateEmailTemplate,
EstimatePaymentEmailProps,
} from '@bigcapital/email-components';
import { GetSaleEstimate } from './GetSaleEstimate';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { GetEstimateMailTemplateAttributesTransformer } from './GetEstimateMailTemplateAttributesTransformer';
import { GetPdfTemplate } from '@/services/PdfTemplate/GetPdfTemplate';
@Service()
export class GetEstimateMailTemplate {
@Inject()
private getEstimateService: GetSaleEstimate;
@Inject()
private transformer: TransformerInjectable;
@Inject()
private getBrandingTemplate: GetPdfTemplate;
/**
* Retrieves the mail template attributes of the given estimate.
* Estimate template attributes are composed of the estimate and branding template attributes.
* @param {number} tenantId
* @param {number} estimateId - Estimate id.
* @returns {Promise<EstimatePaymentEmailProps>}
*/
public async getMailTemplateAttributes(
tenantId: number,
estimateId: number
): Promise<EstimatePaymentEmailProps> {
const estimate = await this.getEstimateService.getEstimate(
tenantId,
estimateId
);
const brandingTemplate = await this.getBrandingTemplate.getPdfTemplate(
tenantId,
estimate.pdfTemplateId
);
const mailTemplateAttributes = await this.transformer.transform(
tenantId,
estimate,
new GetEstimateMailTemplateAttributesTransformer(),
{
estimate,
brandingTemplate,
}
);
return mailTemplateAttributes;
}
/**
* Rertieves the mail template html content.
* @param {number} tenantId
* @param {number} estimateId
* @param overrideAttributes
* @returns
*/
public async getMailTemplate(
tenantId: number,
estimateId: number,
overrideAttributes?: Partial<any>
): Promise<string> {
const attributes = await this.getMailTemplateAttributes(
tenantId,
estimateId
);
const mergedAttributes = {
...attributes,
...overrideAttributes,
};
return renderEstimateEmailTemplate(mergedAttributes);
}
}

View File

@@ -0,0 +1,3 @@
import { Transformer } from '@/lib/Transformer/Transformer';
export class GetEstimateMailTemplateAttributesTransformer extends Transformer {}

View File

@@ -17,6 +17,7 @@ import { mergeAndValidateMailOptions } from '@/services/MailNotification/utils';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { transformEstimateToMailDataArgs } from './utils';
import { GetEstimateMailTemplate } from './GetEstimateMailTemplate';
@Service()
export class SendSaleEstimateMail {
@@ -32,12 +33,15 @@ export class SendSaleEstimateMail {
@Inject()
private contactMailNotification: ContactMailNotification;
@Inject('agenda')
private agenda: any;
@Inject()
private getEstimateMailTemplate: GetEstimateMailTemplate;
@Inject()
private eventPublisher: EventPublisher;
@Inject('agenda')
private agenda: any;
/**
* Triggers the reminder mail of the given sale estimate.
* @param {number} tenantId -
@@ -132,9 +136,45 @@ export class SendSaleEstimateMail {
mailOptions,
formatterArgs
);
return { ...formattedOptions };
// Retrieves the estimate mail template.
const message = await this.getEstimateMailTemplate.getMailTemplate(
tenantId,
saleEstimateId,
{
message: formattedOptions.message,
preview: formattedOptions.message,
}
);
return { ...formattedOptions, message };
};
/**
* Retrieves the formatted mail options.
* @param {number} tenantId
* @param {number} saleEstimateId
* @param {SaleEstimateMailOptionsDTO} messageOptions
* @returns
*/
public async getFormattedMailOptions(
tenantId: number,
saleEstimateId: number,
messageOptions: SaleEstimateMailOptionsDTO
): Promise<SaleEstimateMailOptions> {
const defaultMessageOptions = await this.getMailOptions(
tenantId,
saleEstimateId
);
const parsedMessageOptions = mergeAndValidateMailOptions(
defaultMessageOptions,
messageOptions
);
return this.formatMailOptions(
tenantId,
saleEstimateId,
parsedMessageOptions
);
}
/**
* Sends the mail notification of the given sale estimate.
* @param {number} tenantId
@@ -147,20 +187,10 @@ export class SendSaleEstimateMail {
saleEstimateId: number,
messageOptions: SaleEstimateMailOptionsDTO
): Promise<void> {
const localMessageOpts = await this.getMailOptions(
tenantId,
saleEstimateId
);
// Overrides and validates the given mail options.
const parsedMessageOptions = mergeAndValidateMailOptions(
localMessageOpts,
messageOptions
) as SaleEstimateMailOptions;
const formattedOptions = await this.formatMailOptions(
const formattedOptions = await this.getFormattedMailOptions(
tenantId,
saleEstimateId,
parsedMessageOptions
messageOptions
);
const mail = new Mail()
.setSubject(formattedOptions.subject)
@@ -182,7 +212,6 @@ export class SendSaleEstimateMail {
},
]);
}
const eventPayload = {
tenantId,
saleEstimateId,