From f83f7808c8d41fa66f103199e69bea9b8e84f8fe Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 15 Mar 2024 09:32:36 +0100 Subject: [PATCH] refs #6321 feat: negativeOrigin method --- db/changes/240601/01-updateACLItemLack.sql | 4 ++ .../back/methods/ticket/itemLackOrigin.js | 40 +++++++++++++++++++ .../ticket/specs/itemLackoOrigin.spec.js | 19 +++++++++ modules/ticket/back/models/ticket-methods.js | 1 + 4 files changed, 64 insertions(+) create mode 100644 modules/ticket/back/methods/ticket/itemLackOrigin.js create mode 100644 modules/ticket/back/methods/ticket/specs/itemLackoOrigin.spec.js diff --git a/db/changes/240601/01-updateACLItemLack.sql b/db/changes/240601/01-updateACLItemLack.sql index 90f05e14b..197dea786 100644 --- a/db/changes/240601/01-updateACLItemLack.sql +++ b/db/changes/240601/01-updateACLItemLack.sql @@ -1,5 +1,9 @@ -- Auto-generated SQL script #202401191358 INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) VALUES ('Ticket','itemLack','READ','ALLOW','ROLE','employee'); + INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) VALUES ('Ticket','itemLackDetail','READ','ALLOW','ROLE','employee'); + +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('Ticket','itemLackOrigin','WRITE','ALLOW','ROLE','employee'); diff --git a/modules/ticket/back/methods/ticket/itemLackOrigin.js b/modules/ticket/back/methods/ticket/itemLackOrigin.js new file mode 100644 index 000000000..f720f3d63 --- /dev/null +++ b/modules/ticket/back/methods/ticket/itemLackOrigin.js @@ -0,0 +1,40 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +module.exports = Self => { + Self.remoteMethod('itemLackOrigin', { + description: 'Insert ticket negative into negativeOrigin', + accessType: 'WRITE', + accepts: [{ + arg: 'ctx', + type: 'Object', + http: {source: 'context'} + }, {arg: 'tickets', type: 'array', http: {source: 'body'}}], + returns: + { + type: 'boolean', + root: true + }, + http: { + path: `/itemLack`, + verb: 'POST' + } + }); + + Self.itemLackOrigin = async(ctx, data, options) => { + const myOptions = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + const conn = Self.dataSource.connector; + const stmts = data.map(({itemFk, negativeType, lack}) => + `INSERT INTO vn.negativeOrigin (itemFk, type, quantity) + VALUES (${itemFk}, '${negativeType}', ${lack}) + ON DUPLICATE KEY UPDATE quantity = quantity + VALUES(quantity)`) ?? []; + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql, myOptions); + return result; + }; +}; diff --git a/modules/ticket/back/methods/ticket/specs/itemLackoOrigin.spec.js b/modules/ticket/back/methods/ticket/specs/itemLackoOrigin.spec.js new file mode 100644 index 000000000..901bd12bb --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/itemLackoOrigin.spec.js @@ -0,0 +1,19 @@ +const models = require('vn-loopback/server/server').models; + +describe('Item Lack NegativeOrigin', () => { + it('should return OK', async() => { + const tx = await models.Ticket.beginTransaction({}); + const ctx = {req: {accessToken: {userId: 9}}}; + + const options = {transaction: tx}; + const data = [{itemFk: 1, negativeType: 'FALTAS', lack: 1}, {itemFk: 1, negativeType: 'FALTAS', lack: 2}]; + + await models.Ticket.itemLackOrigin(ctx, data, options); + const query = 'SELECT * FROM vn.negativeOrigin'; + + const negativeOrigin = await models.Application.rawSql(query, null, options); + + expect(negativeOrigin.length).toEqual(1); + expect(negativeOrigin[0].quantity).toEqual(3); + }); +}); diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 687eb8e58..b057e7ffd 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -48,4 +48,5 @@ module.exports = function(Self) { require('../methods/ticket/myLastModified')(Self); require('../methods/ticket/itemLack')(Self); require('../methods/ticket/itemLackDetail')(Self); + require('../methods/ticket/itemLackOrigin')(Self); };