From 6a17a634d41a62e5ce3b8fa733736ef407bf315f Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 24 Oct 2022 09:49:51 +0200 Subject: [PATCH] hotFix(receipt): add receiptPdf --- .../client/back/methods/receipt/receiptPdf.js | 55 +++++++++++++++++++ modules/client/back/models/receipt.js | 1 + modules/client/front/balance/create/index.js | 8 +-- .../client/front/balance/create/index.spec.js | 8 +-- 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 modules/client/back/methods/receipt/receiptPdf.js diff --git a/modules/client/back/methods/receipt/receiptPdf.js b/modules/client/back/methods/receipt/receiptPdf.js new file mode 100644 index 000000000..f55e05040 --- /dev/null +++ b/modules/client/back/methods/receipt/receiptPdf.js @@ -0,0 +1,55 @@ +const {Report} = require('vn-print'); + +module.exports = Self => { + Self.remoteMethodCtx('receiptPdf', { + description: 'Returns the receipt pdf', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The claim id', + http: {source: 'path'} + }, + { + arg: 'recipientId', + type: 'number', + description: 'The recipient id', + required: false + } + ], + returns: [ + { + arg: 'body', + type: 'file', + root: true + }, { + arg: 'Content-Type', + type: 'String', + http: {target: 'header'} + }, { + arg: 'Content-Disposition', + type: 'String', + http: {target: 'header'} + } + ], + http: { + path: '/:id/receipt-pdf', + verb: 'GET' + } + }); + + Self.receiptPdf = async(ctx, id) => { + const args = Object.assign({}, ctx.args); + const params = {lang: ctx.req.getLocale()}; + + delete args.ctx; + for (const param in args) + params[param] = args[param]; + + const report = new Report('receipt', params); + const stream = await report.toPdfStream(); + + return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; + }; +}; diff --git a/modules/client/back/models/receipt.js b/modules/client/back/models/receipt.js index 36a4a8952..b79102e6b 100644 --- a/modules/client/back/models/receipt.js +++ b/modules/client/back/models/receipt.js @@ -2,6 +2,7 @@ const LoopBackContext = require('loopback-context'); module.exports = function(Self) { require('../methods/receipt/filter')(Self); + require('../methods/receipt/receiptPdf')(Self); Self.validateBinded('amountPaid', isNotZero, { message: 'Amount cannot be zero', diff --git a/modules/client/front/balance/create/index.js b/modules/client/front/balance/create/index.js index c6a6e7ff9..935129574 100644 --- a/modules/client/front/balance/create/index.js +++ b/modules/client/front/balance/create/index.js @@ -144,12 +144,8 @@ class Controller extends Dialog { }) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) .then(() => { - if (this.viewReceipt) { - this.vnReport.show('receipt', { - receiptId: receiptId, - companyId: this.companyFk - }); - } + if (this.viewReceipt) + this.vnReport.show(`Receipts/${receiptId}/receipt-pdf`); }); } diff --git a/modules/client/front/balance/create/index.spec.js b/modules/client/front/balance/create/index.spec.js index 77fe32e0f..fa6b48ea4 100644 --- a/modules/client/front/balance/create/index.spec.js +++ b/modules/client/front/balance/create/index.spec.js @@ -85,6 +85,8 @@ describe('Client', () => { }); it('should make an http POST query and then call to the report show() method', () => { + const receiptId = 1; + jest.spyOn(controller.vnApp, 'showSuccess'); jest.spyOn(controller.vnReport, 'show'); window.open = jest.fn(); @@ -92,14 +94,12 @@ describe('Client', () => { controller.$params = {id: 1101}; controller.viewReceipt = true; - $httpBackend.expect('POST', `Clients/1101/createReceipt`).respond({id: 1}); + $httpBackend.expect('POST', `Clients/1101/createReceipt`).respond({id: receiptId}); controller.responseHandler('accept'); $httpBackend.flush(); - const expectedParams = {receiptId: 1, companyId: 442}; - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - expect(controller.vnReport.show).toHaveBeenCalledWith('receipt', expectedParams); + expect(controller.vnReport.show).toHaveBeenCalledWith(`Receipts/${receiptId}/receipt-pdf`); }); });