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
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ module.exports = (db) => {
*/
return async (req, res) => {
try {
// Check to see if the user gets a bonus question
if (Math.random() >= 0.4) {
return res.status(200).json({ success: true });
}

const bonusChallenge = await db
// Get a random bonus question from the database
const response = await db
.collection('bonusChallenge')
.aggregate([
{
Expand All @@ -35,13 +37,14 @@ module.exports = (db) => {
}))
);

if (!bonusChallenge) {
// Check if a bonus question was chosen
if (!response) {
return res
.status(404)
.json({ success: false, error: 'No active bonus challenges found' });
}

return res.status(200).json({ success: true, body: bonusChallenge[0] });
return res.status(200).json({ success: true, body: response[0] });
} catch (error) {
return res.status(500).json({ success: false, error: error.message });
}
Expand Down
3 changes: 3 additions & 0 deletions backend/endpoints/checkFocus/controller/getRandomFocus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module.exports = (db) => {
*/
return async (req, res) => {
try {
// Check to see if the user gets a focus
if (Math.random() >= 0.2) {
return res.status(200).json({ success: true, body: null });
}

// Get a random focus from the database
const checkFocus = await db
.collection('checkFocus')
.aggregate([
Expand All @@ -35,6 +37,7 @@ module.exports = (db) => {
}))
);

// Check if a focus was chosen
if (!checkFocus[0]) {
return res.status(404).json({ success: false, error: 'No active focuses found' });
}
Expand Down
5 changes: 4 additions & 1 deletion backend/endpoints/checkFocus/route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/**
* @param {import('mongodb').Db} db
*/
module.exports = (db) =>
require('express')
.Router()

// GET
.get('/check-focus/random', require('./controller/getRandomFocus')(db));
31 changes: 20 additions & 11 deletions backend/endpoints/hpc/controller/addHpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,48 @@ module.exports = (db) => {
*/
return async (req, res) => {
try {
const { name } = req.body || {};
const { name: rawName } = req.body || {};

// Check name
if (!name) {
return res.status(400).json({ success: false, error: 'Missing hpc name' });
if (rawName === undefined || rawName === null) {
return res.status(400).json({ success: false, error: 'Missing cluster name' });
}

const sanitizedName = String(name).trim();
if (typeof rawName !== 'string') {
return res
.status(400)
.json({ success: false, error: 'The cluster name provided is not a string' });
}

const clusterName = rawName.trim();

if (sanitizedName.length == 0) {
return res.status(400).json({ success: false, error: 'The name provided is empty' });
if (!clusterName) {
return res
.status(400)
.json({ success: false, error: 'The cluster name provided is empty' });
}

// Check if hpc already exists
const existingHpc = await db.collection('cluster').findOne({
const existing = await db.collection('cluster').findOne({
name: {
$regex: `^${sanitizedName}$`,
$regex: `^${clusterName}$`,
$options: 'i'
}
});

if (existingHpc) {
return res.status(409).json({ success: false, error: 'HPC already exits' });
if (existing) {
return res.status(409).json({ success: false, error: 'Cluster already exits' });
}

// Add it to the database
const clusterId = await db
.collection('cluster')
.insertOne({
name: sanitizedName
name: clusterName
})
.then((res) => res.insertedId.toString());

// Populate the database with generic instructions and methods for the cluster
templateIt(clusterId, db);

return res.status(200).json({ success: true });
Expand Down
72 changes: 43 additions & 29 deletions backend/endpoints/hpc/controller/addHpcToPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,92 @@ module.exports = (db) => {
*/
return async (req, res) => {
try {
const { id } = req.params || {};
const { poolId } = req.body || {};
const { id: rawClusterId } = req.params || {};
const { poolId: rawPoolId } = req.body || {};

// Check id
if (!id) {
return res.status(400).json({ success: false, error: 'Missing cluster id' });
// Check cluster id
if (rawClusterId === undefined || rawClusterId === null) {
return res.status(400).json({ success: false, error: 'Missing cluster ID' });
}

if (!poolId) {
return res.status(400).json({ success: false, error: 'Missing pool id' });
if (typeof rawClusterId !== 'string') {
return res
.status(400)
.json({ success: false, error: 'The cluster ID provided is not a string' });
}

const sanitizedId = String(id).trim();
const clusterId = rawClusterId.trim();

if (sanitizedId.length === 0) {
if (!clusterId) {
return res
.status(400)
.json({ success: false, error: 'The cluster id provided is empty' });
.json({ success: false, error: 'The cluster ID provided is empty' });
}

if (!ObjectId.isValid(clusterId)) {
return res.status(400).json({ success: false, error: 'Invalid cluster ID provided' });
}

const sanitizedPoolId = String(poolId).trim();
// Check pool id
if (rawPoolId === undefined || rawPoolId === null) {
return res.status(400).json({ success: false, error: 'Missing pool ID' });
}

if (sanitizedPoolId.length === 0) {
return res.status(400).json({ success: false, error: 'The pool id provided is empty' });
if (typeof rawPoolId !== 'string') {
return res
.status(400)
.json({ success: false, error: 'The pool ID provided is not a string' });
}

if (!ObjectId.isValid(sanitizedId)) {
return res.status(400).json({ success: false, error: 'Invalid cluster id provided' });
const poolId = rawPoolId.trim();

if (!poolId) {
return res.status(400).json({ success: false, error: 'The pool ID provided is empty' });
}

if (!ObjectId.isValid(sanitizedPoolId)) {
return res.status(400).json({ success: false, error: 'Invalid pool id provided' });
if (!ObjectId.isValid(poolId)) {
return res.status(400).json({ success: false, error: 'Invalid pool ID provided' });
}

// Get the cluster
const results = await db.collection('cluster').findOne({
_id: new ObjectId(sanitizedId)
const cluster = await db.collection('cluster').findOne({
_id: new ObjectId(clusterId)
});

if (!results) {
if (!cluster) {
return res.status(404).json({ success: false, error: "Cluster doesn't exist" });
}

if (results.poolId === sanitizedPoolId) {
if (cluster.poolId === clusterId) {
return res
.status(400)
.json({ success: false, error: 'Cluster already assigned to pool' });
}

const poolResults = await db.collection('pool').findOne({
_id: new ObjectId(sanitizedPoolId)
// Get pool
const pool = await db.collection('pool').findOne({
_id: new ObjectId(poolId)
});

if (!poolResults) {
if (!pool) {
return res.status(404).json({ success: false, error: "Pool doesn't exist" });
}

db.collection('cluster').updateOne(
{ _id: new ObjectId(sanitizedId) },
{ _id: new ObjectId(clusterId) },
{
$set: {
poolId: sanitizedPoolId
poolId: poolId
}
}
);

return res.status(200).json({
success: true,
body: {
id: sanitizedId,
name: results.name,
poolId: sanitizedPoolId
id: clusterId,
name: cluster.name,
poolId: poolId
}
});
} catch (error) {
Expand Down
36 changes: 21 additions & 15 deletions backend/endpoints/hpc/controller/deleteHpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,37 @@ module.exports = (db) => {
*/
return async (req, res) => {
try {
const { id } = req.params;
const { id: rawClusterId } = req.params;

// Check id
if (!id) {
return res.status(400).json({ success: false, error: 'Missing hpc id' });
// Check cluster id
if (rawClusterId === undefined || rawClusterId === null) {
return res.status(400).json({ success: false, error: 'Missing cluster ID' });
}

const sanitizedId = String(id).trim();
if (typeof rawClusterId !== 'string') {
return res
.status(400)
.json({ success: false, error: 'The cluster ID provided is not a string' });
}

const clusterId = rawClusterId.trim();

if (sanitizedId.length === 0) {
if (!clusterId) {
return res
.status(400)
.json({ success: false, error: "The hpc id you've provided is empty" });
.json({ success: false, error: 'The cluster ID provided is empty' });
}

if (!ObjectId.isValid(sanitizedId)) {
return res.status(400).json({ success: false, error: 'Invalid cluster id provided' });
if (!ObjectId.isValid(clusterId)) {
return res.status(400).json({ success: false, error: 'Invalid cluster ID provided' });
}

// Check cluster exists
const existingCluster = await db.collection('cluster').findOneAndDelete({
_id: new ObjectId(sanitizedId)
// Get cluster
const cluster = await db.collection('cluster').findOneAndDelete({
_id: new ObjectId(clusterId)
});

if (!existingCluster) {
if (!cluster) {
return res.status(409).json({
success: false,
error: "HPC doesn't exists"
Expand All @@ -46,7 +52,7 @@ module.exports = (db) => {
const reports = await db
.collection('report')
.find({
clusterId: sanitizedId
clusterId: clusterId
})
.toArray()
.then((res) =>
Expand Down Expand Up @@ -74,7 +80,7 @@ module.exports = (db) => {
const instructions = await db
.collection('instruction')
.find({
clusterId: new ObjectId(sanitizedId)
clusterId: new ObjectId(clusterId)
})
.toArray()
.then((res) =>
Expand Down
30 changes: 15 additions & 15 deletions backend/endpoints/hpc/controller/getHpcById.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,41 @@ module.exports = (db) => {
*/
return async (req, res) => {
try {
const { id } = req.params || {};
const { id: rawClusterId } = req.params || {};

// Check id
if (typeof id !== 'string') {
return res
.status(400)
.json({ success: false, error: 'The cluster id provided is not a string' });
// Check cluster id
if (rawClusterId === undefined || rawClusterId === null) {
return res.status(400).json({ success: false, error: 'Missing cluster ID' });
}

if (!id) {
return res.status(400).json({ success: false, error: 'Missing cluster id' });
if (typeof rawClusterId !== 'string') {
return res
.status(400)
.json({ success: false, error: 'The cluster ID provided is not a string' });
}

const sanitizedId = String(id).trim();
const clusterId = rawClusterId.trim();

if (sanitizedId.length === 0) {
if (!clusterId) {
return res
.status(400)
.json({ success: false, error: 'The cluster id provided is empty' });
.json({ success: false, error: 'The cluster ID provided is empty' });
}

if (!ObjectId.isValid(sanitizedId)) {
return res.status(400).json({ success: false, error: 'Invalid cluster id provided' });
if (!ObjectId.isValid(clusterId)) {
return res.status(400).json({ success: false, error: 'Invalid cluster ID provided' });
}

// Get the cluster
const response = await db.collection('cluster').findOne({
_id: new ObjectId(sanitizedId)
_id: new ObjectId(clusterId)
});

if (!response) {
return res.status(404).json({ success: false, error: "Cluster doesn't exist" });
}

response.id = sanitizedId;
response.id = clusterId;
delete response._id;

return res.status(200).json({
Expand Down
Loading
Loading