refs #6321 feat: negativeOrigin method

This commit is contained in:
Javier Segarra 2024-03-15 09:32:36 +01:00
parent 6a12af2eb9
commit f83f7808c8
4 changed files with 64 additions and 0 deletions

View File

@ -1,5 +1,9 @@
-- Auto-generated SQL script #202401191358 -- Auto-generated SQL script #202401191358
INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId)
VALUES ('Ticket','itemLack','READ','ALLOW','ROLE','employee'); VALUES ('Ticket','itemLack','READ','ALLOW','ROLE','employee');
INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId)
VALUES ('Ticket','itemLackDetail','READ','ALLOW','ROLE','employee'); VALUES ('Ticket','itemLackDetail','READ','ALLOW','ROLE','employee');
INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId)
VALUES ('Ticket','itemLackOrigin','WRITE','ALLOW','ROLE','employee');

View File

@ -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;
};
};

View File

@ -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);
});
});

View File

@ -48,4 +48,5 @@ module.exports = function(Self) {
require('../methods/ticket/myLastModified')(Self); require('../methods/ticket/myLastModified')(Self);
require('../methods/ticket/itemLack')(Self); require('../methods/ticket/itemLack')(Self);
require('../methods/ticket/itemLackDetail')(Self); require('../methods/ticket/itemLackDetail')(Self);
require('../methods/ticket/itemLackOrigin')(Self);
}; };