Skip to content

Commit

Permalink
feat: add delete imported org endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Mar 24, 2022
1 parent b613396 commit b33f6d9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';

import { sequelize } from '../database';
import { Organization } from '../models/organizations';

import {
Expand All @@ -8,6 +8,7 @@ import {
assertWalletIsAvailable,
assertDataLayerAvailable,
assertIfReadOnlyMode,
assertCanDeleteOrg,
} from '../utils/data-assertions';

import { ModelKeys, Audit } from '../models';
Expand Down Expand Up @@ -132,14 +133,56 @@ 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();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertHomeOrgExists();

transaction = await sequelize.transaction();

await Organization.update(
{ subscribed: false, registryHash: '0' },
{ where: { orgUid: req.body.orgUid } },
Expand All @@ -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.',
Expand All @@ -162,5 +207,9 @@ export const unsubscribeToOrganization = async (req, res) => {
message: 'Error unsubscribing to organization',
error: error.message,
});

if (transaction) {
await transaction.rollback();
}
}
};
8 changes: 8 additions & 0 deletions src/routes/v1/resources/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b33f6d9

Please sign in to comment.