From 30c896c975adec36486be7386aef9d79919bc176 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 24 May 2023 11:20:07 +0200 Subject: [PATCH] refs #5678 feat(sale_canEdit): handle exist sale --- loopback/locale/es.json | 3 +- modules/ticket/back/methods/sale/canEdit.js | 5 ++- .../back/methods/sale/specs/canEdit.spec.js | 42 +++++++++++++------ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 45993bdd5..956e66985 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -84,6 +84,7 @@ "The current ticket can't be modified": "El ticket actual no puede ser modificado", "The current claim can't be modified": "La reclamación actual no puede ser modificada", "The sales of this ticket can't be modified": "Las lineas de este ticket no pueden ser modificadas", + "The sales do not exist": "La(s) linea(s) seleccionada no(s) exite(n)", "Please select at least one sale": "Por favor selecciona al menos una linea", "All sales must belong to the same ticket": "Todas las lineas deben pertenecer al mismo ticket", "NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada", @@ -291,4 +292,4 @@ "comercialId": "Id comercial", "comercialName": "Comercial", "Invalid NIF for VIES": "Invalid NIF for VIES" -} \ No newline at end of file +} diff --git a/modules/ticket/back/methods/sale/canEdit.js b/modules/ticket/back/methods/sale/canEdit.js index 3091ebca7..df0a939bf 100644 --- a/modules/ticket/back/methods/sale/canEdit.js +++ b/modules/ticket/back/methods/sale/canEdit.js @@ -38,6 +38,9 @@ module.exports = Self => { } }, myOptions); + if (!salesData.length) + throw new UserError(`The sales do not exist`); + const ticketId = salesData[0].ticketFk; const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId, myOptions); @@ -62,7 +65,5 @@ module.exports = Self => { throw new UserError('It is not possible to modify cloned sales'); if (!shouldEditFloramondo) throw new UserError('It is not possible to modify sales that their articles are from Floramondo'); - - return true; }; }; diff --git a/modules/ticket/back/methods/sale/specs/canEdit.spec.js b/modules/ticket/back/methods/sale/specs/canEdit.spec.js index 62f98421a..c12277e80 100644 --- a/modules/ticket/back/methods/sale/specs/canEdit.spec.js +++ b/modules/ticket/back/methods/sale/specs/canEdit.spec.js @@ -17,6 +17,32 @@ describe('sale canEdit()', () => { }); }); + describe('sale not exist', () => { + it('should return error if sale not exist', async() => { + const tx = await models.Sale.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const developerId = 9; + const ctx = {req: {accessToken: {userId: developerId}}}; + + let max = await models.Sale.findOne({fields: ['id'], order: 'id DESC'}, options); + max.id = max.id + 1; + + const sales = [max.id]; + await models.Sale.canEdit(ctx, sales, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e.message; + } + + expect(error).toEqual('The sales do not exist'); + }); + }); + describe('sale editTracked', () => { it('should return true if the role is production regardless of the saleTrackings', async() => { const tx = await models.Sale.beginTransaction({}); @@ -29,9 +55,7 @@ describe('sale canEdit()', () => { const sales = [25]; - const result = await models.Sale.canEdit(ctx, sales, options); - - expect(result).toEqual(true); + await models.Sale.canEdit(ctx, sales, options); await tx.rollback(); } catch (e) { @@ -51,9 +75,7 @@ describe('sale canEdit()', () => { const sales = [10]; - const result = await models.Sale.canEdit(ctx, sales, options); - - expect(result).toEqual(true); + await models.Sale.canEdit(ctx, sales, options); await tx.rollback(); } catch (e) { @@ -87,9 +109,7 @@ describe('sale canEdit()', () => { }); const ctx = {req: {accessToken: {userId: role.id}}}; - const result = await models.Sale.canEdit(ctx, saleCloned, options); - - expect(result).toEqual(true); + await models.Sale.canEdit(ctx, saleCloned, options); await tx.rollback(); } catch (e) { @@ -150,9 +170,7 @@ describe('sale canEdit()', () => { const saleToEdit = await models.Sale.findById(sales[0], null, options); await saleToEdit.updateAttribute('itemFk', 9, options); - const result = await models.Sale.canEdit(ctx, sales, options); - - expect(result).toEqual(true); + await models.Sale.canEdit(ctx, sales, options); await tx.rollback(); } catch (e) {