Merge branch 'dev' of https://git.verdnatura.es/salix into dev

* 'dev' of https://git.verdnatura.es/salix:
  validation client credit - refact
  validate client credit
  AutoLoad items with "scroll" (1st version)
This commit is contained in:
Carlos 2017-10-04 08:47:28 +02:00
commit 9f7170f773
2 changed files with 42 additions and 31 deletions

View File

@ -56,6 +56,10 @@ export default class DropDown {
} }
this.$timeout(() => { this.$timeout(() => {
this._activeOption = value; this._activeOption = value;
// AutoLoad items with "scroll" (1st version):
if (value && value >= this.items.length - 3) {
this.loadItems();
}
}); });
} }

View File

@ -2,7 +2,7 @@ var app = require('../../server/server');
module.exports = function(Client) { module.exports = function(Client) {
var models = app.models; var models = app.models;
var loopBackContext = require('loopback-context');
// Methods // Methods
require('../methods/client/activate.js')(Client); require('../methods/client/activate.js')(Client);
@ -69,20 +69,22 @@ module.exports = function(Client) {
done(); done();
}); });
} }
/*
Client.validateAsync('credit', validateCredit, { Client.validateAsync('credit', validateCredit, {
message: 'No tienes privilegios para modificar el crédito' message: 'No tienes privilegios para modificar el crédito'
}); });
function validateCredit(err, done) { function validateCredit(err, done) {
// FIXME: Id del usuario actual let ctx = loopBackContext.getCurrentContext();
let userId = 1; let accessToken = ctx && ctx.get('accessToken');
let userId = accessToken.userId;
let self = this;
// Comprueba si el rol del usuario puede asignar esa cantidad // Comprueba si el rol del usuario puede asignar esa cantidad
// para ello mira que roles pueden asignar la cantidad que el usuario ha indicado
let filter = { let filter = {
fields: ['roleFk'], fields: ['roleFk'],
where: { where: {
maxAmount: {gt: this.credit} maxAmount: {gt: self.credit}
} }
}; };
models.ClientCreditLimit.find(filter, models.ClientCreditLimit.find(filter,
@ -102,47 +104,52 @@ module.exports = function(Client) {
(_, res) => roleCb(_, res)); (_, res) => roleCb(_, res));
} }
function roleCb(_, count) { function roleCb(_, count) {
//si el usuario no tiene alguno de los roles no continua
if (!(count > 0)) { if (!(count > 0)) {
err(); err();
done(); done();
} else } else
validate(); validate(); //si tiene el rol hay que validar que el último movimiento no fuese crédito 0 insertado por gerencia
} }
// Si se puso a 0 por gerencia, solo gerencia puede aumentarlo // Si se puso a 0 por gerencia, solo gerencia puede aumentarlo
function validate() { function validate() {
let query = 'SELECT MAX(created) created FROM ClientCredit WHERE clientFk = ?'; let query = 'SELECT * FROM ClientCredit WHERE clientFk = ? ORDER BY created DESC LIMIT 1';
Client.dataSource.connector.execute (query, [this.id], Client.dataSource.connector.execute (query, [self.id],
(_, res) => maxCb(_, res)); (_, res) => maxCb(_, res));
} }
function maxCb(_, instances) { function maxCb(_, instances) {
if (instances.length !== 1) { //console.log('maxCb', instances);
done(); if(!instances){
err();
return; return;
} }
let filter = { if (instances.length !== 1 || instances[0].employeeFk == userId || instances[0].amount > 0) {
fields: ['amount', 'employeeFk', 'employee'], done();
where: { return;
clientFk: this.id, }
created: instances[0].created
}, //el ultimo registro tiene valor 0, hay que comprobar que no fue editado por un gerente
include: { let sql = `SELECT count(distinct r.id) as hasManagerRole
relation: 'employee', FROM ClientCredit cc
scope: { JOIN Employee em ON (em.id = cc.employeeFk)
fields: ['userFk'] JOIN Account ac ON (ac.id = em.userFk)
} JOIN RoleMapping rm ON (rm.principalId = ac.id)
} JOIN Role r on (r.id = rm.roleId)
}; WHERE rm.principalType = 'USER'
models.ClientCredit.findOne(filter, AND cc.employeeFk = ${instances[0].employeeFk}
(_, res) => clientCreditCb(_, res)); AND r.\`name\` = 'manager'`;
Client.dataSource.connector.execute(sql, [], (_, res) => clientCreditCb(_, res));
} }
function clientCreditCb(_, instance) {
if (instance.amount == 0 && instance.employee.userFk != userId) function clientCreditCb(_, instance) {
if (!instance || (instance.length && instance[0].hasManagerRole > 0 ))
err(); err();
done(); done();
} }
} }
*/
}; };