From 8497173a5cfdaa1d9bdc771d4cf966f2147b08a4 Mon Sep 17 00:00:00 2001 From: William Wills Date: Wed, 18 Dec 2024 17:19:41 -0500 Subject: [PATCH] fix: circular imports --- src/controllers/organization.controller.js | 3 +- .../organizations/organizations.model.js | 30 +++++++++++++- src/utils/model-utils.js | 40 ------------------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index 83ff8de2..db6fb8c0 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -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()); @@ -222,7 +221,7 @@ export const deleteOrganization = async (req, res) => { ); } - await scrubOrganizationData(orgUid); + await Organization.scrubOrganizationData(orgUid); if (organization.isHome) { return res.json({ diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index f0f5303d..7dc45584 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -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; @@ -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. */ diff --git a/src/utils/model-utils.js b/src/utils/model-utils.js index c02a6ab6..0d77887a 100644 --- a/src/utils/model-utils.js +++ b/src/utils/model-utils.js @@ -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()) { @@ -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} - The projects updated by the user