course,tasklist,task and completed validation

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-07-17 13:04:27 +08:00
parent 612805acaf
commit d49e3be4d2
26 changed files with 1174 additions and 69 deletions
+7 -7
View File
@@ -18,26 +18,26 @@ async function syncObjectivesCreate(Model, parentField, parentId, objectives, tr
}
/**
* For UPDATE — upsert by objective_id, hard delete removed ones
* For UPDATE — upsert by PK field (default "objective_id"), hard delete removed ones
* objectives: [{ objective_id: "123", text: "text1" }, { text: "new" }]
*/
async function syncObjectivesUpdate(Model, parentField, parentId, objectives, transaction) {
async function syncObjectivesUpdate(Model, parentField, parentId, objectives, transaction, pkField = "objective_id") {
if (!objectives?.length) {
await Model.destroy({ where: { [parentField]: parentId }, force: true, transaction });
return;
}
const existing = await Model.findAll({ where: { [parentField]: parentId }, transaction });
const existingMap = new Map(existing.map((o) => [String(o.objective_id), o]));
const existingMap = new Map(existing.map((o) => [String(o[pkField]), o]));
const incomingIds = new Set(
objectives.filter((o) => o.objective_id).map((o) => String(o.objective_id))
objectives.filter((o) => o[pkField]).map((o) => String(o[pkField]))
);
// Hard delete removed
const toDelete = existing.filter((o) => !incomingIds.has(String(o.objective_id)));
const toDelete = existing.filter((o) => !incomingIds.has(String(o[pkField])));
if (toDelete.length) {
await Model.destroy({
where: { objective_id: toDelete.map((o) => o.objective_id) },
where: { [pkField]: toDelete.map((o) => o[pkField]) },
force: true,
transaction,
});
@@ -46,7 +46,7 @@ async function syncObjectivesUpdate(Model, parentField, parentId, objectives, tr
// Update existing or create new
for (let i = 0; i < objectives.length; i++) {
const item = objectives[i];
const record = item.objective_id ? existingMap.get(String(item.objective_id)) : null;
const record = item[pkField] ? existingMap.get(String(item[pkField])) : null;
if (record) {
await record.update({ text: item.text, order_index: i }, { transaction });