Skip to content

Commit

Permalink
Add additional unimplemented functions.
Browse files Browse the repository at this point in the history
Remove redundant code.
Modify attack objects repository so that the base service can be used for the retrieveAll() function.
Avoid using the AttackObject model in the service.
  • Loading branch information
Jack Sheriff committed Feb 28, 2024
1 parent 836cbee commit 914a510
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 90 deletions.
75 changes: 7 additions & 68 deletions app/repository/attack-objects-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const BaseRepository = require('./_base.repository');
const AttackObject = require('../models/attack-object-model');
const util = require('util');
const { BadlyFormattedParameterError, MissingParameterError } = require('../exceptions');
const { lastUpdatedByQueryHelper } = require('../lib/request-parameter-helper');

const regexValidator = require('../lib/regex');
Expand All @@ -19,8 +17,6 @@ class AttackObjectsRepository extends BaseRepository {
duplicateCollection: 'Duplicate collection'
};

relationshipPrefix = 'relationship';

identitiesService;

async retrieveAll(options) {
Expand Down Expand Up @@ -84,7 +80,7 @@ class AttackObjectsRepository extends BaseRepository {
}

// Retrieve the documents
let documents = await AttackObject.aggregate(aggregation);
let documents = await this.model.aggregate(aggregation);

// Add relationships from separate collection
if (!options.attackId && !options.search) {
Expand Down Expand Up @@ -113,69 +109,12 @@ class AttackObjectsRepository extends BaseRepository {
paginatedDocuments = documents.slice(offset);
}

// Add identities
if (!this.identitiesService) {
this.identitiesService = require('../services/identities-service');
}
await this.identitiesService.addCreatedByAndModifiedByIdentitiesToAll(paginatedDocuments);

// Prepare the return value
if (options.includePagination) {
const returnValue = {
pagination: {
total: documents.length,
offset: options.offset,
limit: options.limit
},
data: paginatedDocuments
};
return returnValue;
}
else {
return paginatedDocuments;
}
// Mimic the format that the base repository uses to return documents
return [{
documents: paginatedDocuments,
totalCount: [ { totalCount: documents.length } ]
}];
}

async retrieveVersionById(stixId, modified) {
// Retrieve the version of the attack object with the matching stixId and modified date

// require here to avoid circular dependency
const relationshipsService = require('../services/relationships-service');
const retrieveRelationshipsVersionById = util.promisify(relationshipsService.retrieveVersionById);

if (!stixId) {
throw new MissingParameterError('stixId');
}

if (!modified) {
throw new MissingParameterError('modified');
}

let attackObject;
if (stixId.startsWith(this.relationshipPrefix)) {
attackObject = await retrieveRelationshipsVersionById(stixId, modified);
}
else {
try {
attackObject = await AttackObject.findOne({ 'stix.id': stixId, 'stix.modified': modified });
}
catch(err) {
if (err.name === 'CastError') {
throw new BadlyFormattedParameterError('stixId');
} else {
throw err;
}
}
}

// Note: attackObject is null if not found
if (!this.identitiesService) {
this.identitiesService = require('../services/identities-service');
}
await this.identitiesService.addCreatedByAndModifiedByIdentities(attackObject);
return attackObject;
}

}
}

module.exports = new AttackObjectsRepository(AttackObject);
36 changes: 14 additions & 22 deletions app/services/attack-objects-service.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
'use strict';

const { NotImplementedError} = require('../exceptions');
const AttackObject = require('../models/attack-object-model');
const { NotImplementedError } = require('../exceptions');
const Relationship = require('../models/relationship-model');
const AttackObjectsRepository = require('../repository/attack-objects-repository');
const attackObjectsRepository = require('../repository/attack-objects-repository');
const BaseService = require('./_base.service');

class AttackObjectsService extends BaseService {

systemConfigurationService = require('./system-configuration-service');

errors = {
missingParameter: 'Missing required parameter',
badlyFormattedParameter: 'Badly formatted parameter',
duplicateId: 'Duplicate id',
notFound: 'Document not found',
invalidQueryStringParameter: 'Invalid query string parameter',
duplicateCollection: 'Duplicate collection'
};

relationshipPrefix = 'relationship';

identitiesService;

retrieveById(stixId, options, callback) {
throw new NotImplementedError(this.constructor.name, 'retrieveById');

Check warning on line 15 in app/services/attack-objects-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/attack-objects-service.js#L15

Added line #L15 was not covered by tests
}
Expand All @@ -31,24 +19,27 @@ class AttackObjectsService extends BaseService {
throw new NotImplementedError(this.constructor.name, 'create');

Check warning on line 19 in app/services/attack-objects-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/attack-objects-service.js#L19

Added line #L19 was not covered by tests
}

async retrieveAll(options) {
const res = await this.repository.retrieveAll(options);
return res;
updateFull(stixId, stixModified, data, callback) {
throw new NotImplementedError(this.constructor.name, 'updateFull');

Check warning on line 23 in app/services/attack-objects-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/attack-objects-service.js#L23

Added line #L23 was not covered by tests
}

async retrieveVersionById(stixId, modified) {
const res = await this.repository.retrieveVersionById(stixId, modified);
return res;
deleteVersionById(stixId, stixModified, callback) {
throw new NotImplementedError(this.constructor.name, 'deleteVersionById');

Check warning on line 27 in app/services/attack-objects-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/attack-objects-service.js#L27

Added line #L27 was not covered by tests
}

deleteById(stixId, callback) {
throw new NotImplementedError(this.constructor.name, 'deleteById');

Check warning on line 31 in app/services/attack-objects-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/attack-objects-service.js#L31

Added line #L31 was not covered by tests
}

// Record that this object is part of a collection
async insertCollection(stixId, modified, collectionId, collectionModified) {
let attackObject;
if (stixId.startsWith(this.relationshipPrefix)) {
// TBD: Use relationships service when that is converted to async
attackObject = await Relationship.findOne({ 'stix.id': stixId, 'stix.modified': modified });
}

Check warning on line 40 in app/services/attack-objects-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/attack-objects-service.js#L38-L40

Added lines #L38 - L40 were not covered by tests
else {
attackObject = await AttackObject.findOne({ 'stix.id': stixId, 'stix.modified': modified });
attackObject = await this.repository.retrieveOneByVersion(stixId, modified);
}

if (attackObject) {
Expand Down Expand Up @@ -91,4 +82,5 @@ class AttackObjectsService extends BaseService {
}
}
}
module.exports = new AttackObjectsService(null, AttackObjectsRepository);

module.exports = new AttackObjectsService(null, attackObjectsRepository);

0 comments on commit 914a510

Please sign in to comment.