From 5acbaf036a521cb6dcd2e971d933dcea7e56665d Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 15 Nov 2022 09:30:36 +0100 Subject: [PATCH] refs #3557 back and test done --- .../10503-november/00-sale_missingTrash.sql | 23 ++++++ .../ticket/back/methods/sale/missingTrash.js | 70 +++++++++++++++++++ .../methods/sale/specs/missingTrash.spec.js | 35 ++++++++++ modules/ticket/back/models/sale.js | 1 + 4 files changed, 129 insertions(+) create mode 100644 db/changes/10503-november/00-sale_missingTrash.sql create mode 100644 modules/ticket/back/methods/sale/missingTrash.js create mode 100644 modules/ticket/back/methods/sale/specs/missingTrash.spec.js diff --git a/db/changes/10503-november/00-sale_missingTrash.sql b/db/changes/10503-november/00-sale_missingTrash.sql new file mode 100644 index 000000000..f0de6d5cd --- /dev/null +++ b/db/changes/10503-november/00-sale_missingTrash.sql @@ -0,0 +1,23 @@ +DROP PROCEDURE IF EXISTS vn.sale_missingTrash; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_missingTrash`( + vSaleFk BIGINT, + vQuantity INT, + vIsTrash BOOLEAN, + vWarehouseFk INT, + vNewQuantity INT) +/** + * Modifica la cantidad de una sale tirĂ¡ndola a faltas o basura + * + * @param vSaleFk el id de la sale + * @param vQuantity cantidad que se va a tirar a faltas o basura + * @param vIsTrash true si es basura, false si es faltas + * @param vWarehouseFk id warehouse + * @param vNewQuantity cantidad que se queda en el ticket original + */ +BEGIN + CALL vn.collection_missingTrash(vSaleFk ,vQuantity ,vIsTrash ,vWarehouseFk ,vNewQuantity); +END$$ +DELIMITER ; diff --git a/modules/ticket/back/methods/sale/missingTrash.js b/modules/ticket/back/methods/sale/missingTrash.js new file mode 100644 index 000000000..6d4f69178 --- /dev/null +++ b/modules/ticket/back/methods/sale/missingTrash.js @@ -0,0 +1,70 @@ +module.exports = Self => { + Self.remoteMethodCtx('missingTrash', { + description: 'Modify the amount of a sale by throwing it to faults or garbage', + accessType: 'WRITE', + accepts: [ + { + arg: 'saleFk', + type: 'number', + required: true, + description: 'The sale id' + }, + { + arg: 'quantity', + type: 'number', + required: true, + description: 'Quantity that is going to be thrown away' + }, + { + arg: 'isTrash', + type: 'boolean', + required: true, + description: 'True if garbage, false if fault' + }, + { + arg: 'warehouseFk', + type: 'number', + required: true, + description: 'The warehouse id' + }, + { + arg: 'newQuantity', + type: 'number', + required: true, + description: 'Amount remaining on the original ticket' + }, + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/missingTrash`, + verb: 'POST' + } + }); + + Self.missingTrash = async function (ctx, saleFk, quantity, isTrash, warehouseFk, newQuantity, options) { + const myOptions = {}; + let tx; + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const missingTrash = await Self.rawSql(`CALL vn.sale_missingTrash(?, ?, ?, ?, ?)`, + [saleFk, quantity, isTrash, warehouseFk, newQuantity], myOptions); + + if (tx) await tx.commit(); + + return missingTrash; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/methods/sale/specs/missingTrash.spec.js b/modules/ticket/back/methods/sale/specs/missingTrash.spec.js new file mode 100644 index 000000000..62fd74343 --- /dev/null +++ b/modules/ticket/back/methods/sale/specs/missingTrash.spec.js @@ -0,0 +1,35 @@ +const models = require('vn-loopback/server/server').models; + +fdescribe('sale missingTrash()', () => { + it('should modify the amount of a sale by throwing it to faults or garbage', async () => { + const tx = await models.Sale.beginTransaction({}); + + try { + const options = { transaction: tx }; + + const saleFk = 1; + const quantity = 3; + const isTrash = true; + const warehouseFk = 1; + const newQuantity = 2; + + const ctx = { req: { accessToken: { userId: 9 } } }; + + let sale = await models.Sale.findById(saleFk, null, options); + expect(sale.quantity).toEqual(5); + expect(sale.originalQuantity).toBe(null); + + await models.Sale.missingTrash(ctx, saleFk, quantity, isTrash, warehouseFk, newQuantity, options); + + sale = await models.Sale.findById(saleFk, null, options); + expect(sale.quantity).toEqual(2); + expect(sale.originalQuantity).toBe(5); + + await tx.rollback(); + + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/ticket/back/models/sale.js b/modules/ticket/back/models/sale.js index ae247fc24..cb8814e67 100644 --- a/modules/ticket/back/models/sale.js +++ b/modules/ticket/back/models/sale.js @@ -9,6 +9,7 @@ module.exports = Self => { require('../methods/sale/refund')(Self); require('../methods/sale/canEdit')(Self); require('../methods/sale/usesMana')(Self); + require('../methods/sale/missingTrash')(Self); Self.validatesPresenceOf('concept', { message: `Concept cannot be blank`