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

Refactoring collections service #345

Open
wants to merge 37 commits into
base: project-orion
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
10aa778
refactoring collections service
vsun757 Nov 3, 2023
34193be
added repo for collections service
vsun757 Nov 3, 2023
c8590fa
refactored tests
vsun757 Nov 3, 2023
f39e9ae
refactored controller
vsun757 Nov 3, 2023
6704c8e
changed last function to async
vsun757 Nov 3, 2023
9371edc
Merge branch 'project-orion' into collections-test-303
vsun757 Dec 8, 2023
3bd95ed
refactor
vsun757 Dec 8, 2023
0a5d213
Merge branch 'project-orion' into collections-test-303
vsun757 Dec 18, 2023
0fdd96c
adding workflow to collections spec initialization
vsun757 Dec 19, 2023
5547a10
fixed some issues
vsun757 Dec 19, 2023
558719b
adding debugging
vsun757 Jan 9, 2024
98bbb17
resolved one issue
vsun757 Jan 10, 2024
62b822f
one more error down
vsun757 Jan 10, 2024
49326c2
more accurate error logging
vsun757 Jan 10, 2024
7b64c97
cleaning up error handling
vsun757 Jan 12, 2024
28ad3fb
changing some error throwing
vsun757 Jan 30, 2024
5760d60
resolving some errors
vsun757 Jan 31, 2024
5b491f1
adding back retrieve by id
vsun757 Jan 31, 2024
5495a01
adding back retrieve by id async
vsun757 Jan 31, 2024
8189df3
trying more
vsun757 Jan 31, 2024
82012b5
another cosmetic change
vsun757 Jan 31, 2024
989ab47
changing another error
vsun757 Jan 31, 2024
5ab385d
adding callback support to retrievebyid
vsun757 Feb 1, 2024
be6ee91
alltests but one pass
vsun757 Feb 2, 2024
0dbd209
fixig retreive by id
vsun757 Feb 2, 2024
ea7a3a4
all collections tests pass
vsun757 Feb 2, 2024
ef56147
resolving final issues
vsun757 Feb 2, 2024
fa3eb81
fixed some issues
vsun757 Feb 7, 2024
d32e33d
added callback function check
vsun757 Feb 19, 2024
4a2b397
added workflow to collection bundles object
vsun757 Feb 19, 2024
628e6dd
fixing a typo in collection bundles service
vsun757 Feb 19, 2024
0ae6dc4
all tests pass
vsun757 Feb 19, 2024
d9494a5
all tests pass
vsun757 Feb 19, 2024
9397ce1
starting to resolve linter issues
vsun757 Feb 19, 2024
3b7b2e9
more lint fixes
vsun757 Feb 19, 2024
4ffe198
one more linter issue down
vsun757 Feb 19, 2024
d90d733
linter done
vsun757 Feb 19, 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
111 changes: 54 additions & 57 deletions app/controllers/collections-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const collectionsService = require('../services/collections-service');
const logger = require('../lib/logger');
const { BadlyFormattedParameterError, InvalidQueryStringParameterError, DuplicateIdError } = 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 @@ -14,77 +15,73 @@
search: req.query.search,
lastUpdatedBy: req.query.lastUpdatedBy,
}
try {
const collections = await collectionsService.retrieveAll(options);
logger.debug(`Success: Retrieved ${ collections.length } collection(s)`);
return res.status(200).send(collections);
}
catch (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get collections. Server error.');
}

Check warning on line 26 in app/controllers/collections-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/collections-controller.js#L24-L26

Added lines #L24 - L26 were not covered by tests

collectionsService.retrieveAll(options, function(err, collections) {
if (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get collections. Server error.');
}
else {
logger.debug(`Success: Retrieved ${ collections.length } collection(s)`);
return res.status(200).send(collections);
}
});
};

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

collectionsService.retrieveById(req.params.stixId, options, function (err, collections) {
if (err) {
if (err.message === collectionsService.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 === collectionsService.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 collections. Server error.');
}
try {
const collections = await collectionsService.retrieveById(req.params.stixId, options);
if (collections.length === 0) {
return res.status(404).send('Collection not found.');
}
else {
if (collections.length === 0) {
return res.status(404).send('Collection not found.');
}
else {
logger.debug(`Success: Retrieved ${ collections.length } collection(s) with id ${ req.params.stixId }`);
return res.status(200).send(collections);
}
logger.debug(`Success: Retrieved ${ collections.length } collection(s) with id ${ req.params.stixId }`);
return res.status(200).send(collections);
}
}
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 53 in app/controllers/collections-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/collections-controller.js#L47-L53

Added lines #L47 - L53 were not covered by tests
}
});
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get collections. Server error.');
}
}

Check warning on line 59 in app/controllers/collections-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/collections-controller.js#L55-L59

Added lines #L55 - L59 were not covered by tests
};

exports.retrieveVersionById = function(req, res) {
exports.retrieveVersionById = async function(req, res) {
const options = {
retrieveContents: req.query.retrieveContents
}
collectionsService.retrieveVersionById(req.params.stixId, req.params.modified, options, function(err, collection) {
if (err) {
if (err.message === collectionsService.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 collections. Server error.');
}
}
try {
const collection = await collectionsService.retrieveVersionById(req.params.stixId, req.params.modified, options);
if (!collection) {
return res.status(404).send('Collection not found.');
}

Check warning on line 70 in app/controllers/collections-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/collections-controller.js#L69-L70

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

Check warning on line 79 in app/controllers/collections-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/collections-controller.js#L76-L79

Added lines #L76 - L79 were not covered by tests
else {
if (!collection) {
return res.status(404).send('Collection not found.');
}
else {
logger.debug(`Success: Retrieved collection with id ${collection.id}`);
return res.status(200).send(collection);
}
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get collections. Server error.');

Check warning on line 82 in app/controllers/collections-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/collections-controller.js#L81-L82

Added lines #L81 - L82 were not covered by tests
}
})
}
}

exports.create = async function(req, res) {
Expand All @@ -106,7 +103,7 @@
return res.status(201).send(savedCollection);
}
catch(err) {
if (err.message === collectionsService.errors.duplicateId) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create collection. Duplicate stix.id and stix.modified properties.');
}
Expand Down Expand Up @@ -143,7 +140,7 @@
return res.status(204).end();
}
} catch (error) {
logger.error('Delete collection failed. ' + error);
return res.status(500).send('Unable to delete collection. Server error.');
logger.error('Delete collection failed. ' + error);
return res.status(500).send('Unable to delete collection. Server error.');

Check warning on line 144 in app/controllers/collections-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/collections-controller.js#L143-L144

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

const BaseRepository = require('./_base.repository');
const Collection = require('../models/collection-model');

class CollectionsRepository extends BaseRepository {
}

module.exports = new CollectionsRepository(Collection);
3 changes: 3 additions & 0 deletions app/services/_base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ class BaseService extends AbstractService {

// Record the user account that created the object
if (options.userAccountId) {

data.workspace.workflow = data.workspace.workflow ?? {}

data.workspace.workflow.created_by_user_account = options.userAccountId;
}

Expand Down
10 changes: 5 additions & 5 deletions app/services/collection-bundles-service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const uuid = require('uuid');
const util = require('util');
const semver = require('semver');

const collectionsService = require('../services/collections-service');
Expand Down Expand Up @@ -175,6 +174,9 @@ exports.importBundle = function(collection, data, options, callback) {
additions: [],
changes: [],
duplicates: []
},
workflow : {

}
},
stix: collection
Expand Down Expand Up @@ -669,8 +671,7 @@ exports.exportBundle = async function(options) {
if (options.collectionModified) {
// Retrieve the collection with the provided id and modified date
const retrievalOptions = { retrieveContents: true };
const retrieveCollection = util.promisify(collectionsService.retrieveVersionById);
const collection = await retrieveCollection(options.collectionId, options.collectionModified, retrievalOptions);
const collection = await collectionsService.retrieveVersionById(options.collectionId, options.collectionModified, retrievalOptions);
if (collection) {
const bundle = await createBundle(collection, options);
return bundle;
Expand All @@ -686,8 +687,7 @@ exports.exportBundle = async function(options) {
versions: 'latest',
retrieveContents: true
};
const retrieveCollection = util.promisify(collectionsService.retrieveById);
const collections = await retrieveCollection(options.collectionId, retrievalOptions);
const collections = await collectionsService.retrieveById(options.collectionId, retrievalOptions);
if (collections.length === 1) {
const exportedCollection = collections[0];
const bundle = await createBundle(exportedCollection, options);
Expand Down
Loading
Loading