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

Refactor useraccounts service for improved structure and asynchronicity #344

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
96ba716
refactoring user accounts service
vsun757 Jan 30, 2024
07a523f
finished refactoring service
vsun757 Jan 30, 2024
2557f60
finished controller
vsun757 Jan 30, 2024
cd0c569
refactored tests
vsun757 Jan 30, 2024
23db166
making some more changes
vsun757 Jan 30, 2024
1b26abe
adding back retrieve all
vsun757 Feb 1, 2024
2eacd19
restoring updatefull
vsun757 Feb 1, 2024
583a3c4
no longer uses base service
vsun757 Feb 1, 2024
17c704d
more improvements
vsun757 Feb 1, 2024
bfdcfc2
removing base service from function calls
vsun757 Feb 1, 2024
f3e5bad
migrating function calls to repository
vsun757 Feb 1, 2024
ae9ec8f
repo calls part 2
vsun757 Feb 1, 2024
e72f025
more bug fixes
vsun757 Feb 1, 2024
728eb59
tests now run
vsun757 Feb 1, 2024
28f147d
more tests pass
vsun757 Feb 1, 2024
82aea21
fixing more issues
vsun757 Feb 5, 2024
8b4ea49
fixed one more test error
vsun757 Feb 5, 2024
8334dcb
a bit closer
vsun757 Feb 5, 2024
6a47bca
making some progress
vsun757 Feb 8, 2024
f235d4e
fixing one more error
vsun757 Feb 8, 2024
c4543c7
restoring some repository calls
vsun757 Feb 8, 2024
13a797f
fixing one repository call
vsun757 Feb 8, 2024
19c6032
cleaning up internal function calls
vsun757 Feb 12, 2024
65a9a21
adding function to repository
vsun757 Feb 12, 2024
e4f2377
trying to fix issues
vsun757 Feb 12, 2024
fc57c8d
made another repository call
vsun757 Feb 12, 2024
3b6b1d4
added extra error handling to repo function
vsun757 Feb 12, 2024
039c173
Reorganize user accounts service and repository.
Feb 14, 2024
ca9e552
Fix problem with accessing static functions.
Feb 14, 2024
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
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 @@
};

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

Check warning on line 33 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L31-L33

Added lines #L31 - L33 were not covered by tests
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 @@
return res.status(200).send(userAccount);
}
} catch (err) {
if (err.message === userAccountsService.errors.badlyFormattedParameter) {
if (err instanceof BadlyFormattedParameterError) {

Check warning on line 53 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L53

Added line #L53 was not covered by tests
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.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 @@
return res.status(400).send('Duplicate email');
}
else {
console.log("create");
console.log(err);

Check warning on line 87 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L86-L87

Added lines #L86 - L87 were not covered by tests
logger.error("Failed with error: " + err);
return res.status(500).send('Unable to create user account. Server error.');
}
Expand All @@ -151,30 +102,27 @@
return res.status(200).send(userAccount);
}
} catch (err) {
console.log("updatefullasync");
console.log(err);

Check warning on line 106 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L105-L106

Added lines #L105 - L106 were not covered by tests
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.');

Check warning on line 117 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L117

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

Check warning on line 125 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L123-L125

Added lines #L123 - L125 were not covered by tests
};

exports.deleteAsync = async function (req, res) {
Expand All @@ -187,26 +135,29 @@
return res.status(204).end();
}
} catch (err) {
console.log("deleteasync");
console.log(err);

Check warning on line 139 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L138-L139

Added lines #L138 - L139 were not covered by tests
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.');

Check warning on line 150 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L150

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

Check warning on line 160 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L156-L160

Added lines #L156 - L160 were not covered by tests
};

exports.register = async function(req, res) {
Expand Down Expand Up @@ -244,6 +195,8 @@
return res.status(201).send(userAccount);
}
catch(err) {
console.log("register");
console.log(err);

Check warning on line 199 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L198-L199

Added lines #L198 - L199 were not covered by tests
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.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 @@
}
return res.status(200).send(results);
} catch (err) {
console.log("retrieveTeamsByUserId");
console.log(err);

Check warning on line 231 in app/controllers/user-accounts-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/user-accounts-controller.js#L230-L231

Added lines #L230 - L231 were not covered by tests
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
Loading