From 0fb748719107f0da016cdf3d637e985a8ac9aac5 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 22 Sep 2022 08:32:18 +0200 Subject: [PATCH 1/4] feat: add getItemTypeWorker endpoint --- .../ticket-request/getItemTypeWorker.js | 45 +++++++++++++++++++ .../specs/getItemTypeWorkers.spec.js | 9 ++++ modules/ticket/back/models/ticket-request.js | 1 + .../ticket/front/request/create/index.html | 3 +- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 modules/ticket/back/methods/ticket-request/getItemTypeWorker.js create mode 100644 modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js diff --git a/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js b/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js new file mode 100644 index 000000000..ca559f853 --- /dev/null +++ b/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js @@ -0,0 +1,45 @@ +module.exports = Self => { + Self.remoteMethodCtx('getItemTypeWorker', { + description: 'Returns the workers that appear in itemType', + accessType: 'READ', + accepts: [], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/getItemTypeWorker`, + verb: 'GET' + } + }); + + Self.getItemTypeWorker = async(ctx, options) => { + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const query = + `SELECT DISTINCT w.id, w.firstName, w.lastName, u.name, u.nickname + FROM itemType it + JOIN worker w ON w.id = it.workerFk + JOIN account.user u ON u.id = w.id`; + + const result = await Self.rawSql(query); + + if (tx) await tx.commit(); + + return result; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js b/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js new file mode 100644 index 000000000..d8791b0ad --- /dev/null +++ b/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js @@ -0,0 +1,9 @@ +const models = require('vn-loopback/server/server').models; + +describe('ticket-request getItemTypeWorker()', () => { + it('should return the workers at itemType as result', async() => { + const result = await models.TicketRequest.getItemTypeWorker(); + + expect(result.length).toEqual(2); + }); +}); diff --git a/modules/ticket/back/models/ticket-request.js b/modules/ticket/back/models/ticket-request.js index 234978f33..4125126dc 100644 --- a/modules/ticket/back/models/ticket-request.js +++ b/modules/ticket/back/models/ticket-request.js @@ -5,6 +5,7 @@ module.exports = function(Self) { require('../methods/ticket-request/filter')(Self); require('../methods/ticket-request/deny')(Self); require('../methods/ticket-request/confirm')(Self); + require('../methods/ticket-request/getItemTypeWorker')(Self); Self.observe('before save', async function(ctx) { if (ctx.isNewInstance) { diff --git a/modules/ticket/front/request/create/index.html b/modules/ticket/front/request/create/index.html index 8d25358b5..71a2de260 100644 --- a/modules/ticket/front/request/create/index.html +++ b/modules/ticket/front/request/create/index.html @@ -18,9 +18,8 @@ From 95e5b1a970048a7c5be408ba323e567d9abf90d7 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 22 Sep 2022 13:54:58 +0200 Subject: [PATCH 2/4] fix: code for search-function --- .../ticket-request/getItemTypeWorker.js | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js b/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js index ca559f853..e635f61a9 100644 --- a/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js +++ b/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js @@ -1,8 +1,15 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + module.exports = Self => { Self.remoteMethodCtx('getItemTypeWorker', { description: 'Returns the workers that appear in itemType', accessType: 'READ', - accepts: [], + accepts: [{ + arg: 'filter', + type: 'Object', + description: 'Filter defining where and paginated data', + required: true + }], returns: { type: ['object'], root: true @@ -13,8 +20,9 @@ module.exports = Self => { } }); - Self.getItemTypeWorker = async(ctx, options) => { + Self.getItemTypeWorker = async(ctx, filter, options) => { const myOptions = {}; + const conn = Self.dataSource.connector; let tx; if (typeof options == 'object') @@ -32,11 +40,25 @@ module.exports = Self => { JOIN worker w ON w.id = it.workerFk JOIN account.user u ON u.id = w.id`; - const result = await Self.rawSql(query); + let stmt = new ParameterizedSQL(query); + + if (filter.where) { + const value = filter.where.firstName; + const myFilter = { + where: {or: [ + {'w.firstName': {like: `%${value}%`}}, + {'w.lastName': {like: `%${value}%`}}, + {'u.name': {like: `%${value}%`}}, + {'u.nickname': {like: `%${value}%`}} + ]} + }; + + stmt.merge(conn.makeSuffix(myFilter)); + } if (tx) await tx.commit(); - return result; + return conn.executeStmt(stmt); } catch (e) { if (tx) await tx.rollback(); throw e; From a486b1593b732cf3984317df7167b1b3d669bc60 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 22 Sep 2022 13:55:09 +0200 Subject: [PATCH 3/4] fix: backTest --- .../specs/getItemTypeWorkers.spec.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js b/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js index d8791b0ad..9cb58e006 100644 --- a/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js +++ b/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js @@ -1,8 +1,20 @@ const models = require('vn-loopback/server/server').models; describe('ticket-request getItemTypeWorker()', () => { + const ctx = {req: {accessToken: {userId: 18}}}; + + it('should return the buyer as result', async() => { + const filter = {where: {firstName: 'buyer'}}; + + const result = await models.TicketRequest.getItemTypeWorker(ctx, filter); + + expect(result.length).toEqual(1); + }); + it('should return the workers at itemType as result', async() => { - const result = await models.TicketRequest.getItemTypeWorker(); + const filter = {}; + + const result = await models.TicketRequest.getItemTypeWorker(ctx, filter); expect(result.length).toEqual(2); }); From 5c76437498a2ecc074991e62be99a69ac7d7193a Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 4 Oct 2022 08:19:09 +0200 Subject: [PATCH 4/4] refator: delete unnecessary transaction --- .../ticket-request/getItemTypeWorker.js | 46 ++++++++----------- .../specs/getItemTypeWorkers.spec.js | 2 +- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js b/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js index e635f61a9..fd6af2f82 100644 --- a/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js +++ b/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js @@ -28,40 +28,30 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const query = - `SELECT DISTINCT w.id, w.firstName, w.lastName, u.name, u.nickname + const query = + `SELECT DISTINCT u.nickname FROM itemType it JOIN worker w ON w.id = it.workerFk JOIN account.user u ON u.id = w.id`; - let stmt = new ParameterizedSQL(query); + let stmt = new ParameterizedSQL(query); - if (filter.where) { - const value = filter.where.firstName; - const myFilter = { - where: {or: [ - {'w.firstName': {like: `%${value}%`}}, - {'w.lastName': {like: `%${value}%`}}, - {'u.name': {like: `%${value}%`}}, - {'u.nickname': {like: `%${value}%`}} - ]} - }; + if (filter.where) { + const value = filter.where.firstName; + const myFilter = { + where: {or: [ + {'w.firstName': {like: `%${value}%`}}, + {'w.lastName': {like: `%${value}%`}}, + {'u.name': {like: `%${value}%`}}, + {'u.nickname': {like: `%${value}%`}} + ]} + }; - stmt.merge(conn.makeSuffix(myFilter)); - } - - if (tx) await tx.commit(); - - return conn.executeStmt(stmt); - } catch (e) { - if (tx) await tx.rollback(); - throw e; + stmt.merge(conn.makeSuffix(myFilter)); } + + if (tx) await tx.commit(); + + return conn.executeStmt(stmt); }; }; diff --git a/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js b/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js index 9cb58e006..ae5c508b6 100644 --- a/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js +++ b/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js @@ -16,6 +16,6 @@ describe('ticket-request getItemTypeWorker()', () => { const result = await models.TicketRequest.getItemTypeWorker(ctx, filter); - expect(result.length).toEqual(2); + expect(result.length).toBeGreaterThan(1); }); });