diff --git a/backend/endpoints/method/controller/addMethod.js b/backend/endpoints/method/controller/addMethod.js index 21946e1..be94ce8 100644 --- a/backend/endpoints/method/controller/addMethod.js +++ b/backend/endpoints/method/controller/addMethod.js @@ -11,66 +11,63 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id, content } = req.body || {}; + const { id: rawInstructionId, content: rawContent } = req.body || {}; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing instruction id' }); + // Check instruction id + if (rawInstructionId === undefined || rawInstructionId === null) { + return res.status(400).json({ success: false, error: 'Missing instruction ID' }); } - if (typeof id !== 'string') { + if (typeof rawInstructionId !== 'string') { return res .status(400) - .json({ success: false, error: 'The instruction id provided is not a string' }); + .json({ success: false, error: 'The instruction ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const instructionId = rawInstructionId.trim(); - if (sanitizedId.length === 0) { + if (!instructionId) { return res .status(400) - .json({ success: false, error: 'The instruction id provided is empty' }); + .json({ success: false, error: 'The instruction ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(instructionId)) { return res .status(400) - .json({ success: false, error: 'Invalid instruction id provided' }); + .json({ success: false, error: 'Invalid instruction ID provided' }); } // Check content - if (typeof content !== 'string') { + if (rawContent === undefined || rawContent === null) { + return res.status(400).json({ success: false, error: 'Missing method content' }); + } + + if (typeof rawContent !== 'string') { return res .status(400) .json({ success: false, error: 'The content provided is not a string' }); } - if (!content) { - return res.status(400).json({ success: false, error: 'Missing method content' }); - } - - const sanitizedContent = String(content).trim(); + const content = rawContent.trim(); - if (sanitizedContent.length == 0) { + if (!content) { return res.status(400).json({ success: false, error: 'The content provided is empty' }); } // Check if instruction exists - const instructionExists = await db.collection('instruction').findOne({ - _id: new ObjectId(sanitizedId) + const instruction = await db.collection('instruction').findOne({ + _id: new ObjectId(instructionId) }); - if (!instructionExists) { + if (!instruction) { return res .status(404) .json({ success: false, error: "An instruction with that id doesn't exist" }); } // Add to database - await db.collection('method').insertOne({ - instructionId: sanitizedId, - content: sanitizedContent - }); + await db.collection('method').insertOne({ instructionId, content }); return res.status(200).json({ success: true }); } catch (error) { diff --git a/backend/endpoints/method/controller/deleteMethod.js b/backend/endpoints/method/controller/deleteMethod.js index 3016dfd..6cbe45b 100644 --- a/backend/endpoints/method/controller/deleteMethod.js +++ b/backend/endpoints/method/controller/deleteMethod.js @@ -11,36 +11,36 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params; + const { id: rawMethodId } = req.params; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing method id' }); + // Check method id + if (rawMethodId === undefined || rawMethodId === null) { + return res.status(400).json({ success: false, error: 'Missing method ID' }); } - if (typeof id !== 'string') { + if (typeof rawMethodId !== 'string') { return res .status(400) - .json({ success: false, error: 'The method id provided is not a string' }); + .json({ success: false, error: 'The method ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const methodId = rawMethodId.trim(); - if (sanitizedId.length === 0) { + if (!methodId) { return res .status(400) - .json({ success: false, error: 'The method id provided is empty' }); + .json({ success: false, error: 'The method ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(methodId)) { return res .status(400) - .json({ success: false, error: 'The method is provided is invalid' }); + .json({ success: false, error: 'The method ID provided is invalid' }); } // Delete from the database await db.collection('method').findOneAndDelete({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(methodId) }); return res.status(200).json({ success: true }); diff --git a/backend/endpoints/method/controller/getMethodById.js b/backend/endpoints/method/controller/getMethodById.js index 93eb706..13c2126 100644 --- a/backend/endpoints/method/controller/getMethodById.js +++ b/backend/endpoints/method/controller/getMethodById.js @@ -11,41 +11,41 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawMethodId } = req.params || {}; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing method id' }); + // Check method id + if (rawMethodId === undefined || rawMethodId === null) { + return res.status(400).json({ success: false, error: 'Missing method ID' }); } - if (typeof id !== 'string') { + if (typeof rawMethodId !== 'string') { return res .status(400) - .json({ success: false, error: 'The method id provided is not a string' }); + .json({ success: false, error: 'The method ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const methodId = rawMethodId.trim(); - if (sanitizedId.length === 0) { + if (!methodId) { return res .status(400) - .json({ success: false, error: 'The method id provided is empty' }); + .json({ success: false, error: 'The method ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { - return res.status(400).json({ success: false, error: 'Invalid method id provided' }); + if (!ObjectId.isValid(methodId)) { + return res.status(400).json({ success: false, error: 'Invalid method ID provided' }); } // Get methods const response = await db.collection('method').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(methodId) }); if (!response) { return res.status(409).json({ success: false, error: "Method does't exist" }); } - response.id = sanitizedId; + response.id = methodId; delete response._id; return res.status(200).json({ success: true, body: response }); diff --git a/backend/endpoints/method/controller/getMethods.js b/backend/endpoints/method/controller/getMethods.js index 5b80c8b..cc2bb72 100644 --- a/backend/endpoints/method/controller/getMethods.js +++ b/backend/endpoints/method/controller/getMethods.js @@ -11,36 +11,36 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawInstructionId } = req.params || {}; - // check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing instruction id' }); + // check instruction id + if (rawInstructionId === undefined || rawInstructionId === null) { + return res.status(400).json({ success: false, error: 'Missing instruction ID' }); } - if (typeof id !== 'string') { + if (typeof rawInstructionId !== 'string') { return res .status(400) - .json({ success: false, error: 'The instruction id provided is not a string' }); + .json({ success: false, error: 'The instruction ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const instructionId = rawInstructionId.trim(); - if (sanitizedId.length === 0) { + if (!instructionId) { return res .status(400) - .json({ success: false, error: 'The instruction id provided is empty' }); + .json({ success: false, error: 'The instruction ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(instructionId)) { return res .status(400) - .json({ success: false, error: 'Invalid instruction id provided' }); + .json({ success: false, error: 'Invalid instruction ID provided' }); } // Check if instruction exists const instructionExists = await db.collection('instruction').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(instructionId) }); if (!instructionExists) { @@ -50,9 +50,7 @@ module.exports = (db) => { // Get methods const response = await db .collection('method') - .find({ - instructionId: sanitizedId - }) + .find({ instructionId }) .toArray() .then((res) => res.map(({ _id, ...rest }) => ({ diff --git a/backend/endpoints/method/controller/updateMethod.js b/backend/endpoints/method/controller/updateMethod.js index 2ec7b3a..9532b96 100644 --- a/backend/endpoints/method/controller/updateMethod.js +++ b/backend/endpoints/method/controller/updateMethod.js @@ -11,51 +11,51 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id, content } = req.body; + const { id: rawMethodId, content: rawContent } = req.body; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing method id' }); + // Check method id + if (rawMethodId === undefined || rawMethodId === null) { + return res.status(400).json({ success: false, error: 'Missing method ID' }); } - if (typeof id !== 'string') { + if (typeof rawMethodId !== 'string') { return res .status(400) .json({ success: false, error: 'The method id provided is not a string' }); } - const sanitizedId = String(id).trim(); + const methodId = rawMethodId.trim(); - if (sanitizedId.length === 0) { + if (!methodId) { return res .status(400) .json({ success: false, error: 'The method id provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(methodId)) { return res.status(400).json({ success: false, error: 'Invalid method id provided' }); } // Check content - if (!content) { + if (rawContent === undefined || rawContent === null) { return res.status(400).json({ success: false, error: 'Missing method content' }); } - if (typeof id !== 'string') { + if (typeof rawContent !== 'string') { return res .status(400) .json({ success: false, error: 'The content provided is not a string' }); } - const sanitizedContent = String(content).trim(); + const content = rawContent.trim(); - if (sanitizedContent.length == 0) { + if (!content) { return res.status(400).json({ success: false, error: 'The content provided is empty' }); } // Make sure the method is in the database const method = await db.collection('method').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(methodId) }); if (!method) { @@ -65,7 +65,7 @@ module.exports = (db) => { // Update the method in the database await db .collection('method') - .updateOne({ _id: new ObjectId(sanitizedId) }, { $set: { content: sanitizedContent } }); + .updateOne({ _id: new ObjectId(methodId) }, { $set: { content } }); return res.status(200).json({ success: true }); } catch (error) { diff --git a/backend/endpoints/people/controller/addPeople.js b/backend/endpoints/people/controller/addPeople.js index 97606fb..05309a9 100644 --- a/backend/endpoints/people/controller/addPeople.js +++ b/backend/endpoints/people/controller/addPeople.js @@ -9,29 +9,29 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { name } = req.body || {}; + const { name: rawName } = req.body || {}; // Check name - if (!name) { + if (rawName === undefined || rawName === null) { return res.status(400).json({ success: false, error: 'Missing the persons name' }); } - if (typeof id !== 'string') { + if (typeof rawName !== 'string') { return res .status(400) .json({ success: false, error: 'The name provided is not a string' }); } - const sanitizedName = String(name).trim(); + const name = rawName.trim(); - if (sanitizedName.length === 0) { + if (!name) { return res.status(400).json({ success: false, error: 'The name provided is empty' }); } // Make sure the person exists const person = await db.collection('person').findOne({ name: { - $regex: `^${sanitizedName}$`, + $regex: `^${name}$`, $options: 'i' } }); @@ -41,9 +41,7 @@ module.exports = (db) => { } // Add person to the database - await db.collection('person').insertOne({ - name: sanitizedName - }); + await db.collection('person').insertOne({ name }); return res.status(200).json({ success: true }); } catch (error) { diff --git a/backend/endpoints/people/controller/addPeopleToTeam.js b/backend/endpoints/people/controller/addPeopleToTeam.js index 0750227..8a3ed4c 100644 --- a/backend/endpoints/people/controller/addPeopleToTeam.js +++ b/backend/endpoints/people/controller/addPeopleToTeam.js @@ -11,58 +11,58 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; - const { teamId } = req.body || {}; + const { id: rawPersonId } = req.params || {}; + const { teamId: rawTeamId } = req.body || {}; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: "Missing person's id" }); + // Check persons id + if (rawPersonId === undefined || rawPersonId === null) { + return res.status(400).json({ success: false, error: "Missing person's ID" }); } - if (typeof id !== 'string') { + if (typeof rawPersonId !== 'string') { return res .status(400) - .json({ success: false, error: 'The persons id provided is not a string' }); + .json({ success: false, error: 'The persons ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const personId = rawPersonId.trim(); - if (sanitizedId.length === 0) { + if (!personId) { return res .status(400) - .json({ success: false, error: "The person's id provided is empty" }); + .json({ success: false, error: "The person's ID provided is empty" }); } - if (!ObjectId.isValid(sanitizedId)) { - return res.status(400).json({ success: false, error: 'Invalid person id provided' }); + if (!ObjectId.isValid(personId)) { + return res.status(400).json({ success: false, error: 'Invalid person ID provided' }); } // Check team id - if (!teamId) { - return res.status(400).json({ success: false, error: "Missing team's id" }); + if (rawTeamId === undefined || rawTeamId === null) { + return res.status(400).json({ success: false, error: "Missing team's ID" }); } - if (typeof teamId !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) - .json({ success: false, error: 'The team id provided is not a string' }); + .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedTeamId = String(teamId).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedTeamId.length === 0) { + if (!teamId) { return res .status(400) - .json({ success: false, error: "The team's id provided is empty" }); + .json({ success: false, error: "The team's ID provided is empty" }); } - if (!ObjectId.isValid(sanitizedTeamId)) { - return res.status(400).json({ success: false, error: 'Invalid team id provided' }); + if (!ObjectId.isValid(teamId)) { + return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Check if the person exists const person = await db.collection('person').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(personId) }); if (!person) { @@ -85,7 +85,7 @@ module.exports = (db) => { // Check if the team exists const teamExists = await db.collection('team').findOne({ - _id: new ObjectId(sanitizedTeamId) + _id: new ObjectId(teamId) }); if (!teamExists) { @@ -94,8 +94,8 @@ module.exports = (db) => { // Update the person in the database db.collection('person').updateOne( - { _id: new ObjectId(sanitizedId) }, - { $set: { teamId: sanitizedTeamId } } + { _id: new ObjectId(personId) }, + { $set: { teamId: teamId } } ); return res.status(200).json({ success: true }); diff --git a/backend/endpoints/people/controller/deletePeople.js b/backend/endpoints/people/controller/deletePeople.js index 67e81f2..73ebd2a 100644 --- a/backend/endpoints/people/controller/deletePeople.js +++ b/backend/endpoints/people/controller/deletePeople.js @@ -11,29 +11,31 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params; + const { id: rawPersonId } = req.params; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing persons id' }); + // Check person id + if (rawPersonId === undefined || rawPersonId === null) { + return res.status(400).json({ success: false, error: 'Missing persons ID' }); } - if (typeof id !== 'string') { + if (typeof rawPersonId !== 'string') { return res .status(400) - .json({ success: false, error: 'The persons id provided is not a string' }); + .json({ success: false, error: 'The persons ID provided is not a string' }); } - if (!ObjectId.isValid(id)) { - return res.status(400).json({ success: false, error: 'Invalid person id provided' }); + const personId = rawPersonId.trim(); + + if (!ObjectId.isValid(personId)) { + return res.status(400).json({ success: false, error: 'Invalid person ID provided' }); } // Check if person exists - const existingPerson = await db.collection('person').findOne({ - _id: new ObjectId(id) + const person = await db.collection('person').findOne({ + _id: new ObjectId(personId) }); - if (!existingPerson) { + if (!person) { return res.status(409).json({ success: false, error: "Person doesn't exists" @@ -41,9 +43,7 @@ module.exports = (db) => { } // Check if person has reports - const hasReports = await db.collection('report').findOne({ - personId: id - }); + const hasReports = await db.collection('report').findOne({ personId }); if (hasReports) { return res.status(409).json({ success: false, error: 'This person has reports' }); @@ -51,7 +51,7 @@ module.exports = (db) => { // Delete the person from the database await db.collection('person').deleteOne({ - _id: new ObjectId(id) + _id: new ObjectId(personId) }); return res.status(200).json({ success: true }); diff --git a/backend/endpoints/people/controller/getPeopleById.js b/backend/endpoints/people/controller/getPeopleById.js index 2c94f18..817a59c 100644 --- a/backend/endpoints/people/controller/getPeopleById.js +++ b/backend/endpoints/people/controller/getPeopleById.js @@ -11,41 +11,41 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawPersonId } = req.params || {}; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing persons id' }); + // Check person id + if (rawPersonId === undefined || rawPersonId === null) { + return res.status(400).json({ success: false, error: 'Missing persons ID' }); } - if (typeof id !== 'string') { + if (typeof rawPersonId !== 'string') { return res .status(400) - .json({ success: false, error: 'The persons id provided is not a string' }); + .json({ success: false, error: 'The persons ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const personId = rawPersonId.trim(); - if (sanitizedId.length === 0) { + if (!personId) { return res .status(400) - .json({ success: false, error: 'The persons id provided is empty' }); + .json({ success: false, error: 'The persons ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { - return res.status(400).json({ success: false, error: 'Invalid person id provided' }); + if (!ObjectId.isValid(personId)) { + return res.status(400).json({ success: false, error: 'Invalid person ID provided' }); } // Get the person from the database const response = await db.collection('person').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(personId) }); if (!response) { return res.status(404).json({ success: false, error: "Person doesn't exist" }); } - response.id = sanitizedId; + response.id = personId; delete response._id; return res.status(200).json({ diff --git a/backend/endpoints/people/controller/getPeopleByName.js b/backend/endpoints/people/controller/getPeopleByName.js index 3ad0788..788db83 100644 --- a/backend/endpoints/people/controller/getPeopleByName.js +++ b/backend/endpoints/people/controller/getPeopleByName.js @@ -9,29 +9,29 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { name } = req.params || {}; + const { name: rawName } = req.params || {}; // Check name - if (!name) { + if (rawName === undefined || rawName === null) { return res.status(400).json({ success: false, error: 'Missing persons name' }); } - if (typeof id !== 'string') { + if (typeof rawName !== 'string') { return res .status(400) .json({ success: false, error: 'The persons name provided is not a string' }); } - const sanitizedName = String(name).trim(); + const name = rawName.trim(); - if (sanitizedName.length == 0) { + if (!name) { return res.status(400).json({ success: false, error: 'The name provided is empty' }); } // Check is someone exists const response = await db.collection('person').findOne({ name: { - $regex: `^${sanitizedName}$`, + $regex: `^${name}$`, $options: 'i' } }); diff --git a/backend/endpoints/people/controller/getPeopleByTeam.js b/backend/endpoints/people/controller/getPeopleByTeam.js index 393c2bf..c4b5fba 100644 --- a/backend/endpoints/people/controller/getPeopleByTeam.js +++ b/backend/endpoints/people/controller/getPeopleByTeam.js @@ -11,35 +11,33 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawTeamId } = req.params || {}; - // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing team id' }); + // Check team id + if (rawTeamId === undefined || rawTeamId === null) { + return res.status(400).json({ success: false, error: 'Missing team ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) - .json({ success: false, error: 'The team id provided is not a string' }); + .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedId.length === 0) { - return res.status(400).json({ success: false, error: 'The team id provided is empty' }); + if (!teamId) { + return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { - return res.status(400).json({ success: false, error: 'Invalid team id provided' }); + if (!ObjectId.isValid(teamId)) { + return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Get the people const response = await db .collection('person') - .find({ - teamId: sanitizedId - }) + .find({ teamId }) .toArray() .then((res) => res.map(({ _id, ...rest }) => ({ diff --git a/backend/endpoints/people/controller/getPeopleNotInTeam.js b/backend/endpoints/people/controller/getPeopleNotInTeam.js index b724cb8..e4b78c9 100644 --- a/backend/endpoints/people/controller/getPeopleNotInTeam.js +++ b/backend/endpoints/people/controller/getPeopleNotInTeam.js @@ -11,34 +11,34 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawTeamId } = req.params || {}; // Check id - if (!id) { - return res.status(400).json({ success: false, error: 'Missing team id' }); + if (rawTeamId === undefined || rawTeamId === null) { + return res.status(400).json({ success: false, error: 'Missing team ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) - .json({ success: false, error: 'The team id provided is not a string' }); + .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedId.length === 0) { - return res.status(400).json({ success: false, error: 'The team id provided is empty' }); + if (!teamId) { + return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { - return res.status(400).json({ success: false, error: 'Invalid team id provided' }); + if (!ObjectId.isValid(teamId)) { + return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Get the people const response = await db .collection('person') .find({ - teamId: { $ne: sanitizedId } + teamId: { $ne: teamId } }) .toArray() .then((res) => diff --git a/backend/endpoints/pool/controller/addPoolToTeam.js b/backend/endpoints/pool/controller/addPoolToTeam.js index 1c31072..60230ab 100644 --- a/backend/endpoints/pool/controller/addPoolToTeam.js +++ b/backend/endpoints/pool/controller/addPoolToTeam.js @@ -11,70 +11,67 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { teamId } = req.body || {}; - const { id } = req.params || {}; + const { id: rawPoolId } = req.params || {}; + const { teamId: rawTeamId } = req.body || {}; - // Check id - if (!id) { + // Check pool id + if (rawPoolId === undefined || rawPoolId === null) { return res.status(400).json({ success: false, error: 'missing pool ID' }); } - if (typeof id !== 'string') { + if (typeof rawPoolId !== 'string') { return res .status(400) .json({ success: false, error: 'The pool ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const poolId = rawPoolId.trim(); - if (sanitizedId.length === 0) { + if (!poolId) { return res.status(400).json({ success: false, error: 'The pool ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(poolId)) { return res.status(400).json({ success: false, error: 'Invalid pool ID provided' }); } // Check team id - if (!teamId) { + if (rawTeamId === undefined || rawTeamId === null) { return res.status(400).json({ success: false, error: 'missing team ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedTeamId = String(teamId).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedTeamId.length === 0) { + if (!teamId) { return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedTeamId)) { + if (!ObjectId.isValid(teamId)) { return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Get pool - const pool = await db.collection('pool').findOne({ _id: new ObjectId(sanitizedId) }); + const pool = await db.collection('pool').findOne({ _id: new ObjectId(poolId) }); if (!pool) { return res.status(404).json({ success: false, error: 'Pool with that ID not found' }); } // Get team - const team = await db.collection('team').findOne({ _id: new ObjectId(sanitizedTeamId) }); + const team = await db.collection('team').findOne({ _id: new ObjectId(teamId) }); if (!team) { return res.status(404).json({ success: false, error: 'Team with that ID not found' }); } // Team pool - const teamPool = await db.collection('teampool').findOne({ - teamId: sanitizedTeamId, - poolId: sanitizedId - }); + const teamPool = await db.collection('teampool').findOne({ teamId, poolId }); if (teamPool) { return res @@ -83,15 +80,13 @@ module.exports = (db) => { } // Add to database - const result = await db.collection('teampool').insertOne({ - teamId: sanitizedTeamId, - poolId: sanitizedId - }); + const insertedId = (await db.collection('teampool').insertOne({ teamId, poolId })) + .insertedId; return res.status(200).json({ success: true, body: { - newId: result.insertedId + newId: insertedId } }); } catch (error) { diff --git a/backend/endpoints/pool/controller/createPool.js b/backend/endpoints/pool/controller/createPool.js index fbd7502..3d37d15 100644 --- a/backend/endpoints/pool/controller/createPool.js +++ b/backend/endpoints/pool/controller/createPool.js @@ -9,29 +9,29 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { name } = req.body || {}; + const { name: rawName } = req.body || {}; // Check name - if (!name) { + if (rawName === undefined || rawName === null) { return res.status(400).json({ success: false, error: "Missing pool's name" }); } - if (typeof name !== 'string') { + if (typeof rawName !== 'string') { return res .status(400) .json({ success: false, error: 'The name provided is not a string' }); } - const sanitizedName = String(name).trim(); + const name = rawName.trim(); - if (sanitizedName.length === 0) { + if (!name) { return res.status(400).json({ success: false, error: 'The name provided is empty' }); } // Make sure the person exists const person = await db.collection('pool').findOne({ name: { - $regex: `^${sanitizedName}$`, + $regex: `^${name}$`, $options: 'i' } }); @@ -43,14 +43,12 @@ module.exports = (db) => { } // Add pool to the database - const result = await db.collection('pool').insertOne({ - name: sanitizedName - }); + const insertedId = (await db.collection('pool').insertOne({ name })).insertedId; return res.status(200).json({ success: true, body: { - newId: result.insertedId + newId: insertedId } }); } catch (error) { diff --git a/backend/endpoints/pool/controller/deletePool.js b/backend/endpoints/pool/controller/deletePool.js index 433cbfd..839c49d 100644 --- a/backend/endpoints/pool/controller/deletePool.js +++ b/backend/endpoints/pool/controller/deletePool.js @@ -11,31 +11,31 @@ module.exports = (db) => { */ return async (req, res) => { try { - const id = req.params?.id || req.body?.id || req.query?.id; + const rawPoolId = req.params?.id || req.body?.id || req.query?.id; - // Check id - if (!id) { + // Check pool id + if (rawPoolId === undefined || rawPoolId === null) { return res.status(400).json({ success: false, error: 'Missing pool ID' }); } - if (typeof id !== 'string') { + if (typeof rawPoolId !== 'string') { return res .status(400) .json({ success: false, error: 'The pool ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const poolId = rawPoolId.trim(); - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(poolId)) { return res.status(400).json({ success: false, error: 'Invalid pool ID provided' }); } // Check if pool exits - const existingPerson = await db.collection('pool').findOne({ - _id: new ObjectId(sanitizedId) + const person = await db.collection('pool').findOne({ + _id: new ObjectId(poolId) }); - if (!existingPerson) { + if (!person) { return res.status(409).json({ success: false, error: "Pool doesn't exist" @@ -43,9 +43,7 @@ module.exports = (db) => { } // Check if pool has clusters - const hasClusters = await db.collection('cluster').findOne({ - poolId: sanitizedId - }); + const hasClusters = await db.collection('cluster').findOne({ poolId }); if (hasClusters) { return res @@ -54,9 +52,7 @@ module.exports = (db) => { } // Check if pool is assigned to a team - const isAssigned = await db.collection('teampool').findOne({ - poolId: sanitizedId - }); + const isAssigned = await db.collection('teampool').findOne({ poolId }); if (isAssigned) { return res @@ -66,7 +62,7 @@ module.exports = (db) => { // Delete the pool from the database await db.collection('pool').deleteOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(poolId) }); return res.status(200).json({ success: true }); diff --git a/backend/endpoints/pool/controller/getAllPools.js b/backend/endpoints/pool/controller/getAllPools.js index a5f1ee8..5c1cc5d 100644 --- a/backend/endpoints/pool/controller/getAllPools.js +++ b/backend/endpoints/pool/controller/getAllPools.js @@ -14,9 +14,9 @@ module.exports = (db) => { .find({}) .toArray() .then((res) => - res.map((data) => ({ - id: data._id.toString(), - name: data.name + res.map(({ _id, ...rest }) => ({ + id: _id.toString(), + ...rest })) ); diff --git a/backend/endpoints/pool/controller/getPool.js b/backend/endpoints/pool/controller/getPool.js index 803bc2b..49bb429 100644 --- a/backend/endpoints/pool/controller/getPool.js +++ b/backend/endpoints/pool/controller/getPool.js @@ -11,45 +11,42 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawPoolId } = req.params || {}; - // Check id - if (!id) { + // Check pool id + if (rawPoolId === undefined || rawPoolId === null) { return res.status(400).json({ success: false, error: 'Missing pool ID' }); } - if (typeof id !== 'string') { + if (typeof rawPoolId !== 'string') { return res .status(400) .json({ success: false, error: 'The pool ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const poolId = rawPoolId.trim(); - if (sanitizedId.length === 0) { + if (!poolId) { return res.status(400).json({ success: false, error: 'The pool ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(poolId)) { return res.status(400).json({ success: false, error: 'Invalid pool ID provided' }); } // Get the pool from the database const pool = await db.collection('pool').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(poolId) }); if (!pool) { return res.status(404).json({ success: false, error: "Pool doesn't exist" }); } - return res.status(200).json({ - success: true, - body: { - id: sanitizedId, - name: pool.name - } - }); + pool.id = pool._id.toString(); + delete pool._id; + + return res.status(200).json({ success: true, body: pool }); } catch (error) { return res.status(500).json({ success: false, error: error.message }); } diff --git a/backend/endpoints/pool/controller/getPoolByCluster.js b/backend/endpoints/pool/controller/getPoolByCluster.js index 0c92e93..5685d88 100644 --- a/backend/endpoints/pool/controller/getPoolByCluster.js +++ b/backend/endpoints/pool/controller/getPoolByCluster.js @@ -11,34 +11,34 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawClusterId } = req.params || {}; - // Check id - if (!id) { + // Check cluster id + if (rawClusterId === undefined || rawClusterId === null) { return res.status(400).json({ success: false, error: 'Missing cluster ID' }); } - if (typeof id !== 'string') { + 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' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(clusterId)) { return res.status(400).json({ success: false, error: 'Invalid cluster ID provided' }); } // Get the cluster from the database const cluster = await db.collection('cluster').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(clusterId) }); if (!cluster) { @@ -49,13 +49,10 @@ module.exports = (db) => { return res.status(404).json({ success: false, error: 'Cluster not assigned to pool' }); } - return res.status(200).json({ - success: true, - body: { - id: sanitizedId, - pool: cluster.poolId - } - }); + cluster.id = cluster._id.toString(); + delete cluster._id; + + return res.status(200).json({ success: true, body: cluster }); } catch (error) { return res.status(500).json({ success: false, error: error.message }); } diff --git a/backend/endpoints/pool/controller/getPoolsByTeam.js b/backend/endpoints/pool/controller/getPoolsByTeam.js index 3023376..62bcf88 100644 --- a/backend/endpoints/pool/controller/getPoolsByTeam.js +++ b/backend/endpoints/pool/controller/getPoolsByTeam.js @@ -11,31 +11,31 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawTeamId } = req.params || {}; - // Check id - if (!id) { + // Check team id + if (rawTeamId === undefined || rawTeamId === null) { return res.status(400).json({ success: false, error: 'Missing team ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedId.length === 0) { + if (!teamId) { return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(teamId)) { return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } const pool = await db.collection('team').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(teamId) }); if (!pool) { @@ -43,7 +43,7 @@ module.exports = (db) => { } // Get the teamPool - const teamPools = await db.collection('teampool').find({ teamId: sanitizedId }).toArray(); + const teamPools = await db.collection('teampool').find({ teamId }).toArray(); if (teamPools.length === 0) { return res.status(404).json({ success: false, error: 'Team does not have any pools' }); @@ -57,9 +57,9 @@ module.exports = (db) => { return res.json({ success: true, - body: allPools.map(({ _id, ...pool }) => ({ - id: _id, - ...pool + body: allPools.map(({ _id, ...rest }) => ({ + id: _id.toString(), + ...rest })) }); } catch (error) { diff --git a/backend/endpoints/pool/controller/getPoolsNotInTeam.js b/backend/endpoints/pool/controller/getPoolsNotInTeam.js index af13fc4..99c8e43 100644 --- a/backend/endpoints/pool/controller/getPoolsNotInTeam.js +++ b/backend/endpoints/pool/controller/getPoolsNotInTeam.js @@ -11,32 +11,32 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawTeamId } = req.params || {}; - // Check id - if (!id) { + // Check team id + if (rawTeamId === undefined || rawTeamId === null) { return res.status(400).json({ success: false, error: 'Missing team ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedId.length === 0) { + if (!teamId) { return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(teamId)) { return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Get pool const pool = await db.collection('team').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(teamId) }); if (!pool) { @@ -44,7 +44,7 @@ module.exports = (db) => { } // Get the teamPool - const teamPools = await db.collection('teampool').find({ teamId: sanitizedId }).toArray(); + const teamPools = await db.collection('teampool').find({ teamId }).toArray(); if (teamPools.length === 0) { return res.status(404).json({ success: false, error: 'Team does not have any pools' }); @@ -61,9 +61,9 @@ module.exports = (db) => { return res.json({ success: true, - body: allPools.map(({ _id, ...pool }) => ({ + body: allPools.map(({ _id, ...rest }) => ({ id: _id, - ...pool + ...rest })) }); } catch (error) { diff --git a/backend/endpoints/pool/controller/removePoolFromTeam.js b/backend/endpoints/pool/controller/removePoolFromTeam.js index ca07909..b01eb50 100644 --- a/backend/endpoints/pool/controller/removePoolFromTeam.js +++ b/backend/endpoints/pool/controller/removePoolFromTeam.js @@ -11,70 +11,67 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { teamId } = req.body || {}; - const { id } = req.params || {}; + const { id: rawPoolId } = req.params || {}; + const { teamId: rawTeamId } = req.body || {}; - // Check id - if (!id) { + // Check pool id + if (rawPoolId === undefined || rawPoolId === null) { return res.status(400).json({ success: false, error: 'missing pool ID' }); } - if (typeof id !== 'string') { + if (typeof rawPoolId !== 'string') { return res .status(400) .json({ success: false, error: 'The pool ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const poolId = rawPoolId.trim(); - if (sanitizedId.length === 0) { + if (!poolId) { return res.status(400).json({ success: false, error: 'The pool ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(poolId)) { return res.status(400).json({ success: false, error: 'Invalid pool ID provided' }); } // Check team id - if (!teamId) { + if (rawTeamId === undefined || rawTeamId === null) { return res.status(400).json({ success: false, error: 'missing team ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedTeamId = String(teamId).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedTeamId.length === 0) { + if (teamId.length === 0) { return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedTeamId)) { + if (!ObjectId.isValid(teamId)) { return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Get pool - const pool = await db.collection('pool').findOne({ _id: new ObjectId(sanitizedId) }); + const pool = await db.collection('pool').findOne({ _id: new ObjectId(poolId) }); if (!pool) { return res.status(404).json({ success: false, error: 'Pool with that ID not found' }); } // Get team - const team = await db.collection('team').findOne({ _id: new ObjectId(sanitizedTeamId) }); + const team = await db.collection('team').findOne({ _id: new ObjectId(teamId) }); if (!team) { return res.status(404).json({ success: false, error: 'Team with that ID not found' }); } // Check if pool is assigned to team - const teamPool = await db.collection('teampool').findOne({ - teamId: sanitizedTeamId, - poolId: sanitizedId - }); + const teamPool = await db.collection('teampool').findOne({ teamId, poolId }); if (!teamPool) { return res @@ -83,10 +80,7 @@ module.exports = (db) => { } // Delete from database - await db.collection('teampool').deleteOne({ - teamId: sanitizedTeamId, - poolId: sanitizedId - }); + await db.collection('teampool').deleteOne({ teamId, poolId }); return res.status(200).json({ success: true }); } catch (error) { diff --git a/backend/endpoints/team/controller/addTeam.js b/backend/endpoints/team/controller/addTeam.js index 121bf3f..e83f2ee 100644 --- a/backend/endpoints/team/controller/addTeam.js +++ b/backend/endpoints/team/controller/addTeam.js @@ -9,28 +9,28 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { name } = req.body || {}; + const { name: rawName } = req.body || {}; // Check name - if (!name) { + if (rawName === undefined || rawName === null) { return res.status(400).json({ success: false, error: "Missing team's name" }); } - if (typeof name !== 'string') { + if (typeof rawName !== 'string') { return res .status(400) .json({ success: false, error: 'The name provided is not a string' }); } - const sanitizedName = String(name).trim(); + const name = rawName.trim(); - if (sanitizedName.length == 0) { + if (!name) { return res.status(400).json({ success: false, error: 'The name provided is empty' }); } const existingTeam = await db.collection('team').findOne({ name: { - $regex: `^${sanitizedName}$`, + $regex: `^${name}$`, $options: 'i' } }); @@ -41,7 +41,7 @@ module.exports = (db) => { // Add team to database await db.collection('team').insertOne({ - name: sanitizedName, + name, clusters_per_day: 1 }); diff --git a/backend/endpoints/team/controller/deleteTeam.js b/backend/endpoints/team/controller/deleteTeam.js index d7a6bf2..227bf06 100644 --- a/backend/endpoints/team/controller/deleteTeam.js +++ b/backend/endpoints/team/controller/deleteTeam.js @@ -11,32 +11,32 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params; + const { id: rawTeamId } = req.params || {}; - // Check id - if (!id) { + // Check team id + if (rawTeamId === undefined || rawTeamId === null) { return res.status(400).json({ success: false, error: 'Missing team ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedId.length === 0) { + if (!teamId) { return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(teamId)) { return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Check if team exists const existingTeam = await db.collection('team').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(teamId) }); if (!existingTeam) { @@ -44,24 +44,14 @@ module.exports = (db) => { } // Check if people are linked to the team - const people = await db - .collection('person') - .find({ - teamId: sanitizedId - }) - .toArray(); + const people = await db.collection('person').find({ teamId }).toArray(); if (people.length > 0) { return res.status(409).json({ success: false, error: 'This team has people' }); } // Check if clusters are linked to the team - const clusters = await db - .collection('teampool') - .find({ - teamId: sanitizedId - }) - .toArray(); + const clusters = await db.collection('teampool').find({ teamId }).toArray(); if (clusters.length > 0) { return res.status(409).json({ success: false, error: 'This team has pools' }); @@ -69,7 +59,7 @@ module.exports = (db) => { // Delete the team await db.collection('team').deleteOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(teamId) }); return res.status(200).json({ success: true }); diff --git a/backend/endpoints/team/controller/getTeamById.js b/backend/endpoints/team/controller/getTeamById.js index 484555f..ddbefc1 100644 --- a/backend/endpoints/team/controller/getTeamById.js +++ b/backend/endpoints/team/controller/getTeamById.js @@ -11,32 +11,32 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawTeamId } = req.params || {}; // Check id - if (!id) { + if (rawTeamId === undefined || rawTeamId === null) { return res.status(400).json({ success: false, error: 'Missing teams ID' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedId.length === 0) { + if (!teamId) { return res.status(400).json({ success: false, error: 'The team ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(teamId)) { return res.status(400).json({ success: false, error: 'Invalid team ID provided' }); } // Get the team const response = await db.collection('team').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(teamId) }); if (!response) { diff --git a/backend/endpoints/team/controller/getTeamByName.js b/backend/endpoints/team/controller/getTeamByName.js index 02c92e8..3046f1d 100644 --- a/backend/endpoints/team/controller/getTeamByName.js +++ b/backend/endpoints/team/controller/getTeamByName.js @@ -9,29 +9,29 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { name } = req.params || {}; + const { name: rawName } = req.params || {}; // Check name - if (!name) { + if (rawName === undefined || rawName === null) { return res.status(400).json({ success: false, error: 'Missing team name' }); } - if (typeof name !== 'string') { + if (typeof rawName !== 'string') { return res .status(400) .json({ success: false, error: 'The team name provided is not a string' }); } - const sanitizedName = String(name).trim(); + const name = rawName.trim(); - if (sanitizedName.length == 0) { + if (!name) { return res.status(400).json({ success: false, error: 'The name provided is empty' }); } // Get team const response = await db.collection('team').findOne({ name: { - $regex: `^${sanitizedName}$`, + $regex: `^${name}$`, $options: 'i' } }); diff --git a/backend/endpoints/team/controller/getTeamByPool.js b/backend/endpoints/team/controller/getTeamByPool.js index 7e1056d..9349b2c 100644 --- a/backend/endpoints/team/controller/getTeamByPool.js +++ b/backend/endpoints/team/controller/getTeamByPool.js @@ -11,31 +11,31 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id } = req.params || {}; + const { id: rawPoolId } = req.params || {}; - // Check id - if (!id) { + // Check pool id + if (rawPoolId === undefined || rawPoolId === null) { return res.status(400).json({ success: false, error: 'Missing pool ID' }); } - if (typeof id !== 'string') { + if (typeof rawPoolId !== 'string') { return res .status(400) .json({ success: false, error: 'The team ID provided is not a string' }); } - const sanitizedId = String(id).trim(); + const poolId = rawPoolId.trim(); - if (sanitizedId.length === 0) { + if (!poolId) { return res.status(400).json({ success: false, error: 'The pool ID provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(poolId)) { return res.status(400).json({ success: false, error: 'Invalid pool ID provided' }); } const pool = await db.collection('pool').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(poolId) }); if (!pool) { @@ -43,7 +43,7 @@ module.exports = (db) => { } // Get the teamPool - const teamPool = await db.collection('teampool').find({ poolId: sanitizedId }).toArray(); + const teamPool = await db.collection('teampool').find({ poolId }).toArray(); if (teamPool.length === 0) { return res.status(404).json({ success: false, error: 'Pool does not have any teams' }); @@ -53,7 +53,7 @@ module.exports = (db) => { return res.status(200).json({ success: true, body: teamPool.map((result) => ({ - poolId: sanitizedId, + poolId, teamId: result.teamId })) }); diff --git a/backend/endpoints/team/controller/updateTeamSettings.js b/backend/endpoints/team/controller/updateTeamSettings.js index dc557fb..a0feeea 100644 --- a/backend/endpoints/team/controller/updateTeamSettings.js +++ b/backend/endpoints/team/controller/updateTeamSettings.js @@ -11,26 +11,26 @@ module.exports = (db) => { */ return async (req, res) => { try { - const { id, ...rest } = req.body || {}; + const { id: rawTeamId, ...rest } = req.body || {}; // Check id - if (!id) { + if (rawTeamId === undefined || rawTeamId === null) { return res.status(400).json({ success: false, error: 'Missing team id' }); } - if (typeof id !== 'string') { + if (typeof rawTeamId !== 'string') { return res .status(400) .json({ success: false, error: 'The team id provided is not a string' }); } - const sanitizedId = String(id).trim(); + const teamId = rawTeamId.trim(); - if (sanitizedId.length === 0) { + if (!teamId) { return res.json(400).json({ success: false, error: 'The team id provided is empty' }); } - if (!ObjectId.isValid(sanitizedId)) { + if (!ObjectId.isValid(teamId)) { return res .status(400) .json({ success: false, error: 'The team id provided was invalid' }); @@ -38,7 +38,7 @@ module.exports = (db) => { // Check team exists const teamExists = await db.collection('team').findOne({ - _id: new ObjectId(sanitizedId) + _id: new ObjectId(teamId) }); if (!teamExists) { @@ -57,9 +57,7 @@ module.exports = (db) => { } // Updates values in the database - await db - .collection('team') - .updateOne({ _id: new ObjectId(sanitizedId) }, { $set: updates }); + await db.collection('team').updateOne({ _id: new ObjectId(teamId) }, { $set: updates }); return res.status(200).json({ success: true }); } catch (error) {