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 identities service for improved structure and asynchronicity #342

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3771dfa
refactoring identities service
vsun757 Nov 21, 2023
8ab8a79
refactored service part 2
vsun757 Nov 21, 2023
2ec3929
repo refactored
vsun757 Nov 21, 2023
54ee716
refactored tests
vsun757 Nov 21, 2023
fe96400
refactoring controller
vsun757 Nov 21, 2023
cb93ca6
finished refactor
vsun757 Nov 21, 2023
a2f5d7e
fixing an issue
vsun757 Nov 22, 2023
453c31f
proper stix id
vsun757 Dec 8, 2023
ee2bb6d
Merge branch 'project-orion' into 299-refactor-identities-service-for…
vsun757 Dec 18, 2023
2f7a679
switching identities service to use repository create
vsun757 Dec 19, 2023
f852a00
fixing issues
vsun757 Dec 19, 2023
5d598fe
trying to re-add needed functions
vsun757 Dec 29, 2023
84c3dfd
debugging
vsun757 Jan 10, 2024
d11f4f3
fixing some errors
vsun757 Jan 22, 2024
d33f4ad
Merge branch 'project-orion' into 299-refactor-identities-service-for…
vsun757 Jan 23, 2024
782753f
fixing merge conflicts
vsun757 Jan 23, 2024
ffc645a
Merge branch 'project-orion' into 299-refactor-identities-service-for…
vsun757 Jan 23, 2024
f6c3a04
all tests pass
vsun757 Jan 23, 2024
64fc622
fixing lint issues
vsun757 Jan 23, 2024
b10e229
more lint fixes
vsun757 Jan 23, 2024
099d9ab
almost done with lint issues
vsun757 Jan 23, 2024
1ae728d
almost all linter issues gone
vsun757 Jan 24, 2024
d3e826a
finished with lint
vsun757 Jan 25, 2024
61b0864
removing extra error logging
vsun757 Jan 26, 2024
98b014d
Merge branch 'project-orion' into 299-refactor-identities-service-for…
Jan 29, 2024
311bfba
Remove identity service functions that duplicate base service functions.
Jan 29, 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
200 changes: 98 additions & 102 deletions app/controllers/identities-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const identitiesService = require('../services/identities-service');
const logger = require('../lib/logger');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError } = 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,
Expand All @@ -13,76 +14,75 @@ exports.retrieveAll = function(req, res) {
includePagination: req.query.includePagination
}

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

};

exports.retrieveById = function(req, res) {
exports.retrieveById = async function(req, res) {
const options = {
versions: req.query.versions || 'latest'
}

identitiesService.retrieveById(req.params.stixId, options, function (err, identities) {
if (err) {
if (err.message === identitiesService.errors.badlyFormattedParameter) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else if (err.message === identitiesService.errors.invalidQueryStringParameter) {
logger.warn('Invalid query string: versions=' + req.query.versions);
return res.status(400).send('Query string parameter versions is invalid.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get identities. Server error.');
}
try {
const identities = await identitiesService.retrieveById(req.params.stixId, options);
if (identities.length === 0) {
return res.status(404).send('Identity not found.');
}
else {
logger.debug(`Success: Retrieved ${ identities.length } identities with id ${ req.params.stixId }`);
return res.status(200).send(identities);
}
} catch (err) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else if (err instanceof InvalidQueryStringParameterError) {
logger.warn('Invalid query string: versions=' + req.query.versions);
return res.status(400).send('Query string parameter versions is invalid.');
}
else {
if (identities.length === 0) {
return res.status(404).send('Identity not found.');
}
else {
logger.debug(`Success: Retrieved ${ identities.length } identities with id ${ req.params.stixId }`);
return res.status(200).send(identities);
}
}
});
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get identities. Server error.');
}
}

};

exports.retrieveVersionById = function(req, res) {
identitiesService.retrieveVersionById(req.params.stixId, req.params.modified, function (err, identity) {
if (err) {
if (err.message === identitiesService.errors.badlyFormattedParameter) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get identity. Server error.');
}
} else {
if (!identity) {
return res.status(404).send('Identity not found.');
}
else {
logger.debug(`Success: Retrieved identity with id ${identity.id}`);
return res.status(200).send(identity);
}
}
});
exports.retrieveVersionById = async function(req, res) {

try {
const identity = await identitiesService.retrieveVersionById(req.params.stixId, req.params.modified);
if (!identity) {
return res.status(404).send('Identity not found.');
}
else {
logger.debug(`Success: Retrieved identity with id ${identity.id}`);
return res.status(200).send(identity);
}
} catch (err) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get identity. Server error.');
}
}

};

exports.create = async function(req, res) {
Expand All @@ -100,7 +100,7 @@ exports.create = async function(req, res) {
return res.status(201).send(identity);
}
catch(err) {
if (err.message === identitiesService.errors.duplicateId) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create identity. Duplicate stix.id and stix.modified properties.');
}
Expand All @@ -111,58 +111,54 @@ exports.create = async function(req, res) {
}
};

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

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

exports.deleteVersionById = function(req, res) {
identitiesService.deleteVersionById(req.params.stixId, req.params.modified, function (err, identity) {
if (err) {
logger.error('Delete identity failed. ' + err);
return res.status(500).send('Unable to delete identity. Server error.');
exports.deleteVersionById = async function(req, res) {

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

exports.deleteById = function(req, res) {
identitiesService.deleteById(req.params.stixId, function (err, identities) {
if (err) {
logger.error('Delete identity failed. ' + err);
return res.status(500).send('Unable to identity identity. Server error.');
exports.deleteById = async function(req, res) {

try {
const identities = await identitiesService.deleteById(req.params.stixId);
if (identities.deletedCount === 0) {
return res.status(404).send('Identity not found.');
}
else {
if (identities.deletedCount === 0) {
return res.status(404).send('Identity not found.');
}
else {
logger.debug(`Success: Deleted identity with id ${ req.params.stixId }`);
return res.status(204).end();
}
}
});
logger.debug(`Success: Deleted identity with id ${ req.params.stixId }`);
return res.status(204).end();
}
} catch (err) {
logger.error('Delete identity failed. ' + err);
return res.status(500).send('Unable to identity identity. Server error.');
}
};
8 changes: 8 additions & 0 deletions app/repository/identities-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const BaseRepository = require('./_base.repository');
const Identity = require('../models/identity-model');

class IdentitiesRepository extends BaseRepository { }

module.exports = new IdentitiesRepository(Identity);
3 changes: 0 additions & 3 deletions app/services/_base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ class BaseService extends AbstractService {
return callback(identityError);
}
throw identityError;

}

const paginatedResults = BaseService.paginate(options, results);
if (callback) {
return callback(null, paginatedResults);
}
return paginatedResults;

}

async retrieveById(stixId, options, callback) {
Expand Down Expand Up @@ -200,7 +198,6 @@ class BaseService extends AbstractService {
const document = await this.repository.retrieveOneByVersion(stixId, modified);

if (!document) {
console.log('** NOT FOUND');
if (callback) {
return callback(null, null);
}
Expand Down
Loading
Loading