i18n on backend, improved backend error handling, redundant model-config removed

This commit is contained in:
Juan Ferrer Toribio 2018-01-29 19:57:00 +01:00
parent 97adf0a39e
commit f688090cba
28 changed files with 138 additions and 280 deletions

View File

@ -11,7 +11,7 @@
<vn-horizontal style="align-items: center;">
<vn-none pad-medium-h style="color:#FFA410;">
<i class="material-icons" ng-if="i.isDefaultAddress">star</i>
<i class="material-icons pointer" ng-if="!i.isDefaultAddress&&i.isActive" vn-tooltip="Set as default" tooltip-position="left" ng-click="$ctrl.setDefault(i.id)">star_border</i>
<i class="material-icons pointer" ng-if="!i.isDefaultAddress" vn-tooltip="Set as default" tooltip-position="left" ng-click="$ctrl.setDefault(i.id)">star_border</i>
</vn-none>
<vn-one>
<div><b>{{::i.nickname}}</b></div>
@ -27,9 +27,7 @@
</vn-horizontal>
</vn-vertical>
</vn-card>
<vn-paging index="index" total="index.model.total"></vn-paging>
<vn-float-button
fixed-bottom-right
ui-sref="clientCard.addresses.create"

View File

@ -1,27 +0,0 @@
{
"user": {
"dataSource": "salix"
},
"AccessToken": {
"dataSource": "salix",
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
}
},
"ACL": {
"dataSource": "salix"
},
"RoleMapping": {
"dataSource": "salix"
},
"Role": {
"dataSource": "salix"
},
"Account": {
"dataSource": "account"
}
}

View File

