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..fd6af2f82 --- /dev/null +++ b/modules/ticket/back/methods/ticket-request/getItemTypeWorker.js @@ -0,0 +1,57 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +module.exports = Self => { + Self.remoteMethodCtx('getItemTypeWorker', { + description: 'Returns the workers that appear in itemType', + accessType: 'READ', + accepts: [{ + arg: 'filter', + type: 'Object', + description: 'Filter defining where and paginated data', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/getItemTypeWorker`, + verb: 'GET' + } + }); + + Self.getItemTypeWorker = async(ctx, filter, options) => { + const myOptions = {}; + const conn = Self.dataSource.connector; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + 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); + + 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); + }; +}; 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..ae5c508b6 --- /dev/null +++ b/modules/ticket/back/methods/ticket-request/specs/getItemTypeWorkers.spec.js @@ -0,0 +1,21 @@ +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 filter = {}; + + const result = await models.TicketRequest.getItemTypeWorker(ctx, filter); + + expect(result.length).toBeGreaterThan(1); + }); +}); 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 @@