Find and handle changed substitutions

This commit is contained in:
2022-05-03 17:23:12 +02:00
parent 8a61620410
commit f1c71f6ef6

View File

@ -143,9 +143,39 @@ export class Parser {
"Insert / DB",
`Created new substitution: S:${newSubstitution.id} C:${substitutionChange.id}`
);
// console.log("Oh no...", change);
} else {
console.log("ok");
const differences = this.findDifferences(matchingSubstitution, change);
if (Object.keys(differences).length > 0) {
const prismaOptions = {
where: {
id: matchingSubstitution.id,
},
data: {},
};
if (differences.teacher)
prismaOptions.data.changedTeacher = change.changedTeacher;
if (differences.room) prismaOptions.data.changedRoom = change.room;
if (differences.notes) prismaOptions.data.notes = change.notes;
await prisma.substitution.update(prismaOptions);
const substitutionChange = await prisma.substitutionChange.create({
data: {
substitutionId: matchingSubstitution.id,
type: "change",
changes: differences,
parseEventId: parseEvent.id,
},
});
log(
"Insert / DB",
`Found changed substitution: S:${matchingSubstitution.id} C:${substitutionChange.id}`
);
} else {
log(
"Insert / DB",
`Substitution unchanged: S:${matchingSubstitution.id}`
);
}
}
}
}
@ -156,4 +186,26 @@ export class Parser {
});
return matchingClasses.map((e) => e.name);
}
findDifferences(currentSubstitution, newChange) {
const differences = {};
if (newChange.changedTeacher != currentSubstitution.changedTeacher) {
differences.teacher = {
before: currentSubstitution.changedTeacher,
after: newChange.changedTeacher,
};
}
if (newChange.room != currentSubstitution.changedRoom) {
differences.room = {
before: currentSubstitution.changedRoom,
after: newChange.room,
};
}
if (newChange.notes != currentSubstitution.notes) {
differences.notes = {
before: currentSubstitution.notes,
after: newChange.notes,
};
}
return differences;
}
}