From b5284c740b52163f81a1be02b74e696f1a7f8890 Mon Sep 17 00:00:00 2001 From: Jon Date: Thu, 17 Oct 2024 07:48:53 +0200 Subject: [PATCH 1/2] feat: refs #6242 added new back and test to get ticket problems --- .../11306-greenMonstera/00-firstScript.sql | 3 + modules/ticket/back/methods/ticket/filter.js | 42 ++-------- .../ticket/back/methods/ticket/getSales.js | 9 +- .../back/methods/ticket/getTicketProblems.js | 83 +++++++++++++++++++ .../ticket/specs/getTicketProblems.spec.js | 21 +++++ modules/ticket/back/models/ticket-methods.js | 1 + 6 files changed, 117 insertions(+), 42 deletions(-) create mode 100644 db/versions/11306-greenMonstera/00-firstScript.sql create mode 100644 modules/ticket/back/methods/ticket/getTicketProblems.js create mode 100644 modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js 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 06781c3c8..5197eb2c8 100644 --- a/modules/ticket/back/methods/ticket/filter.js +++ b/modules/ticket/back/methods/ticket/filter.js @@ -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')); diff --git a/modules/ticket/back/methods/ticket/getSales.js b/modules/ticket/back/methods/ticket/getSales.js index e721d90ae..220b90d37 100644 --- a/modules/ticket/back/methods/ticket/getSales.js +++ b/modules/ticket/back/methods/ticket/getSales.js @@ -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; 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 7fe968b26..3e495ce31 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -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); }; -- 2.40.1 From b844f8fdb41f0126df0c24223defdb999da9213c Mon Sep 17 00:00:00 2001 From: Jon Date: Thu, 17 Oct 2024 07:52:29 +0200 Subject: [PATCH 2/2] fix: refs #6242 deleted test --- .../ticket/specs/getTicketProblems.spec.js | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js diff --git a/modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js b/modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js deleted file mode 100644 index 56c3edc1b..000000000 --- a/modules/ticket/back/methods/ticket/specs/getTicketProblems.spec.js +++ /dev/null @@ -1,21 +0,0 @@ -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; - } - }); -}); -- 2.40.1