From ac8797f24d27b8d4433f6c453d745112189c53b9 Mon Sep 17 00:00:00 2001 From: Vicente Falco Date: Wed, 25 Oct 2017 13:47:39 +0200 Subject: [PATCH] Client Service: Employee List active --- .../client/common/methods/client/employee.js | 38 ++++++++++++------- .../common/methods/ticket/change-time.js | 10 ++--- .../common/methods/ticket/change-worker.js | 9 ++--- .../production/common/methods/ticket/list.js | 14 +++---- services/production/server/server.js | 1 - 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/services/client/common/methods/client/employee.js b/services/client/common/methods/client/employee.js index 40a6e19d3..8ad6dbbaa 100644 --- a/services/client/common/methods/client/employee.js +++ b/services/client/common/methods/client/employee.js @@ -13,20 +13,30 @@ module.exports = function(Client) { } }); - Client.employeeList = function(cb) { - var include = {include: {relation: 'user'}, where: {userFk: {neq: null}}}; - Client.app.models.Employee.find(include, function(err, instances) { - if (err) - cb(err, null); - cb(null, generateEmployees(instances)); - }); + let getEmployees = listEmployees => { + let employees = []; + listEmployees.forEach(function(e) { + employees.push({id: e.id, name: e.name}); + }, this); + return employees; }; - function generateEmployees(instances) { - var emps = []; - instances.forEach(function(e) { - emps.push({id: e.id, name: e.user().name}); - }, this); - return emps; - } + Client.employeeList = function(callback) { + let query = `SELECT em.id, CASE em.surname WHEN NULL THEN em.name ELSE concat(em.name, " ", em.surname) END \`name\` + FROM Employee em + JOIN Account ac ON em.userFk = ac.id + JOIN RoleMapping rm on ac.id=rm.principalId + JOIN Role rl on rm.roleId = rl.id + WHERE ac.active + and rl.\`name\`='employee' + ORDER BY em.name ASC`; + + Client.rawSql(query, [], callback) + .then(response => { + callback(null, getEmployees(response)); + }) + .catch(reject => { + callback(reject, null); + }); + }; }; diff --git a/services/production/common/methods/ticket/change-time.js b/services/production/common/methods/ticket/change-time.js index d763d029d..3f6f13c4a 100644 --- a/services/production/common/methods/ticket/change-time.js +++ b/services/production/common/methods/ticket/change-time.js @@ -17,7 +17,6 @@ module.exports = function(Ticket) { path: '/:time/changeTime' } }); - Ticket.changeTime = function(ctx, time, cb) { var tickets = ctx.req.body.tickets; var FakeProduction = Ticket.app.models.FakeProduction; @@ -25,15 +24,14 @@ module.exports = function(Ticket) { var query = `update Ticket set date = CONCAT(DATE(date), ' ', ?) where id in (?)`; var params = [hour, tickets]; - + FakeProduction.updateAll({ticketFk: {inq: tickets}}, {hour: hour}, function(err, res){ - if(err) - cb(err, null) + if (err) + cb(err, null); else Ticket.rawSql(query, params, cb).then(function(response) { cb(null, response); }); }); - }; -} \ No newline at end of file +}; diff --git a/services/production/common/methods/ticket/change-worker.js b/services/production/common/methods/ticket/change-worker.js index c6572a786..c1fcea080 100644 --- a/services/production/common/methods/ticket/change-worker.js +++ b/services/production/common/methods/ticket/change-worker.js @@ -8,7 +8,7 @@ module.exports = function(Ticket) { required: true, description: 'worker id', http: {source: 'path'} - }, + } ], returns: { arg: 'response', @@ -25,12 +25,11 @@ module.exports = function(Ticket) { changeWorker(worker, tickets, cb); }; - var changeWorker = function(worker, tickets, cb){ - var inserts = []; + let changeWorker = function(worker, tickets, cb) { var FakeProduction = Ticket.app.models.FakeProduction; FakeProduction.updateAll({ticketFk: {inq: tickets}}, {workerFk: worker}, function(err, info){ (err) ? cb(err, null) : cb(null, info); }); - } -} \ No newline at end of file + }; +}; diff --git a/services/production/common/methods/ticket/list.js b/services/production/common/methods/ticket/list.js index c465dffec..34178c8c0 100644 --- a/services/production/common/methods/ticket/list.js +++ b/services/production/common/methods/ticket/list.js @@ -1,7 +1,7 @@ module.exports = function(Ticket) { Ticket.remoteMethod('list', { description: 'List tickets for production', - /*accepts: { + /* accepts: { arg: 'id', type: 'number', required: true, @@ -19,7 +19,7 @@ module.exports = function(Ticket) { }); Ticket.list = function(cb) { - //list(); + // list(); }; var list = function(){ @@ -27,11 +27,11 @@ module.exports = function(Ticket) { var query = "CALL production_control_source(?, ?)" - var cb = function(error, res){ - if(error) console.log(error); + var cb = function(error, res) { + if (error) console.log(error); else console.log(res); - }; + }; Ticket.rawSql(query, params, cb); - } -} \ No newline at end of file + }; +}; diff --git a/services/production/server/server.js b/services/production/server/server.js index 1f10f7f51..f59b8971e 100644 --- a/services/production/server/server.js +++ b/services/production/server/server.js @@ -1,4 +1,3 @@ - var vnLoopback = require('../../loopback/server/server.js'); var app = module.exports = vnLoopback.loopback();