diff --git a/e2e/helpers/selectors.js b/e2e/helpers/selectors.js index 1080263f9..38af739c0 100644 --- a/e2e/helpers/selectors.js +++ b/e2e/helpers/selectors.js @@ -177,7 +177,7 @@ export default { closeItemSummaryPreview: 'vn-item-index [vn-id="preview"] button.close' }, itemCreateView: { - name: `${components.vnTextfield}[name="name"]`, + temporalName: `${components.vnTextfield}[name="provisionalName"]`, typeAutocomplete: `vn-autocomplete[field="$ctrl.item.typeFk"]`, intrastatAutocomplete: `vn-autocomplete[field="$ctrl.item.intrastatFk"]`, originAutocomplete: `vn-autocomplete[field="$ctrl.item.originFk"]`, diff --git a/e2e/paths/item-module/08_item_create_and_clone.spec.js b/e2e/paths/item-module/08_item_create_and_clone.spec.js index 2d82c16d8..679e32ff7 100644 --- a/e2e/paths/item-module/08_item_create_and_clone.spec.js +++ b/e2e/paths/item-module/08_item_create_and_clone.spec.js @@ -49,7 +49,7 @@ describe('Item Create/Clone path', () => { it('should create the Infinity Gauntlet item', async() => { const result = await nightmare - .type(selectors.itemCreateView.name, 'Infinity Gauntlet') + .type(selectors.itemCreateView.temporalName, 'Infinity Gauntlet') .autocompleteSearch(selectors.itemCreateView.typeAutocomplete, 'Crisantemo') .autocompleteSearch(selectors.itemCreateView.intrastatAutocomplete, 'Coral y materiales similares') .autocompleteSearch(selectors.itemCreateView.originAutocomplete, 'Holand') diff --git a/modules/item/back/methods/item/new.js b/modules/item/back/methods/item/new.js new file mode 100644 index 000000000..b8e42213c --- /dev/null +++ b/modules/item/back/methods/item/new.js @@ -0,0 +1,71 @@ +let UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethod('new', { + description: 'Create a new item and returns the new ID', + accessType: 'WRITE', + accepts: [{ + arg: 'params', + type: 'object', + http: {source: 'body'} + }], + returns: { + type: 'number', + root: true + }, + http: { + path: `/new`, + verb: 'post' + } + }); + + Self.new = async params => { + let validUpdateParams = [ + 'provisionalName', + 'typeFk', + 'intrastatFk', + 'originFk', + 'relevancy' + ]; + + for (const key in params) { + if (validUpdateParams.indexOf(key) === -1) + throw new UserError(`You don't have enough privileges to do that`); + } + + let transaction = await Self.beginTransaction({}); + try { + let provisionalName = params.provisionalName; + delete params.provisionalName; + + let item = await Self.app.models.Item.create(params, {transaction: transaction}); + + let typeTags = await Self.app.models.ItemTypeTag.find({where: {itemTypeFk: item.typeFk}}); + let query = `SET @isTriggerDisabled = TRUE`; + await Self.rawSql(query, null, {transaction: transaction}); + + let nameTag = await Self.app.models.Tag.findOne({where: {name: 'Nombre temporal'}}); + + let newTags = []; + + newTags.push({itemFk: item.id, tagFk: nameTag.id, value: provisionalName, priority: '2'}); + typeTags.forEach(typeTag => { + newTags.push({itemFk: item.id, tagFk: typeTag.tagFk, value: '', priority: typeTag.priority}); + }); + + await Self.app.models.ItemTag.create(newTags, {transaction: transaction}); + + query = `SET @isTriggerDisabled = FALSE`; + await Self.rawSql(query, null, {transaction: transaction}); + + + query = `CALL vn.itemRefreshTags(?)`; + await Self.rawSql(query, [item.id], {transaction: transaction}); + await transaction.commit(); + return item; + } catch (e) { + await transaction.rollback(); + throw e; + } + }; +}; diff --git a/modules/item/back/methods/item/specs/new.spec.js b/modules/item/back/methods/item/specs/new.spec.js new file mode 100644 index 000000000..3f0f57b4d --- /dev/null +++ b/modules/item/back/methods/item/specs/new.spec.js @@ -0,0 +1,35 @@ +const app = require(`${serviceRoot}/server/server`); + +describe('item new()', () => { + let item; + + afterAll(async() => { + await app.models.Item.destroyById(item.id); + }); + + it('should create a new item, adding the name as a tag', async() => { + let itemParams = { + intrastatFk: 5080000, + originFk: 1, + provisionalName: 'planta', + typeFk: 2, + relevancy: 0 + }; + item = await app.models.Item.new(itemParams); + let temporalNameTag = await app.models.Tag.findOne({where: {name: 'Nombre temporal'}}); + + let temporalName = await app.models.ItemTag.findOne({ + where: { + itemFk: item.id, + tagFk: temporalNameTag.id, + } + }); + item = await app.models.Item.findById(item.id); + + expect(item.intrastatFk).toEqual(5080000); + expect(item.originFk).toEqual(1); + expect(item.typeFk).toEqual(2); + expect(item.name).toEqual('planta'); + expect(temporalName.value).toEqual('planta'); + }); +}); diff --git a/modules/item/back/models/item-tag.json b/modules/item/back/models/item-tag.json index 7d3860dda..2dae3b174 100644 --- a/modules/item/back/models/item-tag.json +++ b/modules/item/back/models/item-tag.json @@ -13,8 +13,7 @@ "description": "Identifier" }, "value": { - "type": "String", - "required": true + "type": "String" }, "priority": { "type": "Number", diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index 49bbc7ebc..e34b2eec5 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -9,8 +9,8 @@ module.exports = Self => { require('../methods/item/getSummary')(Self); require('../methods/item/getCard')(Self); require('../methods/item/regularize')(Self); + require('../methods/item/new')(Self); - Self.validatesPresenceOf('name', {message: 'Cannot be blank'}); Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'}); Self.observe('before save', async function(ctx) { diff --git a/modules/item/back/models/item.json b/modules/item/back/models/item.json index 12dff25eb..1ca2619e7 100644 --- a/modules/item/back/models/item.json +++ b/modules/item/back/models/item.json @@ -13,8 +13,7 @@ "description": "Identifier" }, "name": { - "type": "String", - "required": true + "type": "String" }, "size": { "type": "Number" @@ -22,6 +21,10 @@ "category": { "type": "String" }, + "typeFk": { + "type": "Number", + "required": true + }, "stems": { "type": "Number" }, diff --git a/modules/item/front/create/index.html b/modules/item/front/create/index.html index ee5a37d01..51b83b404 100644 --- a/modules/item/front/create/index.html +++ b/modules/item/front/create/index.html @@ -1,4 +1,4 @@ - + New item - + +