Merge pull request 'MASTER_hotFix(receipt): add receiptPdf' (!1100) from hotFix_receiptPdf into master
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #1100
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Joan Sanchez 2022-10-24 10:23:05 +00:00
commit fe95c45c42
5 changed files with 65 additions and 10 deletions

View File

@ -0,0 +1,3 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES
('Receipt', 'receiptPdf', '*', 'ALLOW', 'ROLE', 'salesAssistant');

View File

@ -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"`];
};
};

View File

@ -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',

View File

@ -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`);
});
}

View File

@ -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`);
});
});