From f1c71f6ef6e2228bda80d8f106c8e82cc83d6e55 Mon Sep 17 00:00:00 2001 From: minie4 Date: Tue, 3 May 2022 17:23:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Find=20and=20handle=20changed=20sub?= =?UTF-8?q?stitutions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/parser/index.js | 56 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/server/parser/index.js b/server/parser/index.js index 392250f..bbcfae0 100644 --- a/server/parser/index.js +++ b/server/parser/index.js @@ -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; + } }