Skip to content

Commit

Permalink
Merge pull request #310 from center-for-threat-informed-defense/290-r…
Browse files Browse the repository at this point in the history
…efactor-tactics-service

Refactor tactics service
  • Loading branch information
ElJocko authored Nov 8, 2023
2 parents 059a5e0 + 9769f0b commit 8c6c583
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 923 deletions.
194 changes: 101 additions & 93 deletions app/controllers/tactics-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const tacticsService = require('../services/tactics-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 @@ -15,96 +16,99 @@ exports.retrieveAll = function(req, res) {
lastUpdatedBy: req.query.lastUpdatedBy,
includePagination: req.query.includePagination
}
try {
const results = await tacticsService.retrieveAll(options)

tacticsService.retrieveAll(options, function(err, results) {
if (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get tactics. Server error.');
}
else {
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total tactic(s)`);
}
else {
logger.debug(`Success: Retrieved ${ results.length } tactic(s)`);
}
return res.status(200).send(results);

} catch (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get tactics. Server error.');
}
});
};

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

tacticsService.retrieveById(req.params.stixId, options, function (err, tactics) {
if (err) {
if (err.message === tacticsService.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 === tacticsService.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 tactics. Server error.');
}
}
else {
try {
const tactics = await tacticsService.retrieveById(req.params.stixId, options);

if (tactics.length === 0) {
return res.status(404).send('Tactic not found.');
}
else {
logger.debug(`Success: Retrieved ${ tactics.length } tactic(s) with id ${ req.params.stixId }`);
return res.status(200).send(tactics);
}

} 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 tactics. Server error.');
}
});
}
};

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

try {

const tactic = await tacticsService.retrieveVersionById(req.params.stixId, req.params.modified);

if (!tactic) {
return res.status(404).send('tactic not found.');
}
else {
logger.debug(`Success: Retrieved tactic with id ${tactic.id}`);
return res.status(200).send(tactic);
}

} 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 tactic. Server error.');
}
});
}
};

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

const options = {
import: false,
userAccountId: req.user?.userAccountId
};

// Create the tactic
try {
const options = {
import: false,
userAccountId: req.user?.userAccountId
};
const tactic = await tacticsService.create(tacticData, options);

logger.debug("Success: Created tactic with id " + tactic.stix.id);
return res.status(201).send(tactic);
}
catch(err) {
if (err.message === tacticsService.errors.duplicateId) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create tactic. Duplicate stix.id and stix.modified properties.');
}
Expand All @@ -115,58 +119,62 @@ exports.create = async function(req, res) {
}
};

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

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

if (!tactic) {
return res.status(404).send('tactic not found.');
} else {
logger.debug("Success: Updated tactic with id " + tactic.stix.id);
return res.status(200).send(tactic);
}
});
} catch (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update tactic. Server error.");
}
};

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

try {

const tactic = await tacticsService.deleteVersionById(req.params.stixId, req.params.modified);

if (!tactic) {
return res.status(404).send('tactic not found.');
} else {
logger.debug("Success: Deleted tactic with id " + tactic.stix.id);
return res.status(204).end();
}
});

} catch (err) {
logger.error('Delete tactic failed. ' + err);
return res.status(500).send('Unable to delete tactic. Server error.');
}

};

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

try {

const tactics = await tacticsService.deleteById(req.params.stixId);

if (tactics.deletedCount === 0) {
return res.status(404).send('Tactic not found.');
} else {
if (tactics.deletedCount === 0) {
return res.status(404).send('Tactic not found.');
} else {
logger.debug(`Success: Deleted tactic with id ${req.params.stixId}`);
return res.status(204).end();
}
logger.debug(`Success: Deleted tactic with id ${req.params.stixId}`);
return res.status(204).end();
}
});

} catch (err) {
logger.error('Delete tactic failed. ' + err);
return res.status(500).send('Unable to delete tactic. Server error.');
}
};

exports.retrieveTechniquesForTactic = async function(req, res) {
Expand All @@ -187,7 +195,7 @@ exports.retrieveTechniquesForTactic = async function(req, res) {
}
}
catch(err) {
if (err.message === tacticsService.errors.badlyFormattedParameter) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
} else {
Expand Down
2 changes: 1 addition & 1 deletion app/repository/groups-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const Group = require('../models/group-model');

class GroupsRepository extends BaseRepository { }

module.exports = new GroupsRepository(Group);
module.exports = new GroupsRepository(Group);
13 changes: 13 additions & 0 deletions app/repository/tactics-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const BaseRepository = require('./_base.repository');
const Tactic = require('../models/tactic-model');

class TacticsRepository extends BaseRepository {

constructor() {
super(Tactic);
}
}

module.exports = new TacticsRepository();
Loading

0 comments on commit 8c6c583

Please sign in to comment.