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

This commit is contained in:
zadam 2020-01-28 22:37:06 +01:00
parent d2e3aedf7f
commit 88213c1bbd
3 changed files with 51 additions and 5 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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