@ -1,5 +1,6 @@
{
"name": "AgencyMode",
"description": "Agency modes",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,5 +1,6 @@
{
"name": "ClientCredit",
"description": "Log of credit changes",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,6 +1,6 @@
{
"name": "ClientObservation",
"description": "Notas de los clientes.",
"description": "Client notes",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,5 +1,6 @@
{
"name": "Company",
"description": "Companies",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,3 +0,0 @@
{
"Hello": "Hello english"
}

View File

@ -1,3 +0,0 @@
{
"Hello": "Hola español"
}

View File

@ -1,63 +1,14 @@
{
"user": {
"dataSource": "salix"
},
"AccessToken": {
"dataSource": "salix",
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
}
},
"ACL": {
"dataSource": "salix"
},
"RoleMapping": {
"dataSource": "salix"
},
"Role": {
"dataSource": "salix"
},
"Account": {
"dataSource": "account"
},
"Client": {
"AgencyMode": {
"dataSource": "vn"
},
"ClientCredit": {
"dataSource": "vn"
},
"ClientCreditLimit": {
"dataSource": "vn"
},
"ClientObservation": {
"dataSource": "vn"
},
"PayMethod": {
"dataSource": "vn"
},
"Address": {
"dataSource": "vn"
},
"AgencyMode": {
"dataSource": "vn"
},
"Province": {
"dataSource": "vn"
},
"Country": {
"dataSource": "vn"
},
"ContactChannel": {
"dataSource": "vn"
},
"Worker": {
"dataSource": "vn"
},
"CreditClassification": {
"Company": {
"dataSource": "vn"
},
"Greuge": {
@ -71,8 +22,5 @@
},
"MandateType": {
"dataSource": "vn"
},
"Company": {
"dataSource": "vn"
}
}

View File

@ -1,29 +1,4 @@
{
"user": {
"dataSource": "salix"
},
"AccessToken": {
"dataSource": "salix",
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
}
},
"ACL": {
"dataSource": "salix"
},
"RoleMapping": {
"dataSource": "salix"
},
"Role": {
"dataSource": "salix"
},
"Account": {
"dataSource": "salix"
},
"Item": {
"dataSource": "vn"
},
@ -53,8 +28,5 @@
},
"Expence": {
"dataSource": "vn"
},
"Country": {
"dataSource": "salix"
}
}

View File

@ -0,0 +1,7 @@
{
"PHONE_INVALID_FORMAT": "The phone format is invalid",
"You are not allowed to change the credit": "You are not allowed to change the credit",
"Unable to mark the equivalence surcharge": "Unable to mark the equivalence surcharge",
"The default consignee can not be unchecked": "The default consignee can not be unchecked",
"Unable to default a disabled consignee": "Unable to default a disabled consignee"
}

View File

@ -0,0 +1,7 @@
{
"PHONE_INVALID_FORMAT": "El formato del teléfono no es correcto",
"You are not allowed to change the credit": "No tienes privilegios para modificar el crédito",
"Unable to mark the equivalence surcharge": "No se puede marcar el recargo de equivalencia",
"The default consignee can not be unchecked": "No se puede desmarcar el consignatario predeterminado",
"Unable to default a disabled consignee": "No se puede poner predeterminado un consignatario desactivado"
}

View File

@ -4,7 +4,7 @@ var isMultiple = require('../helpers').isMultiple;
module.exports = function(Self) {
Self.validate('isDefaultAddress', isActive,
{message: 'No se puede poner predeterminado un consignatario desactivado'}
{message: 'Unable to default a disabled consignee'}
);
function isActive(err) {
if (!this.isActive && this.isDefaultAddress) err();
@ -36,7 +36,7 @@ module.exports = function(Self) {
let finalState = getFinalState(ctx);
if (changes.isActive == false && finalState.isDefaultAddress)
throw new UserError('No se puede desmarcar el consignatario predeterminado');
throw new UserError('The default consignee can not be unchecked');
if (changes.isDefaultAddress == true) {
let filter = {

View File

@ -1,5 +1,6 @@
{
"name": "Address",
"description": "Client addresses",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -92,14 +92,14 @@ module.exports = function(Self) {
let canMarkEqualizationTax = fiLetter != 'A' && fiLetter != 'B';
if (finalState.isEqualizated && !canMarkEqualizationTax)
throw new UserError('No se puede marcar el recargo de equivalencia');
throw new UserError('Unable to mark the equivalence surcharge');
}
if (changes.credit !== undefined)
try {
await validateCreditChange(ctx, finalState);
} catch (e) {
throw new UserError('No tienes privilegios para modificar el crédito');
throw new UserError('You are not allowed to change the credit');
}
});

View File

@ -1,5 +1,6 @@
{
"name": "Country",
"description": "Worldwide countries",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,6 +1,6 @@
{
"name": "CreditClassification",
"description": "Clientes clasificados.",
"description": "Clasified clients",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,5 +1,6 @@
{
"name": "Province",
"description": "Provinces of every country",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -125,7 +125,7 @@ module.exports = function(Self) {
Self.disconnectFromService = function(dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer("").toString('base64'),
bearer: new Buffer('').toString('base64'),
sendImmediately: true
};
};
@ -229,5 +229,5 @@ function removeEmpty(o) {
}
function isEmpty(value) {
return value === undefined || value === "";
return value === undefined || value === '';
}

View File

@ -1,5 +1,6 @@
{
"name": "Warehouse",
"description": "Warehouses from where orders are sent",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,5 +1,6 @@
{
"name": "Worker",
"description": "Company employees",
"base": "VnModel",
"options": {
"mysql": {

View File

@ -1,10 +1,28 @@
const UserError = require('../../common/helpers').UserError;
module.exports = function() {
return function(err, req, res, next) {
// MySQL user-defined exceptions are sent to the client
if (err.sqlState == '45000') {
err.statusCode = 400;
err.message = err.sqlMessage;
// Thrown user errors
if (err instanceof UserError) {
err.message = req.__(err.message);
return next(err);
}
// Validation errors
if (err.statusCode == 422) {
try {
let code;
let messages = err.details.messages;
for (code in messages) break;
err.message = req.__(messages[code][0]);
return next(err);
} catch (e) {}
}
// MySQL user-defined exceptions
if (err.sqlState == '45000')
return next(new UserError(req.__(err.sqlMessage)));
next(err);
};
};

View File

@ -1,18 +1,4 @@
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"user": {
"dataSource": "salix"
},
@ -37,5 +23,38 @@
},
"Account": {
"dataSource": "account"
},
"Address": {
"dataSource": "vn"
},
"Agency": {
"dataSource": "vn"
},
"Client": {
"dataSource": "vn"
},
"ClientCreditLimit": {
"dataSource": "vn"
},
"ContactChannel": {
"dataSource": "vn"
},
"Country": {
"dataSource": "vn"
},
"CreditClassification": {
"dataSource": "vn"
},
"PayMethod": {
"dataSource": "vn"
},
"Province": {
"dataSource": "vn"
},
"Warehouse":{
"dataSource": "vn"
},
"Worker": {
"dataSource": "vn"
}
}

View File

@ -1,9 +1,9 @@
var loopback = require('loopback');
var boot = require('loopback-boot');
var path = require('path');
var fs = require('fs');
var i18n = require('i18n');
let loopback = require('loopback');
let boot = require('loopback-boot');
let path = require('path');
let fs = require('fs');
let i18n = require('i18n');
module.exports = {
loopback: loopback,
@ -13,18 +13,15 @@ module.exports = {
function vnBoot(app, rootDir, rootModule) {
// Internationalization
let i18nDir = rootDir + '/i18n';
let localeDir = `${__dirname}/../common/locale`;
if (fs.existsSync(i18nDir)) {
if (fs.existsSync(localeDir)) {
i18n.configure({
directory: i18nDir,
directory: localeDir,
defaultLocale: 'es'
});
app.get('/prueba', function(req, res) {
i18n.setLocale(req.get('Accept-Language').substring(0, 2));
res.send(i18n.__('Hello'));
});
app.use(i18n.init);
}
// View
@ -45,40 +42,52 @@ function vnBoot(app, rootDir, rootModule) {
app.start = function() {
return app.listen(function() {
app.emit('started');
var baseUrl = app.get('url').replace(/\/$/, '');
let baseUrl = app.get('url').replace(/\/$/, '');
console.log(`Web server ${appName} listening at: %s`, baseUrl);
});
};
let config = require('./config.json');
for (var key in config) {
for (let key in config) {
app.set(key, config[key]);
}
var bootOptions = {
let modelConfigFiles = [
`${__dirname}/model-config.json`,
`${rootDir}/model-config.json`
];
let modelConfig = {};
for (file of modelConfigFiles)
if (fs.existsSync(file))
Object.assign(modelConfig, require(file));
let bootOptions = {
appRootDir: __dirname,
appConfigRootDir: rootDir,
modelsRootDir: rootDir,
models: modelConfig,
modelSources: [
'loopback/common/models',
'loopback/server/models',
path.join(__dirname, '../common/models'),
path.join(__dirname, 'models'),
rootDir + '/../common/models',
rootDir + '/models'
`loopback/common/models`,
`loopback/server/models`,
`${__dirname}/../common/models`,
`${__dirname}/models`,
`${rootDir}/../common/models`,
`${rootDir}/models`
],
mixinDirs: [
'loopback/common/mixins',
'loopback/server/mixins',
path.join(__dirname, '../common/mixins'),
path.join(__dirname, 'mixins'),
rootDir + '/../common/mixins',
rootDir + '/mixins'
`loopback/common/mixins`,
`loopback/server/mixins`,
`${__dirname}/../common/mixins`,
`${__dirname}/mixins`,
`${rootDir}/../common/mixins`,
`${rootDir}/mixins`
],
bootDirs: [
path.join(__dirname, 'boot'),
rootDir + '/boot'
`${__dirname}/boot`,
`${rootDir}/boot`
]
};

View File

@ -1,50 +1,4 @@
{
"user": {
"dataSource": "salix"
},
"AccessToken": {
"dataSource": "salix",
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
}
},
"ACL": {
"dataSource": "salix"
},
"RoleMapping": {
"dataSource": "salix"
},
"Role": {
"dataSource": "salix"
},
"Account": {
"dataSource": "account"
},
"Ticket": {
"dataSource": "vn"
},
"State":{
"dataSource": "vn"
},
"TicketState":{
"dataSource": "vn"
},
"Warehouse":{
"dataSource": "vn"
},
"Client":{
"dataSource": "vn"
},
"Province":{
"dataSource": "vn"
},
"Agency": {
"dataSource": "vn"
},
"FakeProduction": {
"dataSource": "vn"
},
@ -57,10 +11,16 @@
"Route": {
"dataSource": "vn"
},
"TicketTracking": {
"State":{
"dataSource": "vn"
},
"Worker": {
"Ticket": {
"dataSource": "vn"
},
"TicketState":{
"dataSource": "vn"
},
"TicketTracking": {
"dataSource": "vn"
}
}

View File

@ -1,39 +1,11 @@
{
"user": {
"dataSource": "salix"
},
"AccessToken": {
"dataSource": "salix",
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
}
},
"ACL": {
"dataSource": "salix"
},
"RoleMapping": {
"dataSource": "salix"
},
"Role": {
"dataSource": "salix"
},
"Account": {
"dataSource": "account"
},
"Delivery": {
"dataSource": "vn"
},
"Zone": {
"dataSource": "vn"
},
"Agency": {
"dataSource": "vn"
},
"Vehicle": {
"dataSource": "vn"
},
"Zone": {
"dataSource": "vn"
}
}

View File

@ -58,7 +58,7 @@ module.exports = function(app) {
function validateToken(tokenId, cb) {
models.AccessToken.findById(tokenId, function(err, token) {
if (token) {
token.validate(function (err, isValid) {
token.validate(function(err, isValid) {
cb(isValid === true, token);
});
} else

View File

@ -1,27 +0,0 @@
{
"user": {
"dataSource": "salix"
},
"AccessToken": {
"dataSource": "salix",
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
}
},
"ACL": {
"dataSource": "salix"
},
"RoleMapping": {
"dataSource": "salix"
},
"Role": {
"dataSource": "salix"
},
"Account": {
"dataSource": "account"
}
}