Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored teams service #348

Merged
merged 18 commits into from
Mar 6, 2024
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
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)`);

Check warning on line 18 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L18

Added line #L18 was not covered by tests
}
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.');
}

Check warning on line 27 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L25-L27

Added lines #L25 - L27 were not covered by tests
};

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.');

Check warning on line 44 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L42-L44

Added lines #L42 - L44 were not covered by tests
}
});
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get team. Server error.');
}
}

Check warning on line 50 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L46-L50

Added lines #L46 - L50 were not covered by tests
};

exports.create = async function(req, res) {
Expand All @@ -63,56 +61,53 @@
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.');

Check warning on line 83 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L83

Added line #L83 was not covered by tests
} 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.");
}

Check warning on line 91 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L89-L91

Added lines #L89 - L91 were not covered by tests
};

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.');

Check warning on line 99 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L99

Added line #L99 was not covered by tests
} 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.');
}

Check warning on line 107 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L105-L107

Added lines #L105 - L107 were not covered by tests
};

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 @@

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)`);

Check warning on line 126 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L126

Added line #L126 was not covered by tests
}
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.');
}
}

Check warning on line 140 in app/controllers/teams-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/teams-controller.js#L133-L140

Added lines #L133 - L140 were not covered by tests

};
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 DuplicateEmailError extends CustomError {
constructor(options) {
super('Duplicate email', options);
}
}

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

Check warning on line 44 in app/exceptions/index.js

View check run for this annotation

Codecov / codecov/patch

app/exceptions/index.js#L43-L44

Added lines #L43 - L44 were not covered by tests
}

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

//** 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
Loading