From 88213c1bbded7377186b5828d416ac30e234c838 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 28 Jan 2020 22:37:06 +0100 Subject: [PATCH] attributes are now updated only if their type and name does not change, in case of relation not even value must change. If they do, the old attribute is deleted and new is created instead --- src/entities/attribute.js | 14 +++++++++++++ src/entities/branch.js | 2 +- src/routes/api/attributes.js | 40 ++++++++++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/entities/attribute.js b/src/entities/attribute.js index e37c59fba..411ed704b 100644 --- a/src/entities/attribute.js +++ b/src/entities/attribute.js @@ -116,6 +116,20 @@ class Attribute extends Entity { delete pojo.isOwned; delete pojo.__note; } + + createClone(type, name, value) { + return new Attribute({ + noteId: this.noteId, + type: type, + name: name, + value: value, + position: this.position, + isInheritable: this.isInheritable, + isDeleted: false, + utcDateCreated: this.utcDateCreated, + utcDateModified: this.utcDateModified + }); + } } module.exports = Attribute; \ No newline at end of file diff --git a/src/entities/branch.js b/src/entities/branch.js index b0b1f32e4..a4022f88a 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -71,7 +71,7 @@ class Branch extends Entity { delete pojo.origParentNoteId; } - getClone(parentNoteId, notePosition) { + createClone(parentNoteId, notePosition) { return new Branch({ noteId: this.noteId, parentNoteId: parentNoteId, diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.js index ac751dab1..b705e1181 100644 --- a/src/routes/api/attributes.js +++ b/src/routes/api/attributes.js @@ -18,6 +18,27 @@ async function updateNoteAttribute(req) { let attribute; if (body.attributeId) { attribute = await repository.getAttribute(body.attributeId); + + if (attribute.noteId !== noteId) { + return [400, `Attribute ${body.attributeId} is not owned by ${noteId}`]; + } + + if (body.type !== attribute.type + || body.name !== attribute.name + || (body.type === 'relation' && body.value !== attribute.value)) { + + if (body.type !== 'relation' || !!body.value.trim()) { + const newAttribute = attribute.createClone(body.type, body.name, body.value); + await newAttribute.save(); + } + + attribute.isDeleted = true; + await attribute.save(); + + return { + attributeId: attribute.attributeId + }; + } } else { if (body.type === 'relation' && !body.value.trim()) { @@ -30,10 +51,6 @@ async function updateNoteAttribute(req) { attribute.type = body.type; } - if (attribute.noteId !== noteId) { - return [400, `Attribute ${body.attributeId} is not owned by ${noteId}`]; - } - if (body.value.trim()) { attribute.value = body.value; } @@ -78,6 +95,21 @@ async function updateNoteAttributes(req) { if (attributeEntity.noteId !== noteId) { return [400, `Attribute ${attributeEntity.noteId} is not owned by ${noteId}`]; } + + if (attribute.type !== attributeEntity.type + || attribute.name !== attributeEntity.name + || (attribute.type === 'relation' && attribute.value !== attributeEntity.value)) { + + if (attribute.type !== 'relation' || !!attribute.value.trim()) { + const newAttribute = attribute.createClone(attribute.type, attribute.name, attribute.value); + await newAttribute.save(); + } + + attributeEntity.isDeleted = true; + await attributeEntity.save(); + + continue; + } } else { // if it was "created" and then immediatelly deleted, we just don't create it at all