Skip to content

Commit

Permalink
Update assessment item handling for change updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed May 30, 2023
1 parent c0e34e4 commit 78e32c3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ export function addAssessmentItem(context, assessmentItem) {
hints: JSON.stringify(assessmentItem.hints || []),
};

return db.transaction('rw', [TABLE_NAMES.CONTENTNODE, TABLE_NAMES.ASSESSMENTITEM], () => {
return AssessmentItem.add(stringifiedAssessmentItem).then(([contentnode, assessment_id]) => {
context.commit('UPDATE_ASSESSMENTITEM', {
...assessmentItem,
contentnode,
assessment_id,
return db.transaction(
'rw',
[TABLE_NAMES.CONTENTNODE, TABLE_NAMES.ASSESSMENTITEM, TABLE_NAMES.CHANGES_TABLE],
() => {
return AssessmentItem.add(stringifiedAssessmentItem).then(([contentnode, assessment_id]) => {
context.commit('UPDATE_ASSESSMENTITEM', {
...assessmentItem,
contentnode,
assessment_id,
});
return updateNodeComplete(contentnode, context);
});
return updateNodeComplete(contentnode, context);
});
});
}
);
}
export function updateAssessmentItem(context, assessmentItem) {
return updateAssessmentItems(context, [assessmentItem]);
Expand All @@ -65,38 +69,46 @@ export function updateAssessmentItems(context, assessmentItems) {
context.commit('UPDATE_ASSESSMENTITEM', assessmentItem);
});

return db.transaction('rw', [TABLE_NAMES.CONTENTNODE, TABLE_NAMES.ASSESSMENTITEM], () => {
return Promise.all(
assessmentItems.map(assessmentItem => {
// API accepts answers and hints as strings
const stringifiedAssessmentItem = {
...assessmentItem,
};
if (assessmentItem.answers) {
stringifiedAssessmentItem.answers = JSON.stringify(assessmentItem.answers);
}
if (assessmentItem.hints) {
stringifiedAssessmentItem.hints = JSON.stringify(assessmentItem.hints);
}
return AssessmentItem.update(
[assessmentItem.contentnode, assessmentItem.assessment_id],
stringifiedAssessmentItem
).then(() => {
updateNodeComplete(assessmentItem.contentnode, context);
});
})
);
});
return db.transaction(
'rw',
[TABLE_NAMES.CONTENTNODE, TABLE_NAMES.ASSESSMENTITEM, TABLE_NAMES.CHANGES_TABLE],
() => {
return Promise.all(
assessmentItems.map(assessmentItem => {
// API accepts answers and hints as strings
const stringifiedAssessmentItem = {
...assessmentItem,
};
if (assessmentItem.answers) {
stringifiedAssessmentItem.answers = JSON.stringify(assessmentItem.answers);
}
if (assessmentItem.hints) {
stringifiedAssessmentItem.hints = JSON.stringify(assessmentItem.hints);
}
return AssessmentItem.update(
[assessmentItem.contentnode, assessmentItem.assessment_id],
stringifiedAssessmentItem
).then(() => {
updateNodeComplete(assessmentItem.contentnode, context);
});
})
);
}
);
}

export function deleteAssessmentItem(context, assessmentItem) {
return db.transaction('rw', [TABLE_NAMES.CONTENTNODE, TABLE_NAMES.ASSESSMENTITEM], () => {
return AssessmentItem.delete([assessmentItem.contentnode, assessmentItem.assessment_id]).then(
() => {
context.commit('DELETE_ASSESSMENTITEM', assessmentItem);
const contentnode = assessmentItem.contentnode;
return updateNodeComplete(contentnode, context);
}
);
});
return db.transaction(
'rw',
[TABLE_NAMES.CONTENTNODE, TABLE_NAMES.ASSESSMENTITEM, TABLE_NAMES.CHANGES_TABLE],
() => {
return AssessmentItem.delete([assessmentItem.contentnode, assessmentItem.assessment_id]).then(
() => {
context.commit('DELETE_ASSESSMENTITEM', assessmentItem);
const contentnode = assessmentItem.contentnode;
return updateNodeComplete(contentnode, context);
}
);
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1802,18 +1802,20 @@ export const AssessmentItem = new Resource({
});
});
},
// Retain super's delete method
_delete: Resource.prototype.delete,
delete(id) {
const nodeId = id[0];
return this.transaction({ mode: 'rw' }, () => {
return this.table.delete(id);
}).then(data => {
return this._delete(id).then(data => {
return this.modifyAssessmentItemCount(nodeId, -1).then(() => {
return data;
});
});
},
// Retain super's add method
_add: Resource.prototype.add,
add(obj) {
return super.add(obj).then(id => {
return this._add(obj).then(id => {
return this.modifyAssessmentItemCount(obj.contentnode, 1).then(() => {
return id;
});
Expand Down

0 comments on commit 78e32c3

Please sign in to comment.