Partly update fields with PATCH requests

main
Dmitry Kalinin 5 years ago
parent 332bef2110
commit 8966d9e4e8

@ -674,6 +674,11 @@
task: undefined, task: undefined,
}; };
let dirtyFields = {
assignee: false,
status: false,
};
for (const property in data) { for (const property in data) {
if (Object.prototype.hasOwnProperty.call(data, property)) { if (Object.prototype.hasOwnProperty.call(data, property)) {
if (property in initialData) { if (property in initialData) {
@ -715,6 +720,7 @@
if (assignee !== null && !(assignee instanceof User)) { if (assignee !== null && !(assignee instanceof User)) {
throw new ArgumentError('Value must be a user instance'); throw new ArgumentError('Value must be a user instance');
} }
dirtyFields.assignee = true;
data.assignee = assignee; data.assignee = assignee;
}, },
}, },
@ -743,6 +749,7 @@
); );
} }
dirtyFields.status = true;
data.status = status; data.status = status;
}, },
}, },
@ -776,6 +783,12 @@
task: { task: {
get: () => data.task, get: () => data.task,
}, },
__dirtyFields: {
get: () => dirtyFields,
set: (fields) => {
dirtyFields = fields;
},
},
}), }),
); );
@ -879,6 +892,13 @@
use_cache: undefined, use_cache: undefined,
}; };
let dirtyFields = {
name: false,
assignee: false,
bug_tracker: false,
labels: false,
};
for (const property in data) { for (const property in data) {
if (Object.prototype.hasOwnProperty.call(data, property) && property in initialData) { if (Object.prototype.hasOwnProperty.call(data, property) && property in initialData) {
data[property] = initialData[property]; data[property] = initialData[property];
@ -948,6 +968,7 @@
if (!value.trim().length) { if (!value.trim().length) {
throw new ArgumentError('Value must not be empty'); throw new ArgumentError('Value must not be empty');
} }
dirtyFields.name = true;
data.name = value; data.name = value;
}, },
}, },
@ -1006,6 +1027,7 @@
if (assignee !== null && !(assignee instanceof User)) { if (assignee !== null && !(assignee instanceof User)) {
throw new ArgumentError('Value must be a user instance'); throw new ArgumentError('Value must be a user instance');
} }
dirtyFields.assignee = true;
data.assignee = assignee; data.assignee = assignee;
}, },
}, },
@ -1039,6 +1061,7 @@
bugTracker: { bugTracker: {
get: () => data.bug_tracker, get: () => data.bug_tracker,
set: (tracker) => { set: (tracker) => {
dirtyFields.bug_tracker = true;
data.bug_tracker = tracker; data.bug_tracker = tracker;
}, },
}, },
@ -1145,6 +1168,7 @@
} }
} }
dirtyFields.labels = true;
data.labels = [...labels]; data.labels = [...labels];
}, },
}, },
@ -1311,6 +1335,12 @@
dataChunkType: { dataChunkType: {
get: () => data.data_compressed_chunk_type, get: () => data.data_compressed_chunk_type,
}, },
__dirtyFields: {
get: () => dirtyFields,
set: (fields) => {
dirtyFields = fields;
},
},
}), }),
); );
@ -1443,12 +1473,30 @@
Job.prototype.save.implementation = async function () { Job.prototype.save.implementation = async function () {
// TODO: Add ability to change an assignee // TODO: Add ability to change an assignee
if (this.id) { if (this.id) {
const jobData = { const jobData = {};
status: this.status,
assignee_id: this.assignee ? this.assignee.id : null, for (const [field, isDirty] of Object.entries(this.__dirtyFields)) {
}; if (isDirty) {
switch (field) {
case 'status':
jobData.status = this.status;
break;
case 'assignee':
jobData.assignee_id = this.assignee ? this.assignee.id : null;
break;
default:
break;
}
}
}
await serverProxy.jobs.saveJob(this.id, jobData); await serverProxy.jobs.saveJob(this.id, jobData);
this.__dirtyFields = {
status: false,
assignee: false,
};
return this; return this;
} }
@ -1653,14 +1701,38 @@
// TODO: Add ability to change an owner and an assignee // TODO: Add ability to change an owner and an assignee
if (typeof this.id !== 'undefined') { if (typeof this.id !== 'undefined') {
// If the task has been already created, we update it // If the task has been already created, we update it
const taskData = { const taskData = {};
assignee_id: this.assignee ? this.assignee.id : null,
name: this.name, for (const [field, isDirty] of Object.entries(this.__dirtyFields)) {
bug_tracker: this.bugTracker, if (isDirty) {
labels: [...this.labels.map((el) => el.toJSON())], switch (field) {
}; case 'assignee':
taskData.assignee_id = this.assignee ? this.assignee.id : null;
break;
case 'name':
taskData.name = this.name;
break;
case 'bugTracker':
taskData.bug_tracker = this.bugTracker;
break;
case 'labels':
taskData.labels = [...this.labels.map((el) => el.toJSON())];
break;
default:
break;
}
}
}
await serverProxy.tasks.saveTask(this.id, taskData); await serverProxy.tasks.saveTask(this.id, taskData);
this.dirtyFields = {
assignee: false,
name: false,
bugTracker: false,
labels: false,
};
return this; return this;
} }

Loading…
Cancel
Save