refs #6321 feat: new split method
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2024-03-27 14:09:25 +01:00
parent d8d0ced918
commit e6fe245b27
4 changed files with 87 additions and 2 deletions

View File

@ -2,5 +2,6 @@ INSERT INTO salix.ACL (model,property,accessType,permission,principalType,princi
VALUES
('Ticket','itemLack','READ','ALLOW','ROLE','employee'),
('Ticket','itemLackDetail','READ','ALLOW','ROLE','employee'),
('Ticket','itemLackOrigin','READ','ALLOW','ROLE','employee'),
('Ticket','itemLackOrigin','WRITE','ALLOW','ROLE','employee'),
('Ticket','split','WRITE','ALLOW','ROLE','employee'),
('Ticket','negativeOrigin','READ','ALLOW','ROLE','employee');

View File

@ -15,7 +15,7 @@ module.exports = Self => {
root: true
},
http: {
path: `/itemLack`,
path: `/itemLackOrigin`,
verb: 'POST'
}
});

View File

@ -0,0 +1,83 @@
const {ParameterizedSQL} = require('loopback-connector/lib/sql');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('split', {
description: 'Split a ticket or n tickets',
accessType: 'WRITE',
accepts: [
{
type: ['Object'],
required: true,
http: {source: 'body'}
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/split`,
verb: 'POST'
}
});
Self.split = async(ctx, tickets, options) => {
// const models = Self.app.models;
const myOptions = {};
let tx;
let results = [];
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 = [];
try {
const ticketsIds = tickets.map(({id}, index) => id);
const ticketsCount = await Self.rawSql(`
Select t.id tid, s.id sid, count(s.id) count from
vn.ticket t
LEFT JOIN vn.sale s
ON s.ticketFk = t.id
WHERE t.id IN (?) GROUP BY t.id;`,
[ticketsIds], myOptions);
console.log(ticketsCount);
// stmts.push(stmt);
// const sql = ParameterizedSQL.join(stmts, ';');
// const result = await conn.executeStmt(sql, myOptions);
for (const {tid, sid, count} of ticketsCount) {
try {
if (count === 1) {
results.push({ticket: tid, message: 'noSplit'});
continue;
}
const [, [{vNewTicket}]] = await Self.rawSql(`
CALL vn.ticket_clone(?, @vNewTicket);
SELECT @vNewTicket vNewTicket;`,
[tid], myOptions);
if (vNewTicket === 0) continue;
await Self.rawSql(`
UPDATE vn.sale SET isPicked = (id = ?) WHERE ticketFk = ?`,
[sid, tid], myOptions);
await Self.transferSales(ctx, tid, vNewTicket, sid, myOptions);
await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [tid, 'FIXING'], myOptions);
results.push({ticket: tid, message: 'split'});
} catch (error) {
throw new UserError('You cannot close tickets for today');
}
}
return results;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

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