Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/db/mongodb-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
"validator": {
"$jsonSchema": {
"bsonType": "object",
"required": ["_id", "name", "teamId"],
"required": ["_id", "name"],
"properties": {
"_id": {
"bsonType": "objectId"
Expand Down
3 changes: 3 additions & 0 deletions backend/endpoints/hpc/controller/addHpcToPool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ObjectId } = require('mongodb');
const { handleStateChange } = require('../../../schedule/scheduler');

/**
* @param {import('mongodb').Db} db
Expand Down Expand Up @@ -91,6 +92,8 @@ module.exports = (db) => {
}
);

await handleStateChange(db);

return res.status(200).json({
success: true,
body: {
Expand Down
3 changes: 3 additions & 0 deletions backend/endpoints/hpc/controller/removeHpcFromPool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ObjectId } = require('mongodb');
const { handleStateChange } = require('../../../schedule/scheduler');

/**
* @param {import('mongodb').Db} db
Expand Down Expand Up @@ -90,6 +91,8 @@ module.exports = (db) => {
}
);

await handleStateChange(db);

return res.status(200).json({
success: true,
body: {
Expand Down
7 changes: 5 additions & 2 deletions backend/endpoints/people/controller/addPeopleToTeam.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ObjectId } = require('mongodb');
const { handleStateChange } = require('../../../schedule/scheduler');

/**
* @param {import('mongodb').Db} db
Expand Down Expand Up @@ -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
});

Expand All @@ -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 });
Expand Down
3 changes: 3 additions & 0 deletions backend/endpoints/pool/controller/addPoolToTeam.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ObjectId } = require('mongodb');
const { handleStateChange } = require('../../../schedule/scheduler');

/**
* @param {import('mongodb').Db} db
Expand Down Expand Up @@ -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: {
Expand Down
3 changes: 3 additions & 0 deletions backend/endpoints/pool/controller/removePoolFromTeam.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ObjectId } = require('mongodb');
const { handleStateChange } = require('../../../schedule/scheduler');

/**
* @param {import('mongodb').Db} db
Expand Down Expand Up @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/rota/scheduleLogic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getScheduleForDay = require('../../schedule/scheduler');
const { getScheduleForDay } = require('../../schedule/scheduler');

async function getDaily(db, day = new Date()) {
const schedule = {};
Expand Down
3 changes: 3 additions & 0 deletions backend/endpoints/team/controller/updateTeamSettings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ObjectId } = require('mongodb');
const { handleStateChange } = require('../../../schedule/scheduler');

/**
* @param {import('mongodb').Db} db
Expand Down Expand Up @@ -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 });
Expand Down
32 changes: 31 additions & 1 deletion backend/schedule/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>} 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
};
6 changes: 3 additions & 3 deletions frontend/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

npm i

#npm run build
#npm run start
npm run build
npm run start

npm run dev
#npm run dev
Loading