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

@@ -4,6 +4,7 @@ import { GetPaymentReceivedMailStateTransformer } from './GetPaymentReceivedMail
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { Inject, Service } from 'typedi';
import { ContactMailNotification } from '@/services/MailNotification/ContactMailNotification';
import { SendPaymentReceiveMailNotification } from './PaymentReceivedMailNotification';
@Service()
export class GetPaymentReceivedMailState {
@@ -11,7 +12,7 @@ export class GetPaymentReceivedMailState {
private tenancy: HasTenancyService;
@Inject()
private contactMailNotification: ContactMailNotification;
private paymentReceivedMail: SendPaymentReceiveMailNotification;
@Inject()
private transformer: TransformerInjectable;
@@ -35,12 +36,10 @@ export class GetPaymentReceivedMailState {
.withGraphFetched('pdfTemplate')
.throwIfNotFound();
const mailOptions =
await this.contactMailNotification.getDefaultMailOptions(
tenantId,
paymentReceive.customerId
);
const mailOptions = await this.paymentReceivedMail.getMailOptions(
tenantId,
paymentId
);
const transformed = await this.transformer.transform(
tenantId,
paymentReceive,

View File

@@ -0,0 +1,75 @@
import { Inject, Service } from 'typedi';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { GetPdfTemplate } from '@/services/PdfTemplate/GetPdfTemplate';
import { GetPaymentReceived } from './GetPaymentReceived';
import { GetPaymentReceivedMailTemplateAttrsTransformer } from './GetPaymentReceivedMailTemplateAttrsTransformer';
import {
PaymentReceivedEmailTemplateProps,
renderPaymentReceivedEmailTemplate,
} from '@bigcapital/email-components';
@Service()
export class GetPaymentReceivedMailTemplate {
@Inject()
private getPaymentReceivedService: GetPaymentReceived;
@Inject()
private getBrandingTemplate: GetPdfTemplate;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieves the mail template attributes of the given payment received.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceivedId - Payment received id.
* @returns {Promise<PaymentReceivedEmailTemplateProps>}
*/
public async getMailTemplateAttributes(
tenantId: number,
paymentReceivedId: number
): Promise<PaymentReceivedEmailTemplateProps> {
const paymentReceived =
await this.getPaymentReceivedService.getPaymentReceive(
tenantId,
paymentReceivedId
);
const brandingTemplate = await this.getBrandingTemplate.getPdfTemplate(
tenantId,
paymentReceived.pdfTemplateId
);
const mailTemplateAttributes = await this.transformer.transform(
tenantId,
paymentReceived,
new GetPaymentReceivedMailTemplateAttrsTransformer(),
{
paymentReceived,
brandingTemplate,
}
);
return mailTemplateAttributes;
}
/**
* Retrieves the mail template html content.
* @param {number} tenantId
* @param {number} paymentReceivedId
* @param {Partial<PaymentReceivedEmailTemplateProps>} overrideAttributes
* @returns
*/
public async getMailTemplate(
tenantId: number,
paymentReceivedId: number,
overrideAttributes?: Partial<PaymentReceivedEmailTemplateProps>
): Promise<string> {
const mailTemplateAttributes = await this.getMailTemplateAttributes(
tenantId,
paymentReceivedId
);
const mergedAttributes = {
...mailTemplateAttributes,
...overrideAttributes,
};
return renderPaymentReceivedEmailTemplate(mergedAttributes);
}
}

View File

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

View File

@@ -15,8 +15,9 @@ import { GetPaymentReceived } from './GetPaymentReceived';
import { ContactMailNotification } from '@/services/MailNotification/ContactMailNotification';
import { mergeAndValidateMailOptions } from '@/services/MailNotification/utils';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { transformPaymentReceivedToMailDataArgs } from './utils';
import { GetPaymentReceivedMailTemplate } from './GetPaymentReceivedMailTemplate';
import events from '@/subscribers/events';
@Service()
export class SendPaymentReceiveMailNotification {
@@ -29,12 +30,15 @@ export class SendPaymentReceiveMailNotification {
@Inject()
private contactMailNotification: ContactMailNotification;
@Inject('agenda')
private agenda: any;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private paymentMailTemplate: GetPaymentReceivedMailTemplate;
@Inject('agenda')
private agenda: any;
/**
* Sends the mail of the given payment receive.
* @param {number} tenantId
@@ -77,7 +81,82 @@ export class SendPaymentReceiveMailNotification {
tenantId,
invoiceId
);
return transformPaymentReceivedToMailDataArgs(payment);
const commonArgs = await this.contactMailNotification.getCommonFormatArgs(
tenantId
);
const paymentArgs = transformPaymentReceivedToMailDataArgs(payment);
return {
...commonArgs,
...paymentArgs,
};
};
/**
* Retrieves the mail options of the given payment received.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceivedId - Payment received id.
* @param {string} defaultSubject - Default subject of the mail.
* @param {string} defaultContent - Default content of the mail.
* @returns
*/
public getMailOptions = async (
tenantId: number,
paymentReceivedId: number,
defaultSubject: string = DEFAULT_PAYMENT_MAIL_SUBJECT,
defaultContent: string = DEFAULT_PAYMENT_MAIL_CONTENT
): Promise<PaymentReceiveMailOpts> => {
const { PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceived = await PaymentReceive.query().findById(
paymentReceivedId
);
const formatArgs = await this.textFormatter(tenantId, paymentReceivedId);
// Retrieves the default mail options.
const mailOptions =
await this.contactMailNotification.getDefaultMailOptions(
tenantId,
paymentReceived.customerId
);
return {
...mailOptions,
message: defaultContent,
subject: defaultSubject,
attachPdf: true,
formatArgs,
};
};
/**
* Formats the mail options of the given payment receive.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {PaymentReceiveMailOpts} mailOptions
* @returns {Promise<PaymentReceiveMailOpts>}
*/
public formattedMailOptions = async (
tenantId: number,
paymentReceiveId: number,
mailOptions: PaymentReceiveMailOpts
): Promise<PaymentReceiveMailOpts> => {
const formatterArgs = await this.textFormatter(tenantId, paymentReceiveId);
const formattedOptions =
await this.contactMailNotification.formatMailOptions(
tenantId,
mailOptions,
formatterArgs
);
// Retrieves the mail template.
const message = await this.paymentMailTemplate.getMailTemplate(
tenantId,
paymentReceiveId,
{
message: formattedOptions.message,
preview: formattedOptions.message,
}
);
return { ...formattedOptions, message };
};
/**
@@ -105,10 +184,10 @@ export class SendPaymentReceiveMailNotification {
messageDTO
);
// Formats the message options.
return this.contactMailNotification.formatMailOptions(
return this.formattedMailOptions(
tenantId,
parsedMessageOpts,
formatterArgs
paymentReceiveId,
parsedMessageOpts
);
};