From 581229053a149ae5ec43ef93e244962cfb8f357b Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sat, 2 Nov 2024 16:02:17 +0200 Subject: [PATCH] fix: attach payment link in sending invoice mail receipt --- .../controllers/ShareLink/ShareLinkController.ts | 2 -- .../Sales/Invoices/GenerateeInvoicePaymentLink.ts | 8 +++----- .../Sales/Invoices/SendInvoiceInvoiceMailCommon.ts | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/controllers/ShareLink/ShareLinkController.ts b/packages/server/src/api/controllers/ShareLink/ShareLinkController.ts index 52065924f..a06e7bb06 100644 --- a/packages/server/src/api/controllers/ShareLink/ShareLinkController.ts +++ b/packages/server/src/api/controllers/ShareLink/ShareLinkController.ts @@ -30,7 +30,6 @@ export class ShareLinkController extends BaseController { this.validationResult, asyncMiddleware(this.generateShareLink.bind(this)) ); - return router; } @@ -53,7 +52,6 @@ export class ShareLinkController extends BaseController { const link = await this.generateShareLinkService.generatePaymentLink( tenantId, transactionId, - transactionType, publicity, expiryDate ); diff --git a/packages/server/src/services/Sales/Invoices/GenerateeInvoicePaymentLink.ts b/packages/server/src/services/Sales/Invoices/GenerateeInvoicePaymentLink.ts index 7063dee98..79f9f7702 100644 --- a/packages/server/src/services/Sales/Invoices/GenerateeInvoicePaymentLink.ts +++ b/packages/server/src/services/Sales/Invoices/GenerateeInvoicePaymentLink.ts @@ -32,15 +32,14 @@ export class GenerateShareLink { */ async generatePaymentLink( tenantId: number, - transactionId: number, - transactionType: string, + saleInvoiceId: number, publicity: string = 'private', expiryTime: string = '' ) { const { SaleInvoice } = this.tenancy.models(tenantId); const foundInvoice = await SaleInvoice.query() - .findById(transactionId) + .findById(saleInvoiceId) .throwIfNotFound(); // Generate unique uuid for sharable link. @@ -48,8 +47,7 @@ export class GenerateShareLink { const commonEventPayload = { tenantId, - transactionId, - transactionType, + saleInvoiceId, publicity, expiryTime, }; diff --git a/packages/server/src/services/Sales/Invoices/SendInvoiceInvoiceMailCommon.ts b/packages/server/src/services/Sales/Invoices/SendInvoiceInvoiceMailCommon.ts index 4edfe3464..c69bc5ef4 100644 --- a/packages/server/src/services/Sales/Invoices/SendInvoiceInvoiceMailCommon.ts +++ b/packages/server/src/services/Sales/Invoices/SendInvoiceInvoiceMailCommon.ts @@ -8,6 +8,7 @@ import { DEFAULT_INVOICE_MAIL_SUBJECT, } from './constants'; import { GetInvoicePaymentMail } from './GetInvoicePaymentMail'; +import { GenerateShareLink } from './GenerateeInvoicePaymentLink'; @Service() export class SendSaleInvoiceMailCommon { @@ -23,6 +24,9 @@ export class SendSaleInvoiceMailCommon { @Inject() private getInvoicePaymentMail: GetInvoicePaymentMail; + @Inject() + private generatePaymentLinkService: GenerateShareLink; + /** * Retrieves the mail options. * @param {number} tenantId - Tenant id. @@ -81,6 +85,13 @@ export class SendSaleInvoiceMailCommon { mailOptions, formatterArgs ); + // Generates the a new payment link for the given invoice. + const paymentLink = + await this.generatePaymentLinkService.generatePaymentLink( + tenantId, + invoiceId, + 'public' + ); const message = await this.getInvoicePaymentMail.getMailTemplate( tenantId, invoiceId, @@ -88,6 +99,9 @@ export class SendSaleInvoiceMailCommon { // # Invoice message invoiceMessage: formattedOptions.message, preview: formattedOptions.message, + + // # Payment link + viewInvoiceButtonUrl: paymentLink.link, } ); return { ...formattedOptions, message };