From b33f6d99bfe6165052246b6d932712b7fe64ccf9 Mon Sep 17 00:00:00 2001 From: Frantz Arty Date: Wed, 23 Mar 2022 20:24:56 -0400 Subject: [PATCH] feat: add delete imported org endpoint --- src/controllers/organization.controller.js | 51 +++++++++++++++++++++- src/routes/v1/resources/organization.js | 8 ++++ src/utils/data-assertions.js | 7 +++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index 96d2a4b1..37ea7c53 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -1,5 +1,5 @@ import _ from 'lodash'; - +import { sequelize } from '../database'; import { Organization } from '../models/organizations'; import { @@ -8,6 +8,7 @@ import { assertWalletIsAvailable, assertDataLayerAvailable, assertIfReadOnlyMode, + assertCanDeleteOrg, } from '../utils/data-assertions'; import { ModelKeys, Audit } from '../models'; @@ -132,7 +133,47 @@ export const subscribeToOrganization = async (req, res) => { } }; +export const deleteImportedOrg = async (req, res) => { + let transaction; + try { + await assertIfReadOnlyMode(); + await assertDataLayerAvailable(); + await assertWalletIsAvailable(); + await assertWalletIsSynced(); + await assertHomeOrgExists(); + await assertCanDeleteOrg(req.body.orgUid); + + transaction = await sequelize.transaction(); + + await Organization.destroy({ where: { orgUid: req.body.orgUid } }); + + await Promise.all([ + ...Object.keys(ModelKeys).map( + async (key) => + await ModelKeys[key].destroy({ where: { orgUid: req.body.orgUid } }), + ), + Audit.destroy({ where: { orgUid: req.body.orgUid } }), + ]); + + await transaction.commit(); + + return res.json({ + message: + 'UnSubscribed to organization, you will no longer receive updates.', + }); + } catch (error) { + res.status(400).json({ + message: 'Error unsubscribing to organization', + error: error.message, + }); + if (transaction) { + await transaction.rollback(); + } + } +}; + export const unsubscribeToOrganization = async (req, res) => { + let transaction; try { await assertIfReadOnlyMode(); await assertDataLayerAvailable(); @@ -140,6 +181,8 @@ export const unsubscribeToOrganization = async (req, res) => { await assertWalletIsSynced(); await assertHomeOrgExists(); + transaction = await sequelize.transaction(); + await Organization.update( { subscribed: false, registryHash: '0' }, { where: { orgUid: req.body.orgUid } }, @@ -153,6 +196,8 @@ export const unsubscribeToOrganization = async (req, res) => { Audit.destroy({ where: { orgUid: req.body.orgUid } }), ]); + await transaction.commit(); + return res.json({ message: 'UnSubscribed to organization, you will no longer receive updates.', @@ -162,5 +207,9 @@ export const unsubscribeToOrganization = async (req, res) => { message: 'Error unsubscribing to organization', error: error.message, }); + + if (transaction) { + await transaction.rollback(); + } } }; diff --git a/src/routes/v1/resources/organization.js b/src/routes/v1/resources/organization.js index ce77feca..e769f070 100644 --- a/src/routes/v1/resources/organization.js +++ b/src/routes/v1/resources/organization.js @@ -42,6 +42,14 @@ OrganizationRouter.put( }, ); ++OrganizationRouter.delete( + '/import', + validator.body(importOrganizationSchema), + (req, res) => { + return OrganizationController.deleteImportedOrg(req, res); + }, +); + OrganizationRouter.put( '/subscribe', validator.body(subscribeOrganizationSchema), diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index af0f7e20..3d19ab5d 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -71,6 +71,13 @@ export const assertRecordExistance = async (Model, pk) => { return record; }; +export const assertCanDeleteOrg = async (orgUid) => { + const homeOrg = await Organization.getHomeOrg(); + if (homeOrg.orgUid === orgUid) { + throw new Error(`Cant delete your own organization`); + } +}; + export const assertHomeOrgExists = async () => { const homeOrg = await Organization.getHomeOrg(); if (!homeOrg) {