From 1c8a3078156d8e46e52c57eec7d1ef6fff619832 Mon Sep 17 00:00:00 2001 From: minie4 Date: Tue, 3 May 2022 18:36:07 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Mark=20non-existent=20substitutions?= =?UTF-8?q?=20as=20removed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/parser/index.js | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/server/parser/index.js b/server/parser/index.js index bbcfae0..71990ae 100644 --- a/server/parser/index.js +++ b/server/parser/index.js @@ -94,6 +94,7 @@ export class Parser { const knownSubstitutions = await prisma.substitution.findMany({ where: { date: new Date(new Date(date).setUTCHours(0, 0, 0, 0)), + removed: false, }, }); @@ -107,15 +108,19 @@ export class Parser { change.subject = change.notes; } - const matchingSubstitution = knownSubstitutions.find((substitution) => { - return substitution.date == new Date(date).setUTCHours(0, 0, 0, 0) && - substitution.type == (change.type == "Entfall") - ? "cancellation" - : "change" && - substitution.lesson == change.lesson && - classes.sort().join(",") == substitution.class.sort().join(",") && - substitution.changedSubject == change.subject; - }); + const matchingSubstitutionId = knownSubstitutions.findIndex( + (substitution) => { + return substitution.date == new Date(date).setUTCHours(0, 0, 0, 0) && + substitution.type == (change.type == "Entfall") + ? "cancellation" + : "change" && + substitution.lesson == change.lesson && + classes.sort().join(",") == + substitution.class.sort().join(",") && + substitution.changedSubject == change.subject; + } + ); + const matchingSubstitution = knownSubstitutions[matchingSubstitutionId]; if (!matchingSubstitution) { const newSubstitution = await prisma.substitution.create({ @@ -176,8 +181,31 @@ export class Parser { `Substitution unchanged: S:${matchingSubstitution.id}` ); } + knownSubstitutions.splice(matchingSubstitutionId, 1); } } + for (const remainingSubstitution of knownSubstitutions) { + await prisma.substitution.update({ + where: { + id: remainingSubstitution.id, + }, + data: { + removed: true, + }, + }); + const substitutionChange = await prisma.substitutionChange.create({ + data: { + substitutionId: remainingSubstitution.id, + type: "deletion", + changes: {}, + parseEventId: parseEvent.id, + }, + }); + log( + "Insert / DB", + `Deleted removed substitution: S:${remainingSubstitution.id} C:${substitutionChange.id}` + ); + } } getSubstitutionClasses(classList, classString) { const matchingClasses = classList.filter((element) => {