diff --git a/db/versions/11306-greenMonstera/00-firstScript.sql b/db/versions/11306-greenMonstera/00-firstScript.sql new file mode 100644 index 000000000..c9edc6bfb --- /dev/null +++ b/db/versions/11306-greenMonstera/00-firstScript.sql @@ -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'); diff --git a/modules/ticket/back/methods/ticket/filter.js b/modules/ticket/back/methods/ticket/filter.js index 50481f05b..f0116424a 100644 --- a/modules/ticket/back/methods/ticket/filter.js +++ b/modules/ticket/back/methods/ticket/filter.js @@ -306,11 +306,7 @@ module.exports = Self => { (INDEX (ticketFk)) ENGINE = MEMORY SELECT f.id ticketFk - 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]); + FROM tmp.filter f`); stmts.push(stmt); stmts.push('CALL ticket_getProblems(FALSE)'); @@ -330,34 +326,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 hasWhere; - switch (args.problems) { - case true: - condition = `or`; - hasProblem = true; - hasWhere = true; - break; - - case false: - condition = `and`; - hasProblem = 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.hasItemShortage': hasProblem}, - {'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')); diff --git a/modules/ticket/back/methods/ticket/getSales.js b/modules/ticket/back/methods/ticket/getSales.js index 5b2288d31..3b5ee21a6 100644 --- a/modules/ticket/back/methods/ticket/getSales.js +++ b/modules/ticket/back/methods/ticket/getSales.js @@ -98,14 +98,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; diff --git a/modules/ticket/back/methods/ticket/getTicketProblems.js b/modules/ticket/back/methods/ticket/getTicketProblems.js new file mode 100644 index 000000000..351e07b67 --- /dev/null +++ b/modules/ticket/back/methods/ticket/getTicketProblems.js @@ -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]; + }; +}; diff --git a/modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js b/modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js new file mode 100644 index 000000000..56c3edc1b --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js @@ -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; + } + }); +}); diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 620b3e184..995fa2da3 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -46,4 +46,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); };