refs #5878 perf: handle when create customer

This commit is contained in:
Javier Segarra 2024-01-10 14:51:59 +01:00
parent 2c91b5b701
commit c11bcbebee
4 changed files with 92 additions and 28 deletions

View File

@ -2,10 +2,12 @@
const UserError = require('../../util/user-error'); const UserError = require('../../util/user-error');
const logToConsole = require('strong-error-handler/lib/logger'); const logToConsole = require('strong-error-handler/lib/logger');
const valueIsNot = require('./value-is-not'); const valueIsNot = require('./value-is-not');
const valueInvalid = require('./value-invalid');
const validations = [{ const validations = [
...valueIsNot valueIsNot,
}]; valueInvalid
];
module.exports = function() { module.exports = function() {
return function(err, req, res, next) { return function(err, req, res, next) {
// Thrown user errors // Thrown user errors
@ -15,11 +17,11 @@ module.exports = function() {
} }
// Validation errors // Validation errors
if (err.statusCode == 400) { if ([400, 422].includes(err.statusCode)) {
try { try {
validations.forEach(validation => { validations.forEach(validation => {
if (validation.validation(err.message)) { if (validation.validation(err.message)) {
const error = validation.handleError(req); const error = validation.handleError(req, err);
if (error) if (error)
err.message = validation.message(error, req); err.message = validation.message(error, req);
} }
@ -28,8 +30,6 @@ module.exports = function() {
return next(err); return next(err);
} catch (e) { } catch (e) {
} }
}
if (err.statusCode == 422) {
try { try {
let code; let code;
let {messages} = err.details; let {messages} = err.details;

View File

@ -0,0 +1,58 @@
const SLASH = '/';
const $t = require('i18n');
const {models} = require('vn-loopback/server/server');
const mapLocale = require('../../util/map-locales');
module.exports = {
validation: message => String(message).includes('is not valid'),
message: ({tagValue}, {__: $t}) =>
$t('Field are invalid', {tag: tagValue}),
handleError: ({method: verb, originalUrl, body}, err) => {
let tag = null;
let module = null;
let moduleOriginal = null;
let path = null;
try {
if (originalUrl.includes('?'))
originalUrl = originalUrl.split('?')[0];
originalUrl = originalUrl.split('api/')[1];
[module, ...path] = originalUrl.split(SLASH);
moduleOriginal = module;
let model = models[module];
// Capitalize
if (!model) {
module = module.charAt(0).toUpperCase() + module.slice(1);
model = models[module];
}
// Singular
if (!model) {
module = module.substring(0, module.length - 1);
model = models[module];
}
if (!model) throw new Error('No matching model found');
tag = Object.keys(err.details.codes)[0];
if (tag) {
let tagValue = mapLocale[$t.getLocale()][tag];
if (!tagValue) tagValue = tag;
return {tag, tagValue};
}
} catch (error) {
throw new Error(error);
}
}
};
function isJsonString(str) {
try {
let json = JSON.parse(str);
return (typeof json === 'object');
} catch (e) {
return false;
}
}

View File

@ -1,28 +1,15 @@
const SLASH = '/'; const SLASH = '/';
const MODULES = 'modules';
const BACK = 'back';
const path = require('path');
const glob = require('glob');
const modulesPath = `${MODULES}/**/${BACK}/locale/**/**.yml`;
const pathResolve = path.resolve(modulesPath);
const modelsLocale = glob.sync(pathResolve, {}).map(f => {
const file = require(f);
const model = f.substring(f.indexOf(MODULES), f.indexOf(BACK) - 1).split(SLASH)[1];
const locale = path.parse(f).base.split('.')[0];
return [keyMap(model, locale), file.columns];
}
);
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
const mapLocale = new Map(modelsLocale); const $t = require('i18n');
const mapLocale = require('../../util/map-locales');
module.exports = { module.exports = {
validation: message => String(message).startsWith('Value is not'), validation: message => String(message).startsWith('Value is not'),
message: ({tagValue}, {__: $t}) => message: ({tagValue}, {__: $t}) =>
$t('Field are invalid', {tag: tagValue}), $t('Field are invalid', {tag: tagValue}),
handleError: ({method: verb, originalUrl, body, __: $t}) => { handleError: ({method: verb, originalUrl, body}) => {
let tag = null; let tag = null;
let module = null; let module = null;
let moduleOriginal = null;
let path = null; let path = null;
let hasId = false; let hasId = false;
try { try {
@ -33,7 +20,6 @@ module.exports = {
[module, ...path] = originalUrl.split(SLASH); [module, ...path] = originalUrl.split(SLASH);
hasId = path.length > 1; hasId = path.length > 1;
moduleOriginal = module;
let model = models[module]; let model = models[module];
// Capitalize // Capitalize
if (!model) { if (!model) {
@ -98,7 +84,7 @@ module.exports = {
} }
} }
if (tag) { if (tag) {
let tagValue = mapLocale.get(keyMap(moduleOriginal, $t.getLocale()))[tag]; let tagValue = mapLocale[$t.getLocale()][tag];
if (!tagValue) tagValue = tag; if (!tagValue) tagValue = tag;
return {tag, tagValue}; return {tag, tagValue};
} }
@ -109,9 +95,6 @@ module.exports = {
}; };
function keyMap(model, local, connector = '_') {
return `${model}${connector}${local}`;
}
function isJsonString(str) { function isJsonString(str) {
try { try {

View File

@ -0,0 +1,23 @@
const SLASH = '/';
const MODULES = 'modules';
const BACK = 'back';
const path = require('path');
const glob = require('glob');
const modulesPath = `${MODULES}/**/${BACK}/locale/**/**.yml`;
const pathResolve = path.resolve(modulesPath);
const modelsLocale = glob.sync(pathResolve, {}).reduce((acc, f) => {
const file = require(f);
const model = f.substring(f.indexOf(MODULES), f.indexOf(BACK) - 1).split(SLASH)[1];
const locale = path.parse(f).base.split('.')[0];
if (!acc[locale]) acc[locale] = {};
acc[locale] = Object.assign(acc[locale], file.columns);
return acc;
}, {}
);
function keyMap(model, local, connector = '_') {
return `${model}${connector}${local}`;
}
module.exports =modelsLocale;