From 99338bb6f80f973e2a38bbc1fd92b009df3fb740 Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Tue, 17 Jan 2023 10:11:41 +0100 Subject: [PATCH 1/9] isPhotoRequired implemented --- back/models/image.js | 7 +++++++ modules/item/back/methods/item/new.js | 5 ++++- modules/item/back/models/item.json | 6 ++++++ modules/item/front/basic-data/index.html | 6 ++++++ modules/item/front/basic-data/locale/es.yml | 4 +++- modules/item/front/summary/index.html | 10 ++++++++++ 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/back/models/image.js b/back/models/image.js index d736e924f..f646543fc 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -145,6 +145,13 @@ module.exports = Self => { fileName, myOptions ); + if (item.isPhotoRequested) { + await item.updateAttribute( + 'isPhotoRequested', + false, + myOptions + ); + } } if (fs.existsSync(srcFilePath)) diff --git a/modules/item/back/methods/item/new.js b/modules/item/back/methods/item/new.js index fae37836f..a20437887 100644 --- a/modules/item/back/methods/item/new.js +++ b/modules/item/back/methods/item/new.js @@ -24,6 +24,8 @@ module.exports = Self => { const myOptions = {}; let tx; + params.isPhotoRequested = true; + if (typeof options == 'object') Object.assign(myOptions, options); @@ -37,7 +39,8 @@ module.exports = Self => { 'typeFk', 'intrastatFk', 'originFk', - 'relevancy' + 'relevancy', + 'isPhotoRequested', ]; for (const key in params) { diff --git a/modules/item/back/models/item.json b/modules/item/back/models/item.json index 2f58c30a9..10300f7a1 100644 --- a/modules/item/back/models/item.json +++ b/modules/item/back/models/item.json @@ -146,6 +146,12 @@ }, "isLaid": { "type": "boolean" + }, + "isPhotoRequested": { + "type": "boolean", + "mysql":{ + "columnName": "doPhoto" + } } }, "relations": { diff --git a/modules/item/front/basic-data/index.html b/modules/item/front/basic-data/index.html index fd21ab240..974aa37d8 100644 --- a/modules/item/front/basic-data/index.html +++ b/modules/item/front/basic-data/index.html @@ -182,6 +182,12 @@ ng-model="$ctrl.item.isFragile" info="Is shown at website, app that this item cannot travel (wreath, palms, ...)"> + + diff --git a/modules/item/front/basic-data/locale/es.yml b/modules/item/front/basic-data/locale/es.yml index d59752ebb..747dfe792 100644 --- a/modules/item/front/basic-data/locale/es.yml +++ b/modules/item/front/basic-data/locale/es.yml @@ -11,4 +11,6 @@ Identifier: Identificador Fragile: Frágil Is shown at website, app that this item cannot travel (wreath, palms, ...): Se muestra en la web, app que este artículo no puede viajar (coronas, palmas, ...) Multiplier: Multiplicador -Generic: Genérico \ No newline at end of file +Generic: Genérico +This item does need a photo: Este artículo necesita una foto +Do photo: Hacer foto \ No newline at end of file diff --git a/modules/item/front/summary/index.html b/modules/item/front/summary/index.html index a4d40c829..801a82ee0 100644 --- a/modules/item/front/summary/index.html +++ b/modules/item/front/summary/index.html @@ -65,6 +65,16 @@ {{$ctrl.summary.item.itemType.worker.user.name}} + + + +

From 2e988ea701a5d294a0a7a8063b3e2385a0dfe31f Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Fri, 20 Jan 2023 13:34:36 +0100 Subject: [PATCH 2/9] doPhoto updated on item upload --- back/models/image.js | 7 ------- modules/item/back/models/item.js | 9 +++++++++ modules/item/front/summary/index.html | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/back/models/image.js b/back/models/image.js index f646543fc..d736e924f 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -145,13 +145,6 @@ module.exports = Self => { fileName, myOptions ); - if (item.isPhotoRequested) { - await item.updateAttribute( - 'isPhotoRequested', - false, - myOptions - ); - } } if (fs.existsSync(srcFilePath)) diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index b8baa97ea..03a6fb721 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -21,6 +21,15 @@ module.exports = Self => { Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'}); Self.observe('before save', async function(ctx) { + let currentInstance = ctx.currentInstance; + let oldInstance; + let newInstance; + ctx.hookState.oldInstance === undefined ? oldInstance = false : oldInstance = ctx.hookState.oldInstance; + ctx.hookState.newInstance === undefined ? newInstance = false : newInstance = ctx.hookState.newInstance; + if (oldInstance.image && newInstance.image) { + let query = `UPDATE vn.item SET doPhoto = 0 WHERE id = ${currentInstance.id}`; + await Self.rawSql(query); + } await Self.availableId(ctx); }); diff --git a/modules/item/front/summary/index.html b/modules/item/front/summary/index.html index 801a82ee0..8f958bd3b 100644 --- a/modules/item/front/summary/index.html +++ b/modules/item/front/summary/index.html @@ -65,6 +65,26 @@ {{$ctrl.summary.item.itemType.worker.user.name}} + + + + + + + + Date: Wed, 25 Jan 2023 14:44:00 +0100 Subject: [PATCH 3/9] added logs --- back/methods/image/upload.js | 3 ++- back/models/image.js | 3 ++- modules/item/back/models/item.js | 24 ++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/back/methods/image/upload.js b/back/methods/image/upload.js index 676a4b5fb..6e5b80f81 100644 --- a/back/methods/image/upload.js +++ b/back/methods/image/upload.js @@ -40,6 +40,7 @@ module.exports = Self => { const TempContainer = models.TempContainer; const fileOptions = {}; const args = ctx.args; + const user = ctx.req.accessToken.userId; let srcFile; try { @@ -59,7 +60,7 @@ module.exports = Self => { const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); srcFile = path.join(file.client.root, file.container, file.name); - await models.Image.registerImage(args.collection, srcFile, args.fileName, args.id); + await models.Image.registerImage(args.collection, srcFile, args.fileName, args.id, user); } catch (e) { if (fs.existsSync(srcFile)) await fs.unlink(srcFile); diff --git a/back/models/image.js b/back/models/image.js index d736e924f..f7063c654 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -46,7 +46,7 @@ module.exports = Self => { }).bitmap; } - Self.registerImage = async(collectionName, srcFilePath, fileName, entityId) => { + Self.registerImage = async(collectionName, srcFilePath, fileName, entityId, user) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); const myOptions = {transaction: tx}; @@ -140,6 +140,7 @@ module.exports = Self => { const item = await model.findById(entityId, null, myOptions); if (item) { + collection.model == 'item' ? fileName = fileName + '-' + user : fileName; await item.updateAttribute( collection.property, fileName, diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index 03a6fb721..acccf617b 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -21,14 +21,34 @@ module.exports = Self => { Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'}); Self.observe('before save', async function(ctx) { - let currentInstance = ctx.currentInstance; + let instance = ctx.currentInstance; let oldInstance; let newInstance; ctx.hookState.oldInstance === undefined ? oldInstance = false : oldInstance = ctx.hookState.oldInstance; ctx.hookState.newInstance === undefined ? newInstance = false : newInstance = ctx.hookState.newInstance; if (oldInstance.image && newInstance.image) { - let query = `UPDATE vn.item SET doPhoto = 0 WHERE id = ${currentInstance.id}`; + let image = newInstance.image.split('-')[0]; + let user = newInstance.image.split('-')[1]; + + ctx.hookState.newInstance.image = image; + + let query = `UPDATE vn.item SET doPhoto=0 WHERE id = ${instance.id}`; await Self.rawSql(query); + let logQuery = + 'INSERT INTO vn.itemLog(originFk, userFk, action, creationDate, description, oldInstance, newInstance) ' + + 'VALUES (?, ?, ?, ?, ?, ?, ?)'; + + let logParams = [ + instance.id, + user, + 'update', + new Date(), + 'Image', + JSON.stringify(oldInstance), + JSON.stringify(newInstance) + ]; + + await Self.rawSql(logQuery, logParams); } await Self.availableId(ctx); }); From e3de54b81109e98541be7374ea1c2ca295c7be5f Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Fri, 3 Feb 2023 08:35:15 +0100 Subject: [PATCH 4/9] changed styles on summary --- modules/item/front/summary/index.html | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/item/front/summary/index.html b/modules/item/front/summary/index.html index 8f958bd3b..e6746eedf 100644 --- a/modules/item/front/summary/index.html +++ b/modules/item/front/summary/index.html @@ -65,19 +65,23 @@ {{$ctrl.summary.item.itemType.worker.user.name}} - + - + + + + - + + - +

