From 3cbeb7df68ecbfe3d63acf3d25fc9dafe8db09ea Mon Sep 17 00:00:00 2001 From: baz Date: Sat, 13 Jul 2024 21:27:51 +0100 Subject: [PATCH] Create changelog entries when creating new entries --- databaseHelperFunctions.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index cc25e03..eb7659d 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -85,6 +85,8 @@ async function createPlanningGameEntry(user, game, date) { if (entry.status == 'planning') return false; + await createChangelogEntry(user, game, entry.status, 'planning'); + entry.status = 'planning'; if (!date) { @@ -106,6 +108,8 @@ async function createPlayingGameEntry(user, game, date) { if (entry.status == 'playing') return false; + await createChangelogEntry(user, game, entry.status, 'playing'); + entry.status = 'playing'; if (!date) { @@ -127,6 +131,8 @@ async function createBeatenGameEntry(user, game, date) { if (entry.status == 'beat') return false; + await createChangelogEntry(user, game, entry.status, 'beat'); + entry.status = 'beat'; if (!date) { @@ -141,6 +147,13 @@ async function createBeatenGameEntry(user, game, date) { return entry; } +async function createChangelogEntry(user, game, oldStatus, newStatus) { + return await Changelog.create({ userId: user.id, gameId: game.id, newStatus: newStatus, oldStatus: oldStatus }) + .catch((err) => { + console.log(err); + }); +} + async function checkLoggedGameEntry(user, game) { const bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id } }) .catch((err) => { @@ -163,7 +176,14 @@ async function createLoggedGameEntry(user, game, status, date) { console.log(err); }); - if (bg) return true; + if (bg) { + await Changelog.create({ userId: user.id, gameId: game.id, newStatus: status }) + .catch((err) => { + console.log(err); + }); + + return true; + } return false; } @@ -216,6 +236,13 @@ async function deleteLoggedGameId(id, user, status) { const entry = bg; await bg.destroy(); + if (bg) { + await Changelog.create({ userId: user.id, gameId: entry.gameId, oldStatus: status }) + .catch((err) => { + console.log(err); + }); + } + return entry; } @@ -244,6 +271,13 @@ async function deleteLoggedGameNum(num, user, status) { const entry = bg[num - 1]; await bg[num - 1].destroy(); + if (bg) { + await Changelog.create({ userId: user.id, gameId: entry.gameId, oldStatus: status }) + .catch((err) => { + console.log(err); + }); + } + return entry; }