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 all 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 @@
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.');

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L31-L32

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

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

Check warning on line 63 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L53-L63

Added lines #L53 - L63 were not covered by tests
}
});
}

Check warning on line 65 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L65

Added line #L65 was not covered by tests
};

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

Check warning on line 76 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L75-L76

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

Check warning on line 89 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L83-L89

Added lines #L83 - L89 were not covered by tests
}
});
}

Check warning on line 91 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L91

Added line #L91 was not covered by tests
};

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

Check warning on line 130 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L130

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

Check warning on line 138 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L136-L138

Added lines #L136 - L138 were not covered by tests
};

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

Check warning on line 148 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L148

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

Check warning on line 157 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L155-L157

Added lines #L155 - L157 were not covered by tests

};

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

Check warning on line 176 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L175-L176

Added lines #L175 - L176 were not covered by tests
}
};

exports.retrieveTechniquesForTactic = async function(req, res) {
Expand All @@ -187,7 +195,7 @@
}
}
catch(err) {
if (err.message === tacticsService.errors.badlyFormattedParameter) {
if (err instanceof BadlyFormattedParameterError) {

Check warning on line 198 in app/controllers/tactics-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/tactics-controller.js#L198

Added line #L198 was not covered by tests
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