refs #6254 feat(collection_getTickets): refactor code and add test
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2023-09-28 08:30:50 +02:00
parent 1307d4727c
commit d4e5815d41
4 changed files with 56 additions and 28 deletions

View File

@ -4,10 +4,11 @@ module.exports = Self => {
description: 'Make a new collection of tickets', description: 'Make a new collection of tickets',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{ accepts: [{
arg: 'paramId', arg: 'id',
type: 'number', type: 'number',
description: 'The collection or ticket id', description: 'The collection id',
required: true required: true,
http: {source: 'path'}
}, { }, {
arg: 'print', arg: 'print',
type: 'boolean', type: 'boolean',
@ -18,12 +19,12 @@ module.exports = Self => {
root: true root: true
}, },
http: { http: {
path: `/getTickets`, path: `/:id/getTickets`,
verb: 'POST' verb: 'POST'
} }
}); });
Self.getTickets = async(ctx, paramId, print, options) => { Self.getTickets = async(ctx, id, print, options) => {
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const origin = ctx.req.headers.origin; const origin = ctx.req.headers.origin;
const $t = ctx.req.__; // $translate const $t = ctx.req.__; // $translate
@ -42,7 +43,7 @@ module.exports = Self => {
const promises = []; const promises = [];
try { try {
const [tickets] = await Self.rawSql(`CALL vn.collection_getTickets(?)`, [paramId], myOptions); const [tickets] = await Self.rawSql(`CALL vn.collection_getTickets(?)`, [id], myOptions);
const sales = await Self.rawSql(` const sales = await Self.rawSql(`
SELECT SELECT
s.ticketFk, s.ticketFk,
@ -104,14 +105,14 @@ module.exports = Self => {
LEFT JOIN saleMistake sm ON sm.saleFk = s.id LEFT JOIN saleMistake sm ON sm.saleFk = s.id
WHERE tc.collectionFk = ? WHERE tc.collectionFk = ?
GROUP BY s.id, p.code, p2.code GROUP BY s.id, p.code, p2.code
ORDER BY pickingOrder`, [paramId, paramId], myOptions); ORDER BY pickingOrder`, [id, id], myOptions);
if (print) if (print)
await Self.rawSql(`CALL vn.collection_printSticker(?, ?)`, [paramId, null], myOptions); await Self.rawSql(`CALL vn.collection_printSticker(?, ?)`, [id, null], myOptions);
const collection = {collectionFk: paramId, tickets: []}; const collection = {collectionFk: id, tickets: []};
if (tickets && tickets.length) { if (tickets && tickets.length) {
for (let ticket of tickets) { for (const ticket of tickets) {
const ticketId = ticket.ticketFk; const ticketId = ticket.ticketFk;
// SEND ROCKET // SEND ROCKET
@ -142,26 +143,25 @@ module.exports = Self => {
[ticketId], myOptions); [ticketId], myOptions);
// BINDINGS // BINDINGS
ticket.sales = sales.reduce((acc, sale) => { ticket.sales = [];
if (sale.ticketFk == ticketId) { for (const sale of sales) {
if (sale.ticketFk === ticketId) {
sale.Barcodes = []; sale.Barcodes = [];
if (barcodes && barcodes.length) { if (barcodes && barcodes.length) {
sale.Barcodes = barcodes.reduce((bacc, barcode) => { for (const barcode of barcodes) {
if (barcode.saleFk == sale.saleFk) { if (barcode.saleFk === sale.saleFk) {
for (let prop in barcode) { for (let prop in barcode) {
if (['id', 'code'].includes(prop) && barcode[prop]) { if (['id', 'code'].includes(prop) && barcode[prop])
bacc.push(barcode[prop].toString()); sale.Barcodes.push(barcode[prop].toString(), '0' + barcode[prop]);
bacc.push('0' + barcode[prop]);
} }
} }
} }
return bacc;
}, []);
} }
acc.push(sale);
ticket.sales.push(sale);
}
} }
return acc;
}, []);
} }
collection.tickets.push(ticket); collection.tickets.push(ticket);
} }

View File

@ -1,11 +1,39 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('collection getTickets()', () => { describe('collection getTickets()', () => {
it('should return a list of tickets from a collection', async() => { let ctx;
let ctx = {req: {accessToken: {userId: 1107}}}; beforeAll(async() => {
let response = await models.Collection.getCollection(ctx); ctx = {
req: {
accessToken: {userId: 9},
headers: {origin: 'http://localhost'}
}
};
});
expect(response.length).toBeGreaterThan(0); it('should get tickets, sales and barcodes from collection', async() => {
expect(response[0].collectionFk).toEqual(3); const tx = await models.Collection.beginTransaction({});
try {
const options = {transaction: tx};
const collectionId = 1;
const collectionTickets = await models.Collection.getTickets(ctx, collectionId, null, options);
expect(collectionTickets.collectionFk).toEqual(collectionId);
expect(collectionTickets.tickets.length).toEqual(3);
expect(collectionTickets.tickets[0].ticketFk).toEqual(1);
expect(collectionTickets.tickets[1].ticketFk).toEqual(2);
expect(collectionTickets.tickets[2].ticketFk).toEqual(23);
expect(collectionTickets.tickets[0].sales[0].ticketFk).toEqual(1);
expect(collectionTickets.tickets[0].sales[1].ticketFk).toEqual(1);
expect(collectionTickets.tickets[0].sales[2].ticketFk).toEqual(1);
expect(collectionTickets.tickets[0].sales[0].Barcodes.length).toBeTruthy();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });

View File