diff --git a/backend/db/mongodb-schema.json b/backend/db/mongodb-schema.json index a2293f3..4e36cdf 100644 --- a/backend/db/mongodb-schema.json +++ b/backend/db/mongodb-schema.json @@ -157,7 +157,7 @@ "validator": { "$jsonSchema": { "bsonType": "object", - "required": ["_id", "name", "teamId"], + "required": ["_id", "name"], "properties": { "_id": { "bsonType": "objectId" diff --git a/backend/endpoints/hpc/controller/addHpcToPool.js b/backend/endpoints/hpc/controller/addHpcToPool.js index 7983d84..33891fc 100644 --- a/backend/endpoints/hpc/controller/addHpcToPool.js +++ b/backend/endpoints/hpc/controller/addHpcToPool.js @@ -1,4 +1,5 @@ const { ObjectId } = require('mongodb'); +const { handleStateChange } = require('../../../schedule/scheduler'); /** * @param {import('mongodb').Db} db @@ -91,6 +92,8 @@ module.exports = (db) => { } ); + await handleStateChange(db); + return res.status(200).json({ success: true, body: { diff --git a/backend/endpoints/hpc/controller/removeHpcFromPool.js b/backend/endpoints/hpc/controller/removeHpcFromPool.js index f399609..b2e1589 100644 --- a/backend/endpoints/hpc/controller/removeHpcFromPool.js +++ b/backend/endpoints/hpc/controller/removeHpcFromPool.js @@ -1,4 +1,5 @@ const { ObjectId } = require('mongodb'); +const { handleStateChange } = require('../../../schedule/scheduler'); /** * @param {import('mongodb').Db} db @@ -90,6 +91,8 @@ module.exports = (db) => { } ); + await handleStateChange(db); + return res.status(200).json({ success: true, body: { diff --git a/backend/endpoints/people/controller/addPeopleToTeam.js b/backend/endpoints/people/controller/addPeopleToTeam.js index 8a3ed4c..2a26380 100644 --- a/backend/endpoints/people/controller/addPeopleToTeam.js +++ b/backend/endpoints/people/controller/addPeopleToTeam.js @@ -1,4 +1,5 @@ const { ObjectId } = require('mongodb'); +const { handleStateChange } = require('../../../schedule/scheduler'); /** * @param {import('mongodb').Db} db @@ -69,9 +70,9 @@ module.exports = (db) => { return res.status(404).json({ success: false, error: "Person doesn't exist" }); } - // Check if clusters current team wont be left without clusters + // Check if team won't be left with less than one person if (person.teamId) { - const cluster_count = await db.collection('cluster').countDocuments({ + const cluster_count = await db.collection('person').countDocuments({ teamId: person.teamId }); @@ -98,6 +99,8 @@ module.exports = (db) => { { $set: { teamId: teamId } } ); + await handleStateChange(db); + return res.status(200).json({ success: true }); } catch (error) { return res.status(500).json({ success: false, error: error.message }); diff --git a/backend/endpoints/pool/controller/addPoolToTeam.js b/backend/endpoints/pool/controller/addPoolToTeam.js index 60230ab..c2914ee 100644 --- a/backend/endpoints/pool/controller/addPoolToTeam.js +++ b/backend/endpoints/pool/controller/addPoolToTeam.js @@ -1,4 +1,5 @@ const { ObjectId } = require('mongodb'); +const { handleStateChange } = require('../../../schedule/scheduler'); /** * @param {import('mongodb').Db} db @@ -83,6 +84,8 @@ module.exports = (db) => { const insertedId = (await db.collection('teampool').insertOne({ teamId, poolId })) .insertedId; + await handleStateChange(db); + return res.status(200).json({ success: true, body: { diff --git a/backend/endpoints/pool/controller/removePoolFromTeam.js b/backend/endpoints/pool/controller/removePoolFromTeam.js index b01eb50..24cc6d7 100644 --- a/backend/endpoints/pool/controller/removePoolFromTeam.js +++ b/backend/endpoints/pool/controller/removePoolFromTeam.js @@ -1,4 +1,5 @@ const { ObjectId } = require('mongodb'); +const { handleStateChange } = require('../../../schedule/scheduler'); /** * @param {import('mongodb').Db} db @@ -82,6 +83,8 @@ module.exports = (db) => { // Delete from database await db.collection('teampool').deleteOne({ teamId, poolId }); + await handleStateChange(db); + return res.status(200).json({ success: true }); } catch (error) { return res.status(500).json({ success: false, error: error.message }); diff --git a/backend/endpoints/rota/scheduleLogic.js b/backend/endpoints/rota/scheduleLogic.js index 162899e..aef6ecb 100644 --- a/backend/endpoints/rota/scheduleLogic.js +++ b/backend/endpoints/rota/scheduleLogic.js @@ -1,4 +1,4 @@ -const getScheduleForDay = require('../../schedule/scheduler'); +const { getScheduleForDay } = require('../../schedule/scheduler'); async function getDaily(db, day = new Date()) { const schedule = {}; diff --git a/backend/endpoints/team/controller/updateTeamSettings.js b/backend/endpoints/team/controller/updateTeamSettings.js index a0feeea..49b231d 100644 --- a/backend/endpoints/team/controller/updateTeamSettings.js +++ b/backend/endpoints/team/controller/updateTeamSettings.js @@ -1,4 +1,5 @@ const { ObjectId } = require('mongodb'); +const { handleStateChange } = require('../../../schedule/scheduler'); /** * @param {import('mongodb').Db} db @@ -59,6 +60,8 @@ module.exports = (db) => { // Updates values in the database await db.collection('team').updateOne({ _id: new ObjectId(teamId) }, { $set: updates }); + await handleStateChange(db); + return res.status(200).json({ success: true }); } catch (error) { return res.status(500).json({ success: false, error: error.message }); diff --git a/backend/schedule/scheduler.js b/backend/schedule/scheduler.js index cb480af..0dbd78b 100644 --- a/backend/schedule/scheduler.js +++ b/backend/schedule/scheduler.js @@ -300,4 +300,34 @@ async function formatScheduleForDay(db, date) { return scheduleDict; } -module.exports = formatScheduleForDay; +const tomorrow = () => { + const date = new Date(); + date.setDate(date.getDate() + 1); + date.setHours(0, 0, 0, 0); + return date; +}; + +/** + * Remove the data in the schedule following tommorrow after relevant data is changed + * + * @param {import('mongodb').Db} db - MongoDB database instance. + * @returns {Promise} Array of objects containing person IDs and cluster IDs. + */ +async function handleStateChange(db) { + const dateFrom = tomorrow(); + + const scheduleExists = await db.collection('schedule').findOne({ + day: { $gte: dateFrom } + }); + + if (!scheduleExists) return; + + await db.collection('schedule').deleteMany({ + day: { $gte: dateFrom } + }); +} + +module.exports = { + getScheduleForDay: formatScheduleForDay, + handleStateChange: handleStateChange +}; diff --git a/frontend/setup.sh b/frontend/setup.sh index 6627480..55cc8c7 100755 --- a/frontend/setup.sh +++ b/frontend/setup.sh @@ -2,7 +2,7 @@ npm i -#npm run build -#npm run start +npm run build +npm run start -npm run dev \ No newline at end of file +#npm run dev \ No newline at end of file