Skip to content

Commit

Permalink
Merge pull request #344 from center-for-threat-informed-defense/287-r…
Browse files Browse the repository at this point in the history
…efactor-useraccounts-service-for-improved-structure-and-asynchronicity

Refactor useraccounts service for improved structure and asynchronicity
  • Loading branch information
ElJocko authored Feb 14, 2024
2 parents 271a806 + ca9e552 commit 1f2b445
Show file tree
Hide file tree
Showing 6 changed files with 534 additions and 713 deletions.
183 changes: 55 additions & 128 deletions app/controllers/user-accounts-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const userAccountsService = require('../services/user-accounts-service');
const logger = require('../lib/logger');
const config = require('../config/config');
const { BadlyFormattedParameterError } = require('../exceptions');

exports.retrieveAllAsync = async function (req, res) {
exports.retrieveAll = async function (req, res) {
const options = {
offset: req.query.offset || 0,
limit: req.query.limit || 0,
Expand All @@ -16,53 +17,29 @@ exports.retrieveAllAsync = async function (req, res) {
};

try {

const results = await userAccountsService.retrieveAll(options);

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);
}
catch (error) {
logger.error('Failed with error: ' + error);
} catch (err) {
console.log("retrieveall");
console.log(err);
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get user accounts. Server error.');
}
};

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

userAccountsService.retrieveAll(options, function (err, results) {
if (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get user accounts. Server error.');
}
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);
}
});
};

exports.retrieveByIdAsync = async function (req, res) {
const options = {
includeStixIdentity: req.query.includeStixIdentity
};
try {
const userAccount = await userAccountsService.retrieveById(req.params.id, options);
if (!userAccount) {
Expand All @@ -73,7 +50,7 @@ exports.retrieveByIdAsync = async function (req, res) {
return res.status(200).send(userAccount);
}
} catch (err) {
if (err.message === userAccountsService.errors.badlyFormattedParameter) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted user account id: ' + req.params.id);
return res.status(400).send('User account id is badly formatted.');
}
Expand All @@ -84,34 +61,6 @@ exports.retrieveByIdAsync = async function (req, res) {
}
};

exports.retrieveById = function (req, res) {
const options = {
includeStixIdentity: req.query.includeStixIdentity
};

userAccountsService.retrieveById(req.params.id, options, function (err, userAccount) {
if (err) {
if (err.message === userAccountsService.errors.badlyFormattedParameter) {
logger.warn('Badly formatted user account id: ' + req.params.id);
return res.status(400).send('User account id is badly formatted.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get user account. Server error.');
}
}
else {
if (!userAccount) {
return res.status(404).send('User account not found.');
}
else {
logger.debug(`Success: Retrieved user account with id ${req.params.id}`);
return res.status(200).send(userAccount);
}
}
});
};

exports.create = async function(req, res) {
// Get the data from the request
const userAccountData = req.body;
Expand All @@ -134,6 +83,8 @@ exports.create = async function(req, res) {
return res.status(400).send('Duplicate email');
}
else {
console.log("create");
console.log(err);
logger.error("Failed with error: " + err);
return res.status(500).send('Unable to create user account. Server error.');
}
Expand All @@ -151,30 +102,27 @@ exports.updateFullAsync = async function (req, res) {
return res.status(200).send(userAccount);
}
} catch (err) {
console.log("updatefullasync");
console.log(err);
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update user account. Server error.");
}
};

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

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

exports.deleteAsync = async function (req, res) {
Expand All @@ -187,26 +135,29 @@ exports.deleteAsync = async function (req, res) {
return res.status(204).end();
}
} catch (err) {
console.log("deleteasync");
console.log(err);
logger.error('Delete user account failed. ' + err);
return res.status(500).send('Unable to delete user account. Server error.');
}
};

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

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

exports.register = async function(req, res) {
Expand Down Expand Up @@ -244,6 +195,8 @@ exports.register = async function(req, res) {
return res.status(201).send(userAccount);
}
catch(err) {
console.log("register");
console.log(err);
if (err.message === userAccountsService.errors.duplicateEmail) {
logger.warn(`Unable to register user account, duplicate email: ${ userAccountData.email }`);
return res.status(400).send('Duplicate email');
Expand All @@ -255,18 +208,17 @@ exports.register = async function(req, res) {
}
};

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

const userId = req.params.id;

try {
const results = await userAccountsService.retrieveTeamsByUserId(userId, options);
const results = await userAccountsService.constructor.retrieveTeamsByUserId(userId, options);
if (options.includePagination) {
logger.debug(`Success: Retrieved ${results.data.length} of ${results.pagination.total} total team(s)`);
}
Expand All @@ -275,34 +227,9 @@ exports.retrieveTeamsByUserIdAsync = async function (req, res) {
}
return res.status(200).send(results);
} catch (err) {
console.log("retrieveTeamsByUserId");
console.log(err);
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get teams. Server error.');
}
};

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

const userId = req.params.id;

userAccountsService.retrieveTeamsByUserId(userId, options, function (err, results) {
if (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get teams. Server error.');
}
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);
}
});
};
2 changes: 1 addition & 1 deletion app/repository/teams-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const BaseRepository = require('./_base.repository');
class TeamsRepository extends BaseRepository {

// TODO decouple DB logic; migrate DB logic to DAO/repository class
findTeamsByUserId(userAccountId, options) {
retrieveByUserId(userAccountId, options) {
const aggregation = [
{ $sort: { 'name': 1 } },
{ $match: { userIDs: { $in: [userAccountId] } } },
Expand Down
Loading

0 comments on commit 1f2b445

Please sign in to comment.