WIP: #6242 added new back and test to get ticket problems #3116

Draft
jon wants to merge 2 commits from 6242-AddHasRoundingField into dev
5 changed files with 96 additions and 42 deletions

View File

@ -0,0 +1,3 @@
-- Place your SQL code here
INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId)
VALUES ('Ticket','getTicketProblems','READ','ALLOW','ROLE','employee');

View File

@ -298,12 +298,8 @@ module.exports = Self => {
CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems
(INDEX (ticketFk))
ENGINE = MEMORY
SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped
FROM tmp.filter f
LEFT JOIN alertLevel al ON al.id = f.alertLevel
WHERE (al.code = 'FREE' OR f.alertLevel IS NULL)
AND f.shipped >= ?
`, [date]);
SELECT f.id ticketFk
FROM tmp.filter f`);
stmts.push(stmt);
stmts.push('CALL ticket_getProblems(FALSE)');
@ -323,37 +319,13 @@ module.exports = Self => {
if (args.problems != undefined && (!args.from && !args.to))
throw new UserError('Choose a date range or days forward');
let condition;
let hasProblem;
let range;
let hasWhere;
switch (args.problems) {
case true:
condition = `or`;
hasProblem = true;
range = {neq: null};
hasWhere = true;
break;
case false:
condition = `and`;
hasProblem = null;
range = null;
hasWhere = true;
break;
if (typeof args.problems == 'boolean') {
let condition = 0;
if (args.problems)
condition = {neq: condition};
stmt.merge(conn.makeWhere({'tp.totalProblems': condition}));
}
const problems = {[condition]: [
{'tp.isFreezed': hasProblem},
{'tp.hasRisk': hasProblem},
{'tp.hasTicketRequest': hasProblem},
{'tp.itemShortage': range},
{'tp.hasRounding': hasProblem}
]};
if (hasWhere)
stmt.merge(conn.makeWhere(problems));
if (filter.order) {
if (typeof filter.order == 'string') filter.order = [filter.order];
const index = filter.order.findIndex(o => o.includes('stateFk'));

View File

@ -97,14 +97,9 @@ module.exports = Self => {
for (let sale of sales) {
const problems = saleProblems.get(sale.id);
const itemStock = itemAvailable.get(sale.itemFk);
sale.available = itemStock.available;
sale.visible = itemStock.visible;
sale.claim = claimedSales.get(sale.id);
if (problems) {
sale.itemShortage = problems.itemShortage;
sale.hasTicketRequest = problems.hasTicketRequest;
sale.hasComponentLack = problems.hasComponentLack;
for (const problem in problems)
sale[problem] = problems[problem];
}
if (salesWithLogs.includes(sale.id))
sale.$hasLogs = true;

View File

@ -0,0 +1,83 @@
const {buildFilter} = require('vn-loopback/util/filter');
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethodCtx('getTicketProblems', {
description: 'Get problems for a ticket',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/:id/getTicketProblems`,
verb: 'get'
}
});
Self.getTicketProblems = async(ctx, id, options) => {
const myOptions = {};
const stmts = [];
const conn = Self.dataSource.connector;
let stmt;
const ticketId = id;
const where = buildFilter(ctx.args, param => {
switch (param) {
case 'id':
return {'t.id': ticketId};
}
});
if (typeof options == 'object')
Object.assign(myOptions, options);
stmt = new ParameterizedSQL(`
CREATE OR REPLACE TEMPORARY TABLE tmp.filter
(INDEX (id))
ENGINE = MEMORY
SELECT t.id
FROM ticket t
`);
stmt.merge(conn.makeWhere(where));
stmts.push(stmt);
stmt = new ParameterizedSQL(`
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX (ticketFk))
ENGINE = MEMORY
SELECT f.id AS ticketFk
FROM tmp.filter f
`);
stmts.push(stmt);
stmts.push('CALL ticket_getProblems(FALSE)');
stmt = new ParameterizedSQL(`
SELECT f.*, tp.*
FROM tmp.filter f
LEFT JOIN tmp.ticketProblems tp ON tp.ticketFk = f.id
`);
const ticketsIndex = stmts.push(stmt) - 1;
stmts.push(`
DROP TEMPORARY TABLE IF EXISTS
tmp.filter,
tmp.ticket,
tmp.ticketProblems
`);
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
return result[ticketsIndex];
};
};

View File

@ -45,4 +45,5 @@ module.exports = function(Self) {
require('../methods/ticket/docuwareDownload')(Self);
require('../methods/ticket/myLastModified')(Self);
require('../methods/ticket/setWeight')(Self);
require('../methods/ticket/getTicketProblems')(Self);
};