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 mitigations service for improved structure and asynchronicity #318

208 changes: 102 additions & 106 deletions app/controllers/mitigations-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const mitigationsService = require('../services/mitigations-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 @@ -16,94 +17,92 @@
includePagination: req.query.includePagination
}

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

Check warning on line 32 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L30-L32

Added lines #L30 - L32 were not covered by tests
};

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

mitigationsService.retrieveById(req.params.stixId, options, function (err, mitigations) {
if (err) {
if (err.message === mitigationsService.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 === mitigationsService.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 mitigations. Server error.');
}
try {
const mitigations = await mitigationsService.retrieveById(req.params.stixId, options);
if (mitigations.length === 0) {
return res.status(404).send('Mitigation not found.');
}
else {
if (mitigations.length === 0) {
return res.status(404).send('Mitigation not found.');
}
else {
logger.debug(`Success: Retrieved ${ mitigations.length } mitigation(s) with id ${ req.params.stixId }`);
return res.status(200).send(mitigations);
}
}
});
logger.debug(`Success: Retrieved ${ mitigations.length } mitigation(s) with id ${ req.params.stixId }`);
return res.status(200).send(mitigations);
}
} 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 {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get mitigations. Server error.');
}
}

Check warning on line 62 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L50-L62

Added lines #L50 - L62 were not covered by tests

};

exports.retrieveVersionById = function(req, res) {
mitigationsService.retrieveVersionById(req.params.stixId, req.params.modified, function (err, mitigation) {
if (err) {
if (err.message === mitigationsService.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 mitigation. Server error.');
}
} else {
if (!mitigation) {
return res.status(404).send('Mitigation not found.');
}
else {
logger.debug(`Success: Retrieved mitigation with id ${mitigation.id}`);
return res.status(200).send(mitigation);
}
}
});
exports.retrieveVersionById = async function(req, res) {
try {
const mitigation = await mitigationsService.retrieveVersionById(req.params.stixId, req.params.modified);
if (!mitigation) {
return res.status(404).send('Mitigation not found.');
}

Check warning on line 71 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L70-L71

Added lines #L70 - L71 were not covered by tests
else {
logger.debug(`Success: Retrieved mitigation with id ${mitigation.id}`);
return res.status(200).send(mitigation);
}
} 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 mitigation. Server error.');
}

Check warning on line 85 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L77-L85

Added lines #L77 - L85 were not covered by tests
}

};

exports.create = async function(req, res) {
// Get the data from the request
const mitigationData = req.body;
const options = {
import: false,
userAccountId: req.user?.userAccountId
};

// Create the mitigation
try {
const options = {
import: false,
userAccountId: req.user?.userAccountId
};
const mitigation = await mitigationsService.create(mitigationData, options);
logger.debug("Success: Created mitigation with id " + mitigation.stix.id);
return res.status(201).send(mitigation);
}
catch(err) {
if (err.message === mitigationsService.errors.duplicateId) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create mitigation. Duplicate stix.id and stix.modified properties.');
}
Expand All @@ -114,58 +113,55 @@
}
};

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

// Create the mitigation
mitigationsService.updateFull(req.params.stixId, req.params.modified, mitigationData, function(err, mitigation) {
if (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update mitigation. Server error.");

try {
const mitigation = await mitigationsService.updateFull(req.params.stixId, req.params.modified, mitigationData);
if (!mitigation) {
return res.status(404).send('Mitigation not found.');

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L125

Added line #L125 was not covered by tests
} else {
logger.debug("Success: Updated mitigation with id " + mitigation.stix.id);
return res.status(200).send(mitigation);
}
else {
if (!mitigation) {
return res.status(404).send('Mitigation not found.');
} else {
logger.debug("Success: Updated mitigation with id " + mitigation.stix.id);
return res.status(200).send(mitigation);
}
}
});
} catch (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update mitigation. Server error.");
}

Check warning on line 133 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L131-L133

Added lines #L131 - L133 were not covered by tests

};

exports.deleteVersionById = function(req, res) {
mitigationsService.deleteVersionById(req.params.stixId, req.params.modified, function (err, mitigation) {
if (err) {
logger.error('Delete mitigation failed. ' + err);
return res.status(500).send('Unable to delete mitigation. Server error.');
exports.deleteVersionById = async function(req, res) {
try {
const mitigation = await mitigationsService.deleteVersionById(req.params.stixId, req.params.modified);
if (!mitigation) {
return res.status(404).send('Mitigation not found.');

Check warning on line 141 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L141

Added line #L141 was not covered by tests
} else {
logger.debug("Success: Deleted mitigation with id " + mitigation.stix.id);
return res.status(204).end();
}
else {
if (!mitigation) {
return res.status(404).send('Mitigation not found.');
} else {
logger.debug("Success: Deleted mitigation with id " + mitigation.stix.id);
return res.status(204).end();
}
}
});
} catch (err) {
logger.error('Delete mitigation failed. ' + err);
return res.status(500).send('Unable to delete mitigation. Server error.');
}

Check warning on line 149 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L147-L149

Added lines #L147 - L149 were not covered by tests

};

exports.deleteById = function(req, res) {
mitigationsService.deleteById(req.params.stixId, function (err, mitigations) {
if (err) {
logger.error('Delete mitigation failed. ' + err);
return res.status(500).send('Unable to delete mitigation. Server error.');
exports.deleteById = async function(req, res) {
try {
const mitigations = await mitigationsService.deleteById(req.params.stixId);
if (mitigations.deletedCount === 0) {
return res.status(404).send('Mitigation not found.');
}
else {
if (mitigations.deletedCount === 0) {
return res.status(404).send('Mitigation not found.');
}
else {
logger.debug(`Success: Deleted mitigation with id ${ req.params.stixId }`);
return res.status(204).end();
}
}
});
logger.debug(`Success: Deleted mitigation with id ${ req.params.stixId }`);
return res.status(204).end();
}
} catch (err) {
logger.error('Delete mitigation failed. ' + err);
return res.status(500).send('Unable to delete mitigation. Server error.');
}

Check warning on line 166 in app/controllers/mitigations-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/mitigations-controller.js#L164-L166

Added lines #L164 - L166 were not covered by tests
};
8 changes: 8 additions & 0 deletions app/repository/mitigations-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const BaseRepository = require('./_base.repository');
const Mitigation = require('../models/mitigation-model');

class MitigationsRepository extends BaseRepository { }

module.exports = new MitigationsRepository(Mitigation);
Loading