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

Draft
jon wants to merge 2 commits from 6242-AddHasRoundingField into dev
6 changed files with 117 additions and 42 deletions
Showing only changes of commit b5284c740b - Show all commits

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 CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems
(INDEX (ticketFk)) (INDEX (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped SELECT f.id ticketFk
FROM tmp.filter f 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]);
stmts.push(stmt); stmts.push(stmt);
stmts.push('CALL ticket_getProblems(FALSE)'); stmts.push('CALL ticket_getProblems(FALSE)');
@ -323,37 +319,13 @@ module.exports = Self => {
if (args.problems != undefined && (!args.from && !args.to)) if (args.problems != undefined && (!args.from && !args.to))
throw new UserError('Choose a date range or days forward'); throw new UserError('Choose a date range or days forward');
let condition; if (typeof args.problems == 'boolean') {
let hasProblem; let condition = 0;
let range; if (args.problems)
let hasWhere; condition = {neq: condition};
switch (args.problems) { stmt.merge(conn.makeWhere({'tp.totalProblems': condition}));
case true:
condition = `or`;
hasProblem = true;
range = {neq: null};
hasWhere = true;
break;
case false:
condition = `and`;
hasProblem = null;
range = null;
hasWhere = true;
break;
} }
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 (filter.order) {
if (typeof filter.order == 'string') filter.order = [filter.order]; if (typeof filter.order == 'string') filter.order = [filter.order];
const index = filter.order.findIndex(o => o.includes('stateFk')); const index = filter.order.findIndex(o => o.includes('stateFk'));

View File

@ -97,14 +97,9 @@ module.exports = Self => {
for (let sale of sales) { for (let sale of sales) {
const problems = saleProblems.get(sale.id); 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) { if (problems) {
sale.itemShortage = problems.itemShortage; for (const problem in problems)
sale.hasTicketRequest = problems.hasTicketRequest; sale[problem] = problems[problem];
sale.hasComponentLack = problems.hasComponentLack;
} }
if (salesWithLogs.includes(sale.id)) if (salesWithLogs.includes(sale.id))
sale.$hasLogs = true; 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

@ -0,0 +1,21 @@
const models = require('vn-loopback/server/server').models;
fdescribe('ticket getTicketProblems()', () => {
const ctx = {req: {accessToken: 9}};
it('should return the problems of a ticket', async() => {
const tx = await models.Ticket.beginTransaction({});
try {
const options = {transaction: tx};
const problems = await models.Ticket.getTicketProblems(ctx, 11, options);
expect(problems[7].totalProblems).toEqual(3);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

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