Skip to content

Commit

Permalink
Merge pull request #348 from center-for-threat-informed-defense/289-r…
Browse files Browse the repository at this point in the history
…efactor-teams

Refactored teams service
  • Loading branch information
ElJocko authored Mar 6, 2024
2 parents 1100bfc + 7f3178d commit 944e251
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 506 deletions.
Empty file.
158 changes: 76 additions & 82 deletions app/controllers/teams-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,52 @@

const teamsService = require('../services/teams-service');
const logger = require('../lib/logger');
const { NotFoundError, BadlyFormattedParameterError, DuplicateIdError } = require('../exceptions');

exports.retrieveAll = function(req, res) {
exports.retrieveAll = async function(req, res) {
const options = {
offset: req.query.offset || 0,
limit: req.query.limit || 0,
search: req.query.search,
includePagination: req.query.includePagination,
};

teamsService.retrieveAll(options, function(err, results) {
if (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get teams. Server error.');
try {
const results = await teamsService.retrieveAll(options);
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total team(s)`);
}
else {
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total team(s)`);
}
else {
logger.debug(`Success: Retrieved ${ results.length } team(s)`);
}
return res.status(200).send(results);
logger.debug(`Success: Retrieved ${ results.length } team(s)`);
}
});
return res.status(200).send(results);
} catch (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get teams. Server error.');
}
};

exports.retrieveById = function(req, res) {
teamsService.retrieveById(req.params.id, function (err, team) {
if (err) {
if (err.message === teamsService.errors.badlyFormattedParameter) {
logger.warn('Badly formatted teams id: ' + req.params.id);
return res.status(400).send('Team id is badly formatted.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get team. Server error.');
}
exports.retrieveById = async function(req, res) {

try {
const team = await teamsService.retrieveById(req.params.id);
if (!team) {
return res.status(404).send('TEam not found.');
}
else {
if (!team) {
return res.status(404).send('TEam not found.');
}
else {
logger.debug(`Success: Retrieved team with id ${ req.params.id }`);
return res.status(200).send(team);
}
logger.debug(`Success: Retrieved team with id ${ req.params.id }`);
return res.status(200).send(team);
}
} catch (err) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted teams id: ' + req.params.id);
return res.status(400).send('Team id is badly formatted.');
}
});
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get team. Server error.');
}
}
};

exports.create = async function(req, res) {
Expand All @@ -63,56 +61,53 @@ exports.create = async function(req, res) {
return res.status(201).send(team);
}
catch(err) {
if (err.message === teamsService.errors.duplicateName) {
if (err instanceof DuplicateIdError) {
logger.error("Duplicated team name");
return res.status(409).send('Team name must be unique.');
} else {
}
else {
logger.error("Failed with error: " + err);
return res.status(500).send('Unable to create team. Server error.');
}

}
};

exports.updateFull = function(req, res) {
exports.updateFull = async function(req, res) {
// Get the data from the request
const teamData = req.body;

// Create the technique
teamsService.updateFull(req.params.id, teamData, function(err, team) {
if (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update team. Server error.");
}
else {
if (!team) {
return res.status(404).send('Team not found.');
} else {
logger.debug("Success: Updated team with id " + team.id);
return res.status(200).send(team);
}
try {
// Create the technique
const team = await teamsService.updateFull(req.params.id, teamData);
if (!team) {
return res.status(404).send('Team not found.');
} else {
logger.debug("Success: Updated team with id " + team.id);
return res.status(200).send(team);
}
});
} catch (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update team. Server error.");
}
};

exports.delete = function(req, res) {
teamsService.delete(req.params.id, function (err, team) {
if (err) {
logger.error('Delete team failed. ' + err);
return res.status(500).send('Unable to delete team. Server error.');
}
else {
if (!team) {
return res.status(404).send('Team not found.');
} else {
logger.debug("Success: Deleted team with id " + team.id);
return res.status(204).end();
}
exports.delete = async function(req, res) {

try {
const team = await teamsService.delete(req.params.id);
if (!team) {
return res.status(404).send('Team not found.');
} else {
logger.debug("Success: Deleted team with id " + team.id);
return res.status(204).end();
}
});
} catch (err) {
logger.error('Delete team failed. ' + err);
return res.status(500).send('Unable to delete team. Server error.');
}
};

exports.retrieveAllUsers = function(req, res) {
exports.retrieveAllUsers = async function(req, res) {
const options = {
offset: req.query.offset || 0,
limit: req.query.limit || 0,
Expand All @@ -125,24 +120,23 @@ exports.retrieveAllUsers = function(req, res) {

const teamId = req.params.id;

teamsService.retrieveAllUsers(teamId, options,function (err, results) {
if (err) {
if (err.message === teamsService.errors.notFound) {
logger.error(`Could not find team with with id ${teamId}. `);
return res.status(404).send('Team not found');
} else {
logger.error('Retrieve users from teamId failed. ' + err);
return res.status(500).send('Unable to retrieve users. Server error.');
}
try {
const results = await teamsService.retrieveAllUsers(teamId, options);
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total user account(s)`);
}
else {
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total user account(s)`);
}
else {
logger.debug(`Success: Retrieved ${ results.length } user account(s)`);
}
return res.status(200).send(results);
logger.debug(`Success: Retrieved ${ results.length } user account(s)`);
}
});
return res.status(200).send(results);
} catch(err) {
if (err instanceof NotFoundError) {
logger.error(`Could not find team with with id ${teamId}. `);
return res.status(404).send('Team not found');
} else {
logger.error('Retrieve users from teamId failed. ' + err);
return res.status(500).send('Unable to retrieve users. Server error.');
}
}

};
4 changes: 2 additions & 2 deletions app/controllers/user-accounts-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const userAccountsService = require('../services/user-accounts-service');
const logger = require('../lib/logger');
const config = require('../config/config');
const { BadlyFormattedParameterError } = require('../exceptions');
const { BadlyFormattedParameterError, DuplicateEmailError } = require('../exceptions');

exports.retrieveAll = async function (req, res) {
const options = {
Expand Down Expand Up @@ -78,7 +78,7 @@ exports.create = async function(req, res) {
return res.status(201).send(userAccount);
}
catch(err) {
if (err.message === userAccountsService.errors.duplicateEmail) {
if (err instanceof DuplicateEmailError) {
logger.warn(`Unable to create user account, duplicate email: ${ userAccountData.email }`);
return res.status(400).send('Duplicate email');
}
Expand Down
14 changes: 14 additions & 0 deletions app/exceptions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ class DuplicateIdError extends CustomError {
}
}

class DuplicateEmailError extends CustomError {
constructor(options) {
super('Duplicate email', options);
}
}

class DuplicateNameError extends CustomError {
constructor(options) {
super('Duplicate name', options);
}
}

class NotFoundError extends CustomError {
constructor(options) {
super('Document not found', options);
Expand Down Expand Up @@ -112,6 +124,8 @@ module.exports = {

//** Database-related errors */
DuplicateIdError,
DuplicateEmailError,
DuplicateNameError,
NotFoundError,
DatabaseError,

Expand Down
6 changes: 3 additions & 3 deletions app/models/team-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const mongoose = require('mongoose');

// Create the definition
const teamDefinition = {
id: {
type: String,
id: {
type: String,
index: {
unique: true
}
}
},
name: { type: String, required: true, unique: true },
description: { type: String },
Expand Down
Loading

0 comments on commit 944e251

Please sign in to comment.