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); + }); + }); });