From c7f22a10bab9c37130adf7b04f7635dba413bd55 Mon Sep 17 00:00:00 2001 From: alexandre Date: Mon, 20 Feb 2023 10:39:12 +0100 Subject: [PATCH 5/9] refs #5063 refactored hook --- back/methods/image/upload.js | 3 +- back/models/image.js | 4 +-- modules/item/back/models/item.js | 58 ++++++++++++-------------------- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/back/methods/image/upload.js b/back/methods/image/upload.js index 6e5b80f81..676a4b5fb 100644 --- a/back/methods/image/upload.js +++ b/back/methods/image/upload.js @@ -40,7 +40,6 @@ module.exports = Self => { const TempContainer = models.TempContainer; const fileOptions = {}; const args = ctx.args; - const user = ctx.req.accessToken.userId; let srcFile; try { @@ -60,7 +59,7 @@ module.exports = Self => { const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); srcFile = path.join(file.client.root, file.container, file.name); - await models.Image.registerImage(args.collection, srcFile, args.fileName, args.id, user); + await models.Image.registerImage(args.collection, srcFile, args.fileName, args.id); } catch (e) { if (fs.existsSync(srcFile)) await fs.unlink(srcFile); diff --git a/back/models/image.js b/back/models/image.js index e8cab11aa..ba92ee383 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -46,7 +46,7 @@ module.exports = Self => { }).bitmap; } - Self.registerImage = async(collectionName, srcFilePath, fileName, entityId, user) => { + Self.registerImage = async(collectionName, srcFilePath, fileName, entityId) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); const myOptions = {transaction: tx}; @@ -140,7 +140,7 @@ module.exports = Self => { const item = await model.findById(entityId, null, myOptions); if (item) { - collection.model == 'item' ? fileName = fileName + '-' + user : fileName; + if (collection.model == 'Item') fileName += '#'; await item.updateAttribute( collection.property, fileName, diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index acccf617b..ec4be2f7b 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -21,52 +21,38 @@ module.exports = Self => { Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'}); Self.observe('before save', async function(ctx) { - let instance = ctx.currentInstance; - let oldInstance; - let newInstance; - ctx.hookState.oldInstance === undefined ? oldInstance = false : oldInstance = ctx.hookState.oldInstance; - ctx.hookState.newInstance === undefined ? newInstance = false : newInstance = ctx.hookState.newInstance; - if (oldInstance.image && newInstance.image) { - let image = newInstance.image.split('-')[0]; - let user = newInstance.image.split('-')[1]; + if (ctx.isNewInstance) await Self.availableId(ctx); + else { + const changes = ctx.data || ctx.instance; + const orgData = ctx.currentInstance; - ctx.hookState.newInstance.image = image; + const hasChanges = orgData && changes; + const image = changes.image || orgData.image; + const imageChanged = hasChanges && orgData.image != image; - let query = `UPDATE vn.item SET doPhoto=0 WHERE id = ${instance.id}`; - await Self.rawSql(query); - let logQuery = - 'INSERT INTO vn.itemLog(originFk, userFk, action, creationDate, description, oldInstance, newInstance) ' + - 'VALUES (?, ?, ?, ?, ?, ?, ?)'; - - let logParams = [ - instance.id, - user, - 'update', - new Date(), - 'Image', - JSON.stringify(oldInstance), - JSON.stringify(newInstance) - ]; - - await Self.rawSql(logQuery, logParams); + if (imageChanged) { + try { + changes.image = changes.image.slice(0, -1); + if (orgData.isPhotoRequested == true) changes.isPhotoRequested = false; + } catch (e) { + throw new UserError(e); + } + } } - await Self.availableId(ctx); }); Self.availableId = async function(ctx) { - if (ctx.isNewInstance) { - try { - let query = `SELECT i1.id + 1 as id FROM vn.item i1 + try { + let query = `SELECT i1.id + 1 as id FROM vn.item i1 LEFT JOIN vn.item i2 ON i1.id + 1 = i2.id WHERE i2.id IS NULL ORDER BY i1.id LIMIT 1`; - let newId = await Self.rawSql(query); + let newId = await Self.rawSql(query); - ctx.instance.id = newId[0].id; - return ctx.instance.id; - } catch (e) { - throw new UserError(e); - } + ctx.instance.id = newId[0].id; + return ctx.instance.id; + } catch (e) { + throw new UserError(e); } }; }; From 04877c1425a036816f5375e03930b75891d51b3a Mon Sep 17 00:00:00 2001 From: alexandre Date: Mon, 20 Feb 2023 13:15:47 +0100 Subject: [PATCH 6/9] refs #5063 refactor hook --- modules/item/back/methods/item/new.js | 6 ++---- modules/item/front/summary/index.html | 25 ------------------------- 2 files changed, 2 insertions(+), 29 deletions(-) diff --git a/modules/item/back/methods/item/new.js b/modules/item/back/methods/item/new.js index 0bd9035cf..d066f2c9f 100644 --- a/modules/item/back/methods/item/new.js +++ b/modules/item/back/methods/item/new.js @@ -24,8 +24,6 @@ module.exports = Self => { const myOptions = {}; let tx; - params.isPhotoRequested = true; - if (typeof options == 'object') Object.assign(myOptions, options); @@ -40,8 +38,7 @@ module.exports = Self => { 'intrastatFk', 'originFk', 'priority', - 'tag', - 'isPhotoRequested' + 'tag' ]; for (const key in params) { @@ -60,6 +57,7 @@ module.exports = Self => { const itemType = await models.ItemType.findById(params.typeFk, {fields: ['isLaid']}, myOptions); params.isLaid = itemType.isLaid; + params.isPhotoRequested = true; const item = await models.Item.create(params, myOptions); diff --git a/modules/item/front/summary/index.html b/modules/item/front/summary/index.html index c604f5c8f..46a2baef4 100644 --- a/modules/item/front/summary/index.html +++ b/modules/item/front/summary/index.html @@ -75,31 +75,6 @@ {{$ctrl.summary.item.itemType.worker.user.name}} - - - - - - - - - - - - Date: Fri, 24 Feb 2023 10:25:08 +0100 Subject: [PATCH 7/9] refs #5063 deleted item checked in image, added front test --- back/models/image.js | 1 - modules/item/back/models/item.js | 33 ++++++--------------- modules/item/front/descriptor/index.js | 5 ++++ modules/item/front/descriptor/index.spec.js | 16 +++++++++- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/back/models/image.js b/back/models/image.js index ba92ee383..37f4ec20d 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -140,7 +140,6 @@ module.exports = Self => { const item = await model.findById(entityId, null, myOptions); if (item) { - if (collection.model == 'Item') fileName += '#'; await item.updateAttribute( collection.property, fileName, diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index ec4be2f7b..72b172f48 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -22,37 +22,22 @@ module.exports = Self => { Self.observe('before save', async function(ctx) { if (ctx.isNewInstance) await Self.availableId(ctx); - else { - const changes = ctx.data || ctx.instance; - const orgData = ctx.currentInstance; - - const hasChanges = orgData && changes; - const image = changes.image || orgData.image; - const imageChanged = hasChanges && orgData.image != image; - - if (imageChanged) { - try { - changes.image = changes.image.slice(0, -1); - if (orgData.isPhotoRequested == true) changes.isPhotoRequested = false; - } catch (e) { - throw new UserError(e); - } - } - } }); Self.availableId = async function(ctx) { - try { - let query = `SELECT i1.id + 1 as id FROM vn.item i1 + if (ctx.isNewInstance) { + try { + let query = `SELECT i1.id + 1 as id FROM vn.item i1 LEFT JOIN vn.item i2 ON i1.id + 1 = i2.id WHERE i2.id IS NULL ORDER BY i1.id LIMIT 1`; - let newId = await Self.rawSql(query); + let newId = await Self.rawSql(query); - ctx.instance.id = newId[0].id; - return ctx.instance.id; - } catch (e) { - throw new UserError(e); + ctx.instance.id = newId[0].id; + return ctx.instance.id; + } catch (e) { + throw new UserError(e); + } } }; }; diff --git a/modules/item/front/descriptor/index.js b/modules/item/front/descriptor/index.js index 972c89ae0..f45a5ed1f 100644 --- a/modules/item/front/descriptor/index.js +++ b/modules/item/front/descriptor/index.js @@ -91,6 +91,11 @@ class Controller extends Descriptor { this.$.photo.setAttribute('src', newSrc); this.$.photo.setAttribute('zoom-image', newZoomSrc); + + if (this.item.isPhotoRequested) { + this.$http.patch(`Items/${this.item.id}`, {isPhotoRequested: false}) + .then(() => this.item.isPhotoRequested = false); + } } getWarehouseName(warehouseFk) { diff --git a/modules/item/front/descriptor/index.spec.js b/modules/item/front/descriptor/index.spec.js index 23053432e..8df0b3fa8 100644 --- a/modules/item/front/descriptor/index.spec.js +++ b/modules/item/front/descriptor/index.spec.js @@ -8,7 +8,8 @@ describe('vnItemDescriptor', () => { id: 1, itemType: { warehouseFk: 1 - } + }, + isPhotoRequested: true }; const stock = { visible: 1, @@ -43,4 +44,17 @@ describe('vnItemDescriptor', () => { expect(controller.item).toEqual(item); }); }); + + describe('onUploadResponse()', () => { + it(`should change isPhotoRequested when a new photo is uploaded`, () => { + $httpBackend.expectPATCH(`Items/${item.id}`).respond(); + controller.$rootScope = {imagePath: () => {}}; + controller.$.photo = {setAttribute: () => {}}; + controller.item = item; + controller.onUploadResponse(); + $httpBackend.flush(); + + expect(controller.item.isPhotoRequested).toEqual(false); + }); + }); }); From 6d6fa11da14b8c339c7d314a83ed9e93b69e3037 Mon Sep 17 00:00:00 2001 From: alexandre Date: Fri, 24 Feb 2023 10:37:35 +0100 Subject: [PATCH 8/9] refs #5063 fixed front test --- modules/item/front/descriptor/index.js | 6 ++---- modules/item/front/descriptor/index.spec.js | 7 +++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/modules/item/front/descriptor/index.js b/modules/item/front/descriptor/index.js index f45a5ed1f..5f38d08dd 100644 --- a/modules/item/front/descriptor/index.js +++ b/modules/item/front/descriptor/index.js @@ -92,10 +92,8 @@ class Controller extends Descriptor { this.$.photo.setAttribute('src', newSrc); this.$.photo.setAttribute('zoom-image', newZoomSrc); - if (this.item.isPhotoRequested) { - this.$http.patch(`Items/${this.item.id}`, {isPhotoRequested: false}) - .then(() => this.item.isPhotoRequested = false); - } + if (this.item.isPhotoRequested) + this.$http.patch(`Items/${this.item.id}`, {isPhotoRequested: false}); } getWarehouseName(warehouseFk) { diff --git a/modules/item/front/descriptor/index.spec.js b/modules/item/front/descriptor/index.spec.js index 8df0b3fa8..dc2a5acf7 100644 --- a/modules/item/front/descriptor/index.spec.js +++ b/modules/item/front/descriptor/index.spec.js @@ -47,14 +47,13 @@ describe('vnItemDescriptor', () => { describe('onUploadResponse()', () => { it(`should change isPhotoRequested when a new photo is uploaded`, () => { - $httpBackend.expectPATCH(`Items/${item.id}`).respond(); + controller.item = item; controller.$rootScope = {imagePath: () => {}}; controller.$.photo = {setAttribute: () => {}}; - controller.item = item; + + $httpBackend.expectPATCH(`Items/${item.id}`).respond(200); controller.onUploadResponse(); $httpBackend.flush(); - - expect(controller.item.isPhotoRequested).toEqual(false); }); }); }); From 698a3396fc19d7265cda9a1faeb7979337813533 Mon Sep 17 00:00:00 2001 From: alexandre Date: Fri, 24 Feb 2023 10:40:24 +0100 Subject: [PATCH 9/9] refs #5063 deleted changes in item hook --- modules/item/back/models/item.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index 72b172f48..b8baa97ea 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -21,7 +21,7 @@ module.exports = Self => { Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'}); Self.observe('before save', async function(ctx) { - if (ctx.isNewInstance) await Self.availableId(ctx); + await Self.availableId(ctx); }); Self.availableId = async function(ctx) {