Correcciones varias
This commit is contained in:
parent
82e4fdab68
commit
67829f5694
|
@ -3,4 +3,4 @@ var vnLoopback = require('../../loopback/server/server.js');
|
||||||
|
|
||||||
var app = module.exports = vnLoopback.loopback();
|
var app = module.exports = vnLoopback.loopback();
|
||||||
app.set('applications', require('./application.json'));
|
app.set('applications', require('./application.json'));
|
||||||
vnLoopback.boot(app, __dirname);
|
vnLoopback.boot(app, __dirname, module);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
module.exports = function(Self) {
|
module.exports = function(Self) {
|
||||||
|
|
||||||
let loopBackContext = require('loopback-context');
|
let loopBackContext = require('loopback-context');
|
||||||
|
|
||||||
Self.validate('text', isEnabled, {message: 'Se debe rellenar el campo de texto'});
|
Self.validate('text', isEnabled, {message: 'Se debe rellenar el campo de texto'});
|
||||||
function isEnabled(err) {
|
function isEnabled(err) {
|
||||||
if (!this.text) err();
|
if (!this.text) err();
|
||||||
|
@ -9,15 +8,15 @@ module.exports = function(Self) {
|
||||||
|
|
||||||
Self.observe('before save', function(ctx, next) {
|
Self.observe('before save', function(ctx, next) {
|
||||||
ctx.instance.created = Date();
|
ctx.instance.created = Date();
|
||||||
let currentUser = loopBackContext.getCurrentContext();
|
let cctx = loopBackContext.getCurrentContext();
|
||||||
let userId = currentUser.get('currentUser');
|
let userId = cctx.get('currentUser');
|
||||||
let app = require('../../server/server');
|
let app = require('../../server/server');
|
||||||
let Employee = app.models.Employee;
|
let Employee = app.models.Employee;
|
||||||
Employee.findOne({where: {userFk: userId}}, function (err, user){
|
Employee.findOne({where: {userFk: userId}}, function (err, user){
|
||||||
if (user){
|
if (user) {
|
||||||
ctx.instance.employeeFk = user.id;
|
ctx.instance.employeeFk = user.id;
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,7 +15,7 @@ module.exports = function(Self) {
|
||||||
require('../methods/client/roles.js')(Self);
|
require('../methods/client/roles.js')(Self);
|
||||||
require('../methods/client/salesperson.js')(Self);
|
require('../methods/client/salesperson.js')(Self);
|
||||||
require('../methods/client/addressesPropagateRe.js')(Self);
|
require('../methods/client/addressesPropagateRe.js')(Self);
|
||||||
|
|
||||||
// Validations
|
// Validations
|
||||||
|
|
||||||
Self.validatesUniquenessOf('name', {
|
Self.validatesUniquenessOf('name', {
|
||||||
|
@ -40,7 +40,7 @@ module.exports = function(Self) {
|
||||||
message: 'Correo electrónico inválido',
|
message: 'Correo electrónico inválido',
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
allowBlank: true,
|
allowBlank: true,
|
||||||
with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w(,[\w|\.|\-]+@\w[\w|\.|\-]*\w)*$/
|
with: /^[\w|.|-]+@\w[\w|.|-]*\w(,[\w|.|-]+@\w[\w|.|-]*\w)*$/
|
||||||
});
|
});
|
||||||
Self.validatesLengthOf('postcode', {
|
Self.validatesLengthOf('postcode', {
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
|
@ -49,15 +49,15 @@ module.exports = function(Self) {
|
||||||
});
|
});
|
||||||
|
|
||||||
var validateIban = require('../validations/validateIban');
|
var validateIban = require('../validations/validateIban');
|
||||||
Self.validateBinded('iban',validateIban,{
|
Self.validateBinded('iban', validateIban, {
|
||||||
message:'El iban no tiene el formato correcto'
|
message: 'El iban no tiene el formato correcto'
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.validate('payMethod', hasSalesMan, {
|
Self.validate('payMethod', hasSalesMan, {
|
||||||
message: 'No se puede cambiar la forma de pago si no hay comercial asignado'
|
message: 'No se puede cambiar la forma de pago si no hay comercial asignado'
|
||||||
});
|
});
|
||||||
function hasSalesMan(err) {
|
function hasSalesMan(err) {
|
||||||
if(this.payMethod && !this.salesPerson)
|
if (this.payMethod && !this.salesPerson)
|
||||||
err();
|
err();
|
||||||
}
|
}
|
||||||
Self.validateAsync('payMethodFk', hasIban, {
|
Self.validateAsync('payMethodFk', hasIban, {
|
||||||
|
@ -76,10 +76,9 @@ module.exports = function(Self) {
|
||||||
});
|
});
|
||||||
function validateCredit(err, done) {
|
function validateCredit(err, done) {
|
||||||
let ctx = loopBackContext.getCurrentContext();
|
let ctx = loopBackContext.getCurrentContext();
|
||||||
let accessToken = ctx && ctx.get('accessToken');
|
let userId = ctx && ctx.get('currentUser');
|
||||||
let userId = accessToken.userId;
|
|
||||||
let self = this;
|
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
|
// para ello mira que roles pueden asignar la cantidad que el usuario ha indicado
|
||||||
let filter = {
|
let filter = {
|
||||||
|
@ -94,7 +93,7 @@ module.exports = function(Self) {
|
||||||
function limitCb(_, instances) {
|
function limitCb(_, instances) {
|
||||||
let requiredRoles = [];
|
let requiredRoles = [];
|
||||||
for (instance of instances)
|
for (instance of instances)
|
||||||
requiredRoles.push (instance.roleFk);
|
requiredRoles.push(instance.roleFk);
|
||||||
|
|
||||||
let where = {
|
let where = {
|
||||||
roleId: {inq: requiredRoles},
|
roleId: {inq: requiredRoles},
|
||||||
|
@ -105,24 +104,24 @@ module.exports = function(Self) {
|
||||||
(_, res) => roleCb(_, res));
|
(_, res) => roleCb(_, res));
|
||||||
}
|
}
|
||||||
function roleCb(_, count) {
|
function roleCb(_, count) {
|
||||||
//si el usuario no tiene alguno de los roles no continua
|
// Si el usuario no tiene alguno de los roles no continua
|
||||||
if (!(count > 0)) {
|
if (!(count > 0)) {
|
||||||
err();
|
err();
|
||||||
done();
|
done();
|
||||||
} else
|
} else
|
||||||
validate(); //si tiene el rol hay que validar que el último movimiento no fuese crédito 0 insertado por gerencia
|
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 * FROM ClientCredit WHERE clientFk = ? ORDER BY created DESC LIMIT 1';
|
let query = 'SELECT * FROM ClientCredit WHERE clientFk = ? ORDER BY created DESC LIMIT 1';
|
||||||
Self.dataSource.connector.execute (query, [self.id],
|
Self.dataSource.connector.execute(query, [self.id],
|
||||||
(_, res) => maxCb(_, res));
|
(_, res) => maxCb(_, res));
|
||||||
}
|
}
|
||||||
|
|
||||||
function maxCb(_, instances) {
|
function maxCb(_, instances) {
|
||||||
//console.log('maxCb', instances);
|
// console.log('maxCb', instances);
|
||||||
if(!instances){
|
if (!instances) {
|
||||||
err();
|
err();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -131,8 +130,8 @@ module.exports = function(Self) {
|
||||||
done();
|
done();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//el ultimo registro tiene valor 0, hay que comprobar que no fue editado por un gerente
|
// El ultimo registro tiene valor 0, hay que comprobar que no fue editado por un gerente
|
||||||
let sql = `SELECT count(distinct r.id) as hasManagerRole
|
let sql = `SELECT count(distinct r.id) as hasManagerRole
|
||||||
FROM ClientCredit cc
|
FROM ClientCredit cc
|
||||||
JOIN Employee em ON (em.id = cc.employeeFk)
|
JOIN Employee em ON (em.id = cc.employeeFk)
|
||||||
|
@ -146,11 +145,10 @@ module.exports = function(Self) {
|
||||||
Self.dataSource.connector.execute(sql, [], (_, res) => clientCreditCb(_, res));
|
Self.dataSource.connector.execute(sql, [], (_, res) => clientCreditCb(_, res));
|
||||||
}
|
}
|
||||||
|
|
||||||
function clientCreditCb(_, instance) {
|
function clientCreditCb(_, instance) {
|
||||||
if (!instance || (instance.length && instance[0].hasManagerRole > 0 ))
|
if (!instance || (instance.length && instance[0].hasManagerRole > 0))
|
||||||
err();
|
err();
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
};
|
|
||||||
|
|
|
@ -11,8 +11,5 @@
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.verdnatura.es/salix"
|
"url": "https://git.verdnatura.es/salix"
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"loopback-context": "^3.3.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,4 +2,4 @@
|
||||||
var vnLoopback = require('../../loopback/server/server.js');
|
var vnLoopback = require('../../loopback/server/server.js');
|
||||||
|
|
||||||
var app = module.exports = vnLoopback.loopback();
|
var app = module.exports = vnLoopback.loopback();
|
||||||
vnLoopback.boot(app, __dirname);
|
vnLoopback.boot(app, __dirname, module);
|
||||||
|
|
|
@ -1,13 +1,5 @@
|
||||||
var cors = require('cors');
|
var cors = require('cors');
|
||||||
|
|
||||||
var whitelist = ['http://localhost:8080'];
|
|
||||||
var corsOptions = {
|
|
||||||
origin: function(origin, callback) {
|
|
||||||
var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
|
|
||||||
callback(originIsWhitelisted ? null : 'Bad Request', originIsWhitelisted);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = function() {
|
module.exports = function() {
|
||||||
return cors({origin: true});
|
return cors({origin: true});
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,7 +10,7 @@ module.exports = {
|
||||||
boot: vnBoot
|
boot: vnBoot
|
||||||
};
|
};
|
||||||
|
|
||||||
function vnBoot(app, rootDir, cb) {
|
function vnBoot(app, rootDir, rootModule) {
|
||||||
// Internationalization
|
// Internationalization
|
||||||
|
|
||||||
let i18nDir = rootDir + '/i18n';
|
let i18nDir = rootDir + '/i18n';
|
||||||
|
@ -22,7 +22,7 @@ function vnBoot(app, rootDir, cb) {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/prueba', function(req, res) {
|
app.get('/prueba', function(req, res) {
|
||||||
i18n.setLocale(req.get('Accept-Language').substring(0,2));
|
i18n.setLocale(req.get('Accept-Language').substring(0, 2));
|
||||||
res.send(i18n.__('Hello'));
|
res.send(i18n.__('Hello'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -63,31 +63,28 @@ function vnBoot(app, rootDir, cb) {
|
||||||
modelSources: [
|
modelSources: [
|
||||||
"loopback/common/models",
|
"loopback/common/models",
|
||||||
"loopback/server/models",
|
"loopback/server/models",
|
||||||
__dirname + "/../common/models",
|
path.join(__dirname, "../common/models"),
|
||||||
__dirname + "/models",
|
path.join(__dirname, "models"),
|
||||||
rootDir + "/../common/models",
|
rootDir + "/../common/models",
|
||||||
rootDir + "/models"
|
rootDir + "/models"
|
||||||
],
|
],
|
||||||
mixinDirs: [
|
mixinDirs: [
|
||||||
"loopback/common/mixins",
|
"loopback/common/mixins",
|
||||||
"loopback/server/mixins",
|
"loopback/server/mixins",
|
||||||
__dirname + "/../common/mixins",
|
path.join(__dirname, "../common/mixins"),
|
||||||
__dirname + "/mixins",
|
path.join(__dirname, "mixins"),
|
||||||
rootDir + "/../common/mixins",
|
rootDir + "/../common/mixins",
|
||||||
rootDir + "/mixins"
|
rootDir + "/mixins"
|
||||||
],
|
],
|
||||||
bootDirs: [
|
bootDirs: [
|
||||||
__dirname + "/boot",
|
path.join(__dirname, "boot"),
|
||||||
rootDir + "/boot"
|
rootDir + "/boot"
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
boot(app, bootOptions, function(err) {
|
boot(app, bootOptions, function(err) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
if (require.main === module)
|
if (require.main === rootModule)
|
||||||
app.start();
|
app.start();
|
||||||
|
|
||||||
if (cb)
|
|
||||||
cb(app);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,4 +2,4 @@
|
||||||
var vnLoopback = require('../../loopback/server/server.js');
|
var vnLoopback = require('../../loopback/server/server.js');
|
||||||
|
|
||||||
var app = module.exports = vnLoopback.loopback();
|
var app = module.exports = vnLoopback.loopback();
|
||||||
vnLoopback.boot(app, __dirname);
|
vnLoopback.boot(app, __dirname, module);
|
||||||
|
|
|
@ -2,4 +2,4 @@
|
||||||
var vnLoopback = require('../../loopback/server/server.js');
|
var vnLoopback = require('../../loopback/server/server.js');
|
||||||
|
|
||||||
var app = module.exports = vnLoopback.loopback();
|
var app = module.exports = vnLoopback.loopback();
|
||||||
vnLoopback.boot(app, __dirname);
|
vnLoopback.boot(app, __dirname, module);
|
||||||
|
|
|
@ -10,4 +10,4 @@ app.set('url auth', '/auth');
|
||||||
|
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
|
||||||
vnLoopback.boot(app, __dirname);
|
vnLoopback.boot(app, __dirname, module);
|
||||||
|
|
Loading…
Reference in New Issue