From 1054730d1d7dfec2fffca772c753cdea9073c2ea Mon Sep 17 00:00:00 2001 From: William Wills Date: Fri, 6 Dec 2024 10:22:44 -0500 Subject: [PATCH] fix: assorted bug fixes --- src/controllers/organization.controller.js | 11 +++++------ src/datalayer/persistance.js | 5 ++++- src/datalayer/syncService.js | 6 +++++- src/tasks/sync-registries.js | 5 ++++- src/utils/data-assertions.js | 2 +- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index 040fec03..f5197e25 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -264,21 +264,20 @@ export const subscribeToOrganization = async (req, res) => { export const deleteImportedOrg = async (req, res) => { let transaction; try { + const orgUid = req.body.orgUid; await assertIfReadOnlyMode(); await assertWalletIsSynced(); - await assertHomeOrgExists(); - await assertCanDeleteOrg(req.body.orgUid); + await assertCanDeleteOrg(orgUid); transaction = await sequelize.transaction(); - await Organization.destroy({ where: { orgUid: req.body.orgUid } }); + await Organization.destroy({ where: { orgUid } }); await Promise.all([ ...Object.keys(ModelKeys).map( - async (key) => - await ModelKeys[key].destroy({ where: { orgUid: req.body.orgUid } }), + async (key) => await ModelKeys[key].destroy({ where: { orgUid } }), ), - Audit.destroy({ where: { orgUid: req.body.orgUid } }), + Audit.destroy({ where: { orgUid } }), ]); await transaction.commit(); diff --git a/src/datalayer/persistance.js b/src/datalayer/persistance.js index b4fd823a..0a803058 100644 --- a/src/datalayer/persistance.js +++ b/src/datalayer/persistance.js @@ -542,7 +542,10 @@ const subscribeToStoreOnDataLayer = async ( return { success: true }; } - const { storeIds: subscriptions } = await getSubscriptions(); + const { storeIds: subscriptions, success } = await getSubscriptions(); + if (!success) { + return false; + } if (subscriptions.includes(storeId)) { logger.info(`Already subscribed to: ${storeId}`); diff --git a/src/datalayer/syncService.js b/src/datalayer/syncService.js index 5de948f5..9488e897 100644 --- a/src/datalayer/syncService.js +++ b/src/datalayer/syncService.js @@ -27,7 +27,11 @@ const subscribeToStoreOnDataLayer = async (storeId) => { }; const getSubscribedStoreData = async (storeId) => { - const { storeIds: subscriptions } = await dataLayer.getSubscriptions(storeId); + const { storeIds: subscriptions, success } = + await dataLayer.getSubscriptions(); + if (!success) { + throw new Error('failed to retrieve subscriptions from datalayer'); + } const alreadySubscribed = subscriptions.includes(storeId); if (!alreadySubscribed) { diff --git a/src/tasks/sync-registries.js b/src/tasks/sync-registries.js index ab34cf14..5e85c053 100644 --- a/src/tasks/sync-registries.js +++ b/src/tasks/sync-registries.js @@ -102,7 +102,10 @@ const processJob = async () => { }); // verify that the latest organization root hash is up to date with the audit records. attempt correction. - if (mostRecentOrgAuditRecord?.rootHash !== organization?.registryHash) { + if ( + mostRecentOrgAuditRecord && + mostRecentOrgAuditRecord?.rootHash !== organization?.registryHash + ) { logger.warn( `latest root hash in org table for organization ${organization.name} (orgUid ${organization.orgUid}) does not match the audit records. attempting to correct`, ); diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index 69848867..e0591ba7 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -114,7 +114,7 @@ export const assertRecordExistance = async (Model, pk) => { export const assertCanDeleteOrg = async (orgUid) => { const homeOrg = await Organization.getHomeOrg(); - if (homeOrg.orgUid === orgUid) { + if (homeOrg?.orgUid === orgUid) { throw new Error(`Cant delete your own organization`); } };