From 050c338ffb67283ae303f06e786a492bd88262b8 Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 13 Jan 2025 14:29:13 +0100 Subject: [PATCH] feat: refs #8387 crudModel --- loopback/locale/en.json | 3 ++- loopback/locale/es.json | 5 +++-- modules/item/back/models/item-tag.js | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 80da13ae5..e6b7b98c1 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -250,5 +250,6 @@ "Holidays to past days not available": "Holidays to past days not available", "Price cannot be blank": "Price cannot be blank", "There are tickets to be invoiced": "There are tickets to be invoiced", - "The address of the customer must have information about Incoterms and Customs Agent": "The address of the customer must have information about Incoterms and Customs Agent" + "The address of the customer must have information about Incoterms and Customs Agent": "The address of the customer must have information about Incoterms and Customs Agent", + "The value must be a number or a range of numbers": "The value must be a number or a range of numbers" } \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index fcee0e111..93ba0d59a 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -398,5 +398,6 @@ "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles", "All tickets have a route order": "Todos los tickets tienen orden de ruta", "Price cannot be blank": "Price cannot be blank", - "There are tickets to be invoiced": "La zona tiene tickets por facturar" -} + "There are tickets to be invoiced": "La zona tiene tickets por facturar", + "The value must be a number or a range of numbers": "The value must be a number or a range of numbers" +} \ No newline at end of file diff --git a/modules/item/back/models/item-tag.js b/modules/item/back/models/item-tag.js index 5b7163913..338ef64cf 100644 --- a/modules/item/back/models/item-tag.js +++ b/modules/item/back/models/item-tag.js @@ -10,4 +10,27 @@ module.exports = Self => { return new UserError(`Tag value cannot be blank`); return err; }); + + Self.observe('before save', async ctx => { + const validValue = new RegExp('^\\d{1,3}(-\\d{1,3})?$'); + let tagFk; + let value; + + if (ctx.isNewInstance) { + tagFk = ctx.instance.tagFk; + value = ctx.instance.value; + } + const newData = ctx.data.value || null; + const currentData = ctx.currentInstance.value || null; + const models = Self.app.models; + const validTag = await models.Tag.findOne({where: {name: 'Longitud(cm)'}}); + + if (tagFk === validTag.id || (currentData && currentData === validTag.id)) { + if ( + (value && !validValue.test(value)) || + (newData && !validValue.test(newData)) + ) + throw new UserError('The value must be a number or a range of numbers'); + } + }); };