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 Software service #339

Merged
merged 29 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2344cf4
updating software service and new software repository
vsun757 Sep 29, 2023
818dc64
changing tests
vsun757 Sep 29, 2023
66f84b1
refactoring software controller
vsun757 Oct 4, 2023
f6cf706
removed an unnecessary parameter
vsun757 Oct 4, 2023
52e6b98
changed one more controller piece
vsun757 Oct 4, 2023
b074393
fixed error enum for software service
vsun757 Oct 4, 2023
143375b
removing assignments where unneeded
vsun757 Oct 5, 2023
abb30ab
fixed results in software controller
vsun757 Oct 6, 2023
f341c1d
passed all tests
vsun757 Oct 6, 2023
4348d21
fixing lint issues
vsun757 Oct 12, 2023
ddb3dca
making things async
vsun757 Oct 16, 2023
05d4713
merging in project orion changes
vsun757 Dec 5, 2023
fafed49
starting to change software service
vsun757 Dec 5, 2023
3833347
aligning with refactor
vsun757 Dec 6, 2023
586c677
redoing repo
vsun757 Dec 6, 2023
82cecb7
Merge branch 'project-orion' into software-test-293
vsun757 Dec 18, 2023
2b2dda6
trying to make changes
vsun757 Dec 18, 2023
c28f8d4
removing duplicate code
vsun757 Dec 19, 2023
d9e0c67
trying to get this to work with 2 stix types
vsun757 Dec 28, 2023
f79290d
debugging
vsun757 Jan 10, 2024
18ac0cf
almost there
vsun757 Jan 10, 2024
ca5bb0a
even closer
vsun757 Jan 10, 2024
45f257a
trying to re-do create function
vsun757 Jan 10, 2024
6a98141
trying final things
vsun757 Jan 10, 2024
5620d59
fixing lots of tests
vsun757 Jan 11, 2024
8b3af3e
resolving more errors
vsun757 Jan 11, 2024
a1594e1
making syntax checks
vsun757 Jan 11, 2024
225273b
Merge branch 'project-orion' into software-test-293
Jan 24, 2024
d721767
Minor cleanup and formatting.
Jan 24, 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
242 changes: 125 additions & 117 deletions app/controllers/software-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const softwareService = require('../services/software-service');
const logger = require('../lib/logger');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError, PropertyNotAllowedError } = 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,165 +17,172 @@
lastUpdatedBy: req.query.lastUpdatedBy,
includePagination: req.query.includePagination
}

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

Check warning on line 34 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L30-L34

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

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

softwareService.retrieveById(req.params.stixId, options, function (err, software) {
if (err) {
if (err.message === softwareService.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 === softwareService.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 software. Server error.');
}
try {
const software = await softwareService.retrieveById(req.params.stixId, options);
if (software.length === 0) {
return res.status(404).send('Software not found.');
}
else {
logger.debug(`Success: Retrieved ${ software.length } software with id ${ req.params.stixId }`);
return res.status(200).send(software);
}

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

Check warning on line 58 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L52-L58

Added lines #L52 - L58 were not covered by tests
}
else {
if (software.length === 0) {
return res.status(404).send('Software not found.');
}
else {
logger.debug(`Success: Retrieved ${ software.length } software with id ${ req.params.stixId }`);
return res.status(200).send(software);
}
}
});
console.log("retrieve by id error");
console.log(err);
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get software. Server error.');
}
}

Check warning on line 66 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L61-L66

Added lines #L61 - L66 were not covered by tests

};

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

if (!software) {
return res.status(404).send('Software not found.');
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L75 - L76 were not covered by tests
else {
logger.debug(`Success: Retrieved software with id ${software.id}`);
return res.status(200).send(software);
}
} 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 {
console.log("retrieve version by id error");
console.log(err);
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get software. Server error.');
}
}

Check warning on line 93 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L82-L93

Added lines #L82 - L93 were not covered by tests

};

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

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

// Create the software
try {
const options = {
import: false,
userAccountId: req.user?.userAccountId
};
const software = await softwareService.create(softwareData, options);
const software = await await softwareService.create(softwareData, options);
logger.debug("Success: Created software with id " + software.stix.id);
return res.status(201).send(software);
}
catch(err) {
if (err.message === softwareService.errors.duplicateId) {
} catch(err) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create software. Duplicate stix.id and stix.modified properties.');
}
else if (err.message === softwareService.errors.missingProperty) {
logger.warn(`Unable to create software, missing property ${ err.propertyName }`);
return res.status(400).send(`Unable to create software, missing property ${ err.propertyName }`);
}
else if (err.message === softwareService.errors.propertyNotAllowed) {
else if (err instanceof PropertyNotAllowedError) {
logger.warn(`Unable to create software, property ${ err.propertyName } is not allowed`);
return res.status(400).send(`Unable to create software, property ${ err.propertyName } is not allowed`);
}
else {
console.log("create error");
console.log(err);

Check warning on line 122 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L121-L122

Added lines #L121 - L122 were not covered by tests
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to create software. Server error.");
}
}
};

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

// Create the software
softwareService.updateFull(req.params.stixId, req.params.modified, softwareData, function(err, software) {
if (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update software. Server error.");
try {
// Create the software
const software = await softwareService.updateFull(req.params.stixId, req.params.modified, softwareData);

if (!software) {
return res.status(404).send('Software not found.');

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L138

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

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L144-L148

Added lines #L144 - L148 were not covered by tests
};

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

if (!software) {
return res.status(404).send('Software not found.');

Check warning on line 156 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L156

Added line #L156 was not covered by tests
} else {
logger.debug("Success: Deleted software with id " + software.stix.id);
return res.status(204).end();
}
else {
if (!software) {
return res.status(404).send('Software not found.');
} else {
logger.debug("Success: Deleted software with id " + software.stix.id);
return res.status(204).end();
}
}
});

} catch (err) {
console.log("delete version by id error");
console.log(err);
logger.error('Delete software failed. ' + err);
return res.status(500).send('Unable to delete software. Server error.');
}

Check warning on line 167 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L163-L167

Added lines #L163 - L167 were not covered by tests
};

exports.deleteById = function(req, res) {
softwareService.deleteById(req.params.stixId, function (err, softwares) {
if (err) {
logger.error('Delete software failed. ' + err);
return res.status(500).send('Unable to delete software. Server error.');
exports.deleteById = async function(req, res) {
try {
const softwares = await softwareService.deleteById(req.params.stixId);

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

} catch (err) {
console.log("delete by id error");
console.log(err);
logger.error('Delete software failed. ' + err);
return res.status(500).send('Unable to delete software. Server error.');
}

Check warning on line 187 in app/controllers/software-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/software-controller.js#L183-L187

Added lines #L183 - L187 were not covered by tests
};
8 changes: 8 additions & 0 deletions app/exceptions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ class NotImplementedError extends CustomError {
}
}

class PropertyNotAllowedError extends CustomError {
constructor(propertyName, options) {
super(`Unable to create software, property ${propertyName} is not allowed`, options);
}
}

class InvalidTypeError extends CustomError {
constructor(options) {
super('Invalid stix.type', options);
Expand Down Expand Up @@ -114,5 +120,7 @@ module.exports = {
IdentityServiceError,
TechniquesServiceError,
TacticsServiceError,
PropertyNotAllowedError,

InvalidTypeError,
};
8 changes: 8 additions & 0 deletions app/repository/software-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const BaseRepository = require('./_base.repository');
const Software = require('../models/software-model');

class SoftwareRepository extends BaseRepository { }

module.exports = new SoftwareRepository(Software);
Loading
Loading