diff --git a/loopback/server/middleware/specs/value-is-not.spec.js b/loopback/server/middleware/specs/value-is-not.spec.js deleted file mode 100644 index 4d65ddf56..000000000 --- a/loopback/server/middleware/specs/value-is-not.spec.js +++ /dev/null @@ -1,59 +0,0 @@ -const valueIsNot = require('../value-is-not'); - -fdescribe('Value is not', () => { - const i18n = require('i18n'); - - const userId = 9; - const ctx = { - req: { - - accessToken: {userId: userId}, - headers: {origin: 'http://localhost:5000'}, - } - }; - - fit('UpdateFiscalData endpoint', () => { - let messageError = 'Value is not number'; - const tag = 'provinceFk'; - try { - expect(valueIsNot.validation(messageError)).toBeTrue(); - const data = { - method: 'PATCH', - originalUrl: '/api/supplier/updateFiscalData', - body: { - [tag]: null - }, - __: i18n - }; - const result = valueIsNot.handleError(data); - - expect(result.tag).toEqual(tag); - expect(valueIsNot.message(result, i18n)).toEqual('El campo \'provincia\' no es válido'); - } catch (error) { - expect(error).toBeDefined(); - } - }); - - describe('OsTicket', () => { - let messageError = 'Value is not object'; - const tag = 'additionalData'; - it('sendToSupport endpoint', () => { - try { - expect(valueIsNot.validation(messageError)).toBeTrue(); - const data = { - method: 'POST', - originalUrl: '/api/OsTickets/send-to-support?access_token=DEFAULT_TOKEN', - body: { - [tag]: '{ \'foo\': 42 }' - } - }; - const result = valueIsNot.handleError(data); - - expect(result.tag).toEqual(tag); - expect(valueIsNot.message(tag, i18n)).toEqual(`El campo '${tag}' no es válido`); - } catch (error) { - expect(error).toBeDefined(); - } - }); - }); -}); diff --git a/loopback/server/middleware/value-invalid.js b/loopback/server/middleware/value-invalid.js deleted file mode 100644 index 962aa9e1f..000000000 --- a/loopback/server/middleware/value-invalid.js +++ /dev/null @@ -1,58 +0,0 @@ -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; - } -} diff --git a/loopback/server/middleware/value-is-not.js b/loopback/server/middleware/value-is-not.js deleted file mode 100644 index 021af3fb9..000000000 --- a/loopback/server/middleware/value-is-not.js +++ /dev/null @@ -1,106 +0,0 @@ -const SLASH = '/'; -const {models} = require('vn-loopback/server/server'); -const $t = require('i18n'); -const mapLocale = require('../../util/map-locales'); - -module.exports = { - validation: message => String(message).startsWith('Value is not'), - message: ({tagValue}, {__: $t}) => - $t('Field are invalid', {tag: tagValue}), - handleError: ({method: verb, originalUrl, body}) => { - let tag = null; - let module = null; - let path = null; - let hasId = false; - try { - if (originalUrl.includes('?')) - originalUrl = originalUrl.split('?')[0]; - - originalUrl = originalUrl.split('api/')[1]; - [module, ...path] = originalUrl.split(SLASH); - hasId = path.length > 1; - - 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'); - const currentMethod = model.sharedClass.methods().find(method => { - const methodMatch = [method.http].flat().filter(el => el.verb === verb.toLowerCase()).find(el => { - let isValid = false; - if (hasId) - isValid = el.path.replace(':id', path[0]) === SLASH + path.join(SLASH); - else - isValid = el.path.endsWith(path[0]); - return isValid; - }); - if (methodMatch) - return method; - } - ); - if (!currentMethod) throw new Error('No matching currentMethod found'); - const {accepts} = currentMethod; - for (const [key, value] of Object.entries(body)) { - if (!value) { - tag = key; - break; - } - const accept = accepts.find(acc => acc.arg === key); - if (accept.type !== 'any') { - let isValid = false; - if (value) { - switch (accept.type) { - case 'object': - isValid = isJsonString(value); - break; - case 'number': - isValid = /^[0-9]*$/.test(value); - break; - - case 'boolean': - isValid = value instanceof Boolean; - break; - - case 'date': - isValid = (new Date(date) !== 'Invalid Date') && !isNaN(new Date(date)); - break; - - case 'string': - isValid = typeof value == 'string'; - break; - } - } - if (!isValid) { - tag = key; - break; - } - } - } - 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; - } -} diff --git a/loopback/util/map-locales.js b/loopback/util/map-locales.js deleted file mode 100644 index 692de3f23..000000000 --- a/loopback/util/map-locales.js +++ /dev/null @@ -1,23 +0,0 @@ -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; - diff --git a/loopback/util/map-methods.js b/loopback/util/map-methods.js deleted file mode 100644 index 2858cd61d..000000000 --- a/loopback/util/map-methods.js +++ /dev/null @@ -1,12 +0,0 @@ -const SLASH = '/'; -const MODULES = 'modules'; -const BACK = 'back'; - -const app = require('vn-loopback/server/server'); - -const modelsMethods = app; -function keyMap(model, local, connector = '_') { - return `${model}${connector}${local}`; -} -module.exports = modelsMethods; -