Client Service: Employee List active

This commit is contained in:
Vicente Falco 2017-10-25 13:47:39 +02:00
parent 2eeae6cc36
commit ac8797f24d
5 changed files with 39 additions and 33 deletions

View File

@ -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);
});
};
};

View File

@ -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);
});
});
};
}
};

View File

@ -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);
});
}
}
};
};

View File

@ -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);
}
}
};
};

View File

@ -1,4 +1,3 @@
var vnLoopback = require('../../loopback/server/server.js');
var app = module.exports = vnLoopback.loopback();