Skip to content

Commit

Permalink
feat: add org subscribe unsubscribe endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Mar 18, 2022
1 parent 35c88ef commit 4218ba9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
assertIfReadOnlyMode,
} from '../utils/data-assertions';

import { ModelKeys, Audit } from '../models';

export const findAll = async (req, res) => {
return res.json(await Organization.getOrgsMap());
};
Expand Down Expand Up @@ -129,3 +131,36 @@ export const subscribeToOrganization = async (req, res) => {
});
}
};

export const unsubscribeToOrganization = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertHomeOrgExists();

await Organization.update(
{ subscribed: false, registryHash: '0' },
{ 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 } }),
]);

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,
});
}
};
2 changes: 1 addition & 1 deletion src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class Organization extends Model {
static subscribeToOrganization = async (orgUid) => {
const exists = await Organization.findOne({ where: { orgUid } });
if (exists) {
await Organization.update({ subscribed: true }, { orgUid });
await Organization.update({ subscribed: true }, { where: { orgUid } });
} else {
throw new Error(
'Can not subscribe, please import this organization first',
Expand Down
31 changes: 30 additions & 1 deletion src/routes/v1/resources/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import express from 'express';
import joiExpress from 'express-joi-validation';

import { OrganizationController } from '../../../controllers';
import { newOrganizationSchema } from '../../../validations';
import {
newOrganizationSchema,
unsubscribeOrganizationSchema,
subscribeOrganizationSchema,
importOrganizationSchema,
} from '../../../validations';

const validator = joiExpress.createValidator({ passError: true });
const OrganizationRouter = express.Router();
Expand All @@ -29,4 +34,28 @@ OrganizationRouter.put('/', (req, res) => {
return OrganizationController.importOrg(req, res);
});

OrganizationRouter.put(
'/import',
validator.body(importOrganizationSchema),
(req, res) => {
return OrganizationController.importOrg(req, res);
},
);

OrganizationRouter.put(
'/subscribe',
validator.body(subscribeOrganizationSchema),
(req, res) => {
return OrganizationController.subscribeToOrganization(req, res);
},
);

OrganizationRouter.put(
'/unsubscribe',
validator.body(unsubscribeOrganizationSchema),
(req, res) => {
return OrganizationController.unsubscribeToOrganization(req, res);
},
);

export { OrganizationRouter };
5 changes: 4 additions & 1 deletion src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ dotenv.config();
const task = new Task('sync-audit', async () => {
log('Syncing Audit Information');
if (process.env.USE_SIMULATOR === 'false') {
const organizations = await Organization.findAll({ raw: true });
const organizations = await Organization.findAll({
where: { subscribed: true },
raw: true,
});
await Promise.all(
organizations.map((organization) => syncOrganizationAudit(organization)),
);
Expand Down
8 changes: 8 additions & 0 deletions src/validations/organizations.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ export const newOrganizationSchema = Joi.object({
export const importOrganizationSchema = Joi.object({
orgUid: Joi.string().required(),
});

export const unsubscribeOrganizationSchema = Joi.object({
orgUid: Joi.string().required(),
});

export const subscribeOrganizationSchema = Joi.object({
orgUid: Joi.string().required(),
});

0 comments on commit 4218ba9

Please sign in to comment.