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

Add late binding to the base service. #341

Merged
merged 1 commit into from
Jan 23, 2024
Merged
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
33 changes: 26 additions & 7 deletions app/services/_base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const uuid = require('uuid');
const systemConfigurationService = require('./system-configuration-service');
const identitiesService = require('./identities-service');
const attackObjectsService = require('./attack-objects-service');
const config = require('../config/config');
const { DatabaseError,
IdentityServiceError,
Expand All @@ -20,6 +18,20 @@ class BaseService extends AbstractService {
this.repository = repository;
}

static attackObjectsService;

static identitiesService;

static requireServices() {
// Late binding to avoid circular dependencies
if (!BaseService.identitiesService) {
BaseService.identitiesService = require('./identities-service');
}
if (!BaseService.attackObjectsService) {
BaseService.attackObjectsService = require('./attack-objects-service');
}
}

// Helper function to determine if the last argument is a callback
static isCallback(arg) {
return typeof arg === 'function';
Expand All @@ -45,6 +57,7 @@ class BaseService extends AbstractService {
}

async retrieveAll(options, callback) {
BaseService.requireServices();
if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
Expand All @@ -61,7 +74,7 @@ class BaseService extends AbstractService {
}

try {
await identitiesService.addCreatedByAndModifiedByIdentitiesToAll(results[0].documents);
await BaseService.identitiesService.addCreatedByAndModifiedByIdentitiesToAll(results[0].documents);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
Expand All @@ -83,6 +96,8 @@ class BaseService extends AbstractService {
}

async retrieveById(stixId, options, callback) {
BaseService.requireServices();

if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
Expand All @@ -100,7 +115,7 @@ class BaseService extends AbstractService {
const documents = await this.repository.retrieveAllById(stixId);

try {
await identitiesService.addCreatedByAndModifiedByIdentitiesToAll(documents);
await BaseService.identitiesService.addCreatedByAndModifiedByIdentitiesToAll(documents);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
Expand All @@ -121,7 +136,7 @@ class BaseService extends AbstractService {

if (document) {
try {
await identitiesService.addCreatedByAndModifiedByIdentities(document);
await BaseService.identitiesService.addCreatedByAndModifiedByIdentities(document);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
Expand Down Expand Up @@ -159,6 +174,8 @@ class BaseService extends AbstractService {
}

async retrieveVersionById(stixId, modified, callback) {
BaseService.requireServices();

if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
Expand Down Expand Up @@ -190,7 +207,7 @@ class BaseService extends AbstractService {
return null;
} else {
try {
await identitiesService.addCreatedByAndModifiedByIdentities(document);
await BaseService.identitiesService.addCreatedByAndModifiedByIdentities(document);
} catch (err) {
const identityError = new IdentityServiceError({
details: err.message,
Expand All @@ -215,6 +232,8 @@ class BaseService extends AbstractService {
}

async create(data, options, callback) {
BaseService.requireServices();

if (BaseService.isCallback(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
Expand Down Expand Up @@ -242,7 +261,7 @@ class BaseService extends AbstractService {
}

// Set the default marking definitions
await attackObjectsService.setDefaultMarkingDefinitions(data);
await BaseService.attackObjectsService.setDefaultMarkingDefinitions(data);

// Get the organization identity
const organizationIdentityRef = await systemConfigurationService.retrieveOrganizationIdentityRef();
Expand Down
Loading