Skip to content

Commit

Permalink
fix: circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
wwills2 committed Dec 18, 2024
1 parent 3df82e2 commit 8497173
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { getDataModelVersion } from '../utils/helpers';

import { ModelKeys, Audit, Organization, Staging } from '../models';
import { getOwnedStores, getSubscriptions } from '../datalayer/persistance.js';
import { scrubOrganizationData } from '../utils/model-utils.js';

export const findAll = async (req, res) => {
return res.json(await Organization.getOrgsMap());
Expand Down Expand Up @@ -222,7 +221,7 @@ export const deleteOrganization = async (req, res) => {
);
}

await scrubOrganizationData(orgUid);
await Organization.scrubOrganizationData(orgUid);

if (organization.isHome) {
return res.json({
Expand Down
30 changes: 29 additions & 1 deletion src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { sequelize } from '../../database';

import datalayer from '../../datalayer';
import { logger } from '../../config/logger.js';
import { FileStore, Staging } from '../';
import { Audit, FileStore, ModelKeys, Staging } from '../';
import { getDataModelVersion } from '../../utils/helpers';
import { getConfig } from '../../utils/config-loader';
const { USE_SIMULATOR, AUTO_SUBSCRIBE_FILESTORE } = getConfig().APP;
Expand Down Expand Up @@ -698,6 +698,34 @@ class Organization extends Model {
}
}

/**
* removes all records of an organization from all models with an `orgUid` column
* @param orgUid
*/
static async scrubOrganizationData(orgUid) {
const transaction = await sequelize.transaction();
try {
for (const model of ModelKeys) {
await model.destroy({ where: { orgUid }, transaction });
}

await Staging.truncate();
await Organization.destroy({ where: { orgUid }, transaction });
await FileStore.destroy({ where: { orgUid }, transaction });
await Audit.destroy({ where: { orgUid }, transaction });

await transaction.commit();
} catch (error) {
logger.error(
`failed to delete all db records for organization ${orgUid}, rolling back changes. Error: ${error.message}`,
);
await transaction.rollback();
throw new Error(
`an error occurred while deleting records corresponding to organization ${orgUid}. no changes have been made`,
);
}
}

/**
* Synchronizes metadata for all subscribed organizations.
*/
Expand Down
40 changes: 0 additions & 40 deletions src/utils/model-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import { columnsToInclude } from './helpers.js';
import Sequelize from 'sequelize';

import { Mutex } from 'async-mutex';
import {
Audit,
FileStore,
ModelKeys,
Organization,
Staging,
} from '../models/index.js';
import { sequelize } from '../database/index.js';
import { logger } from '../config/logger.js';

export async function waitForSyncRegistriesTransaction() {
if (processingSyncRegistriesTransactionMutex.isLocked()) {
Expand Down Expand Up @@ -42,37 +33,6 @@ export function formatModelAssociationName(model) {
return `${model.model.name}${model.pluralize ? 's' : ''}`;
}

/**
* removes all records of an organization from all models with an `orgUid` column
* @param orgUid
*/
export async function scrubOrganizationData(orgUid) {
logger.info(
`deleting all database entries corresponding to organization ${orgUid}`,
);
const transaction = await sequelize.transaction();
try {
for (const model of ModelKeys) {
await model.destroy({ where: { orgUid }, transaction });
}

await Staging.truncate();
await Organization.destroy({ where: { orgUid }, transaction });
await FileStore.destroy({ where: { orgUid }, transaction });
await Audit.destroy({ where: { orgUid }, transaction });

await transaction.commit();
} catch (error) {
logger.error(
`failed to delete all db records for organization ${orgUid}, rolling back changes. Error: ${error.message}`,
);
await transaction.rollback();
throw new Error(
`an error occurred while deleting records corresponding to organization ${orgUid}. no changes have been made`,
);
}
}

/**
* Finds the deleted sub-items (e.g. labels)
* @param updatedItems {Array<Object>} - The projects updated by the user
Expand Down

0 comments on commit 8497173

Please sign in to comment.