Compare commits

..

2 Commits

Author SHA1 Message Date
baz 3cbeb7df68 Create changelog entries when creating new entries 2024-07-13 21:27:51 +01:00
baz 5fb39b28b3 Add changelog database 2024-07-13 21:00:14 +01:00
4 changed files with 64 additions and 5 deletions

View File

@ -1,4 +1,4 @@
const { Users, Games, LoggedGames } = require ('./dbObjects.js'); const { Users, Games, LoggedGames, Changelog } = require ('./dbObjects.js');
const fs = require('fs'); const fs = require('fs');
async function checkUserRegistration(user) { async function checkUserRegistration(user) {
@ -85,6 +85,8 @@ async function createPlanningGameEntry(user, game, date) {
if (entry.status == 'planning') return false; if (entry.status == 'planning') return false;
await createChangelogEntry(user, game, entry.status, 'planning');
entry.status = 'planning'; entry.status = 'planning';
if (!date) { if (!date) {
@ -106,6 +108,8 @@ async function createPlayingGameEntry(user, game, date) {
if (entry.status == 'playing') return false; if (entry.status == 'playing') return false;
await createChangelogEntry(user, game, entry.status, 'playing');
entry.status = 'playing'; entry.status = 'playing';
if (!date) { if (!date) {
@ -127,6 +131,8 @@ async function createBeatenGameEntry(user, game, date) {
if (entry.status == 'beat') return false; if (entry.status == 'beat') return false;
await createChangelogEntry(user, game, entry.status, 'beat');
entry.status = 'beat'; entry.status = 'beat';
if (!date) { if (!date) {
@ -141,6 +147,13 @@ async function createBeatenGameEntry(user, game, date) {
return entry; 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) { async function checkLoggedGameEntry(user, game) {
const bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id } }) const bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id } })
.catch((err) => { .catch((err) => {
@ -163,7 +176,14 @@ async function createLoggedGameEntry(user, game, status, date) {
console.log(err); 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; return false;
} }
@ -216,6 +236,13 @@ async function deleteLoggedGameId(id, user, status) {
const entry = bg; const entry = bg;
await bg.destroy(); await bg.destroy();
if (bg) {
await Changelog.create({ userId: user.id, gameId: entry.gameId, oldStatus: status })
.catch((err) => {
console.log(err);
});
}
return entry; return entry;
} }
@ -244,6 +271,13 @@ async function deleteLoggedGameNum(num, user, status) {
const entry = bg[num - 1]; const entry = bg[num - 1];
await bg[num - 1].destroy(); 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; return entry;
} }

View File

@ -10,6 +10,7 @@ const sequelize = new Sequelize('database', 'username', 'password', {
const db = require('./models/games.js')(sequelize, Sequelize.DataTypes); const db = require('./models/games.js')(sequelize, Sequelize.DataTypes);
require('./models/users.js')(sequelize, Sequelize.DataTypes); require('./models/users.js')(sequelize, Sequelize.DataTypes);
require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes); require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
require('./models/changelog.js')(sequelize, Sequelize.DataTypes);
const force = process.argv.includes('--force') || process.argv.includes('-f'); const force = process.argv.includes('--force') || process.argv.includes('-f');

View File

@ -10,15 +10,24 @@ const sequelize = new Sequelize('database', 'username', 'password', {
const Users = require('./models/users.js')(sequelize, Sequelize.DataTypes); const Users = require('./models/users.js')(sequelize, Sequelize.DataTypes);
const Games = require('./models/games.js')(sequelize, Sequelize.DataTypes); const Games = require('./models/games.js')(sequelize, Sequelize.DataTypes);
const LoggedGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes); const LoggedGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
const Changelog = require('./models/changelog.js')(sequelize, Sequelize.DataTypes);
// Create userId foreignKey // Create userId foreignKey in LoggedGames
Users.hasMany(LoggedGames); Users.hasMany(LoggedGames);
LoggedGames.belongsTo(Users); LoggedGames.belongsTo(Users);
// Create gameId foreignKey // Create gameId foreignKey in LoggedGames
Games.hasMany(LoggedGames); Games.hasMany(LoggedGames);
LoggedGames.belongsTo(Games); LoggedGames.belongsTo(Games);
// Create userId foreignKey in Changelog
Users.hasMany(Changelog);
Changelog.belongsTo(Users);
// Create gameId foreignKey in Changelog
Games.hasMany(Changelog);
Changelog.belongsTo(Games);
Reflect.defineProperty(Users.prototype, 'addUser', { Reflect.defineProperty(Users.prototype, 'addUser', {
value: async function addUser(userData) { value: async function addUser(userData) {
const user = await Users.findOne({ const user = await Users.findOne({
@ -44,4 +53,4 @@ sequelize.sync({ alter: true })
console.log(err); console.log(err);
}); });
module.exports = { sequelize, Users, Games, LoggedGames }; module.exports = { sequelize, Users, Games, LoggedGames, Changelog };

15
models/changelog.js Normal file
View File

@ -0,0 +1,15 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('changelog', {
oldStatus: {
type: DataTypes.ENUM('planning', 'playing', 'beat'),
allowNull: true,
},
newStatus: {
type: DataTypes.ENUM('planning', 'playing', 'beat'),
allowNull: true,
},
}, {
timestamps: true,
});
};