From 0fb748719107f0da016cdf3d637e985a8ac9aac5 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 22 Sep 2022 08:32:18 +0200 Subject: [PATCH] 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 @@