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

View File

@ -1,11 +1,39 @@
const models = require('vn-loopback/server/server').models;
describe('collection getTickets()', () => {
it('should return a list of tickets from a collection', async() => {
let ctx = {req: {accessToken: {userId: 1107}}};
let response = await models.Collection.getCollection(ctx);
let ctx;
beforeAll(async() => {
ctx = {
req: {
accessToken: {userId: 9},
headers: {origin: 'http://localhost'}
}
};
});
expect(response.length).toBeGreaterThan(0);
expect(response[0].collectionFk).toEqual(3);
it('should get tickets, sales and barcodes from collection', async() => {
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