From ba8091d69718367a35a8d08d84c09b4bbcbd23f3 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Mon, 11 Nov 2024 19:01:43 +0200 Subject: [PATCH] fix: download invoice document on payment page --- .../controllers/ShareLink/PublicSharableLinkController.ts | 7 ++++--- .../src/services/PaymentLinks/GetPaymentLinkInvoicePdf.ts | 6 ++++-- .../src/services/PaymentLinks/PaymentLinksApplication.ts | 6 ++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/controllers/ShareLink/PublicSharableLinkController.ts b/packages/server/src/api/controllers/ShareLink/PublicSharableLinkController.ts index ea31ec2f0..aae07abe7 100644 --- a/packages/server/src/api/controllers/ShareLink/PublicSharableLinkController.ts +++ b/packages/server/src/api/controllers/ShareLink/PublicSharableLinkController.ts @@ -102,12 +102,13 @@ export class PublicSharableLinkController extends BaseController { const { paymentLinkId } = req.params; try { - const pdfContent = await this.paymentLinkApp.getPaymentLinkInvoicePdf( - paymentLinkId - ); + const [pdfContent, filename] = + await this.paymentLinkApp.getPaymentLinkInvoicePdf(paymentLinkId); + res.set({ 'Content-Type': 'application/pdf', 'Content-Length': pdfContent.length, + 'Content-Disposition': `attachment; filename="${filename}"`, }); res.send(pdfContent); } catch (error) { diff --git a/packages/server/src/services/PaymentLinks/GetPaymentLinkInvoicePdf.ts b/packages/server/src/services/PaymentLinks/GetPaymentLinkInvoicePdf.ts index 7122cfea2..41ff85c0b 100644 --- a/packages/server/src/services/PaymentLinks/GetPaymentLinkInvoicePdf.ts +++ b/packages/server/src/services/PaymentLinks/GetPaymentLinkInvoicePdf.ts @@ -12,9 +12,11 @@ export class GetPaymentLinkInvoicePdf { * Retrieves the sale invoice PDF of the given payment link id. * @param {number} tenantId * @param {number} paymentLinkId - * @returns {Promise} + * @returns {Promise} */ - async getPaymentLinkInvoicePdf(paymentLinkId: string): Promise { + async getPaymentLinkInvoicePdf( + paymentLinkId: string + ): Promise<[Buffer, string]> { const paymentLink = await PaymentLink.query() .findOne('linkId', paymentLinkId) .where('resourceType', 'SaleInvoice') diff --git a/packages/server/src/services/PaymentLinks/PaymentLinksApplication.ts b/packages/server/src/services/PaymentLinks/PaymentLinksApplication.ts index 4e152535f..6938f964b 100644 --- a/packages/server/src/services/PaymentLinks/PaymentLinksApplication.ts +++ b/packages/server/src/services/PaymentLinks/PaymentLinksApplication.ts @@ -11,7 +11,7 @@ export class PaymentLinksApplication { @Inject() private createInvoiceCheckoutSessionService: CreateInvoiceCheckoutSession; - + @Inject() private getPaymentLinkInvoicePdfService: GetPaymentLinkInvoicePdf; @@ -45,7 +45,9 @@ export class PaymentLinksApplication { * @param {number} paymentLinkId * @returns {Promise } */ - public getPaymentLinkInvoicePdf(paymentLinkId: string): Promise { + public getPaymentLinkInvoicePdf( + paymentLinkId: string + ): Promise<[Buffer, string]> { return this.getPaymentLinkInvoicePdfService.getPaymentLinkInvoicePdf( paymentLinkId );