From 4218ba9fcf56b4fdc7132f56ab0172a36e94fd00 Mon Sep 17 00:00:00 2001 From: Frantz Arty Date: Fri, 18 Mar 2022 14:44:17 -0400 Subject: [PATCH] feat: add org subscribe unsubscribe endpoints --- src/controllers/organization.controller.js | 35 +++++++++++++++++++ .../organizations/organizations.model.js | 2 +- src/routes/v1/resources/organization.js | 31 +++++++++++++++- src/tasks/sync-audit-table.js | 5 ++- src/validations/organizations.validations.js | 8 +++++ 5 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index 15686518..96d2a4b1 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -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()); }; @@ -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, + }); + } +}; diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index 6acd09f9..7aba7999 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -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', diff --git a/src/routes/v1/resources/organization.js b/src/routes/v1/resources/organization.js index f51e6c16..ce77feca 100644 --- a/src/routes/v1/resources/organization.js +++ b/src/routes/v1/resources/organization.js @@ -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(); @@ -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 }; diff --git a/src/tasks/sync-audit-table.js b/src/tasks/sync-audit-table.js index e2b22685..c132c7f9 100644 --- a/src/tasks/sync-audit-table.js +++ b/src/tasks/sync-audit-table.js @@ -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)), ); diff --git a/src/validations/organizations.validations.js b/src/validations/organizations.validations.js index d1a1a236..bf60fd5b 100644 --- a/src/validations/organizations.validations.js +++ b/src/validations/organizations.validations.js @@ -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(), +});