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 tactics service #310

Merged
merged 21 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
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
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
14 changes: 14 additions & 0 deletions app/repository/groups-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const BaseRepository = require('./_base.repository');
const Group = require('../models/group-model');

class GroupsRepository extends BaseRepository {

constructor() {
super(Group);

}
}

module.exports = new GroupsRepository();
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