Skip to content
Merged

push #77

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
47 changes: 22 additions & 25 deletions backend/endpoints/method/controller/addMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 12 additions & 12 deletions backend/endpoints/method/controller/deleteMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
26 changes: 13 additions & 13 deletions backend/endpoints/method/controller/getMethodById.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: 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 });
Expand Down
28 changes: 13 additions & 15 deletions backend/endpoints/method/controller/getMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }) => ({
Expand Down
28 changes: 14 additions & 14 deletions backend/endpoints/method/controller/updateMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
16 changes: 7 additions & 9 deletions backend/endpoints/people/controller/addPeople.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
});
Expand All @@ -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) {
Expand Down
Loading
Loading