From 825e08c883dada6207e1cb233465a0a2acd17003 Mon Sep 17 00:00:00 2001 From: Anthony Benites <104385099+ec2sw@users.noreply.github.com> Date: Thu, 5 May 2022 17:27:44 -0400 Subject: [PATCH] feat: add organization resync API --- src/controllers/organization.controller.js | 40 ++++++++++++++++++++ src/routes/v1/resources/organization.js | 13 ++++++- src/validations/organizations.validations.js | 4 ++ tests/resources/organization.spec.js | 15 ++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index 3ed8288d..bc521fff 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -219,3 +219,43 @@ export const unsubscribeToOrganization = async (req, res) => { } } }; + +export const resyncOrganization = async (req, res) => { + let transaction; + try { + await assertIfReadOnlyMode(); + await assertWalletIsSynced(); + await assertHomeOrgExists(); + + transaction = await sequelize.transaction(); + + await Organization.update( + { 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 } }), + ]); + + await transaction.commit(); + + return res.json({ + message: + 'Resyncing organization completed', + }); + } catch (error) { + res.status(400).json({ + message: 'Error resyncing 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 8be7bd79..f052fbf3 100644 --- a/src/routes/v1/resources/organization.js +++ b/src/routes/v1/resources/organization.js @@ -5,10 +5,11 @@ import joiExpress from 'express-joi-validation'; import { OrganizationController } from '../../../controllers'; import { + importOrganizationSchema, newOrganizationSchema, - unsubscribeOrganizationSchema, + resyncOrganizationSchema, subscribeOrganizationSchema, - importOrganizationSchema, + unsubscribeOrganizationSchema, } from '../../../validations'; const validator = joiExpress.createValidator({ passError: true }); @@ -66,4 +67,12 @@ OrganizationRouter.put( }, ); +OrganizationRouter.put( + '/resync', + validator.body(resyncOrganizationSchema), + (req, res) => { + return OrganizationController.resyncOrganization(req, res); + }, +); + export { OrganizationRouter }; diff --git a/src/validations/organizations.validations.js b/src/validations/organizations.validations.js index 83fb7e36..e560960f 100644 --- a/src/validations/organizations.validations.js +++ b/src/validations/organizations.validations.js @@ -18,3 +18,7 @@ export const unsubscribeOrganizationSchema = Joi.object({ export const subscribeOrganizationSchema = Joi.object({ orgUid: Joi.string().required(), }); + +export const resyncOrganizationSchema = Joi.object({ + orgUid: Joi.string().required(), +}); diff --git a/tests/resources/organization.spec.js b/tests/resources/organization.spec.js index eaccc018..59e30e95 100644 --- a/tests/resources/organization.spec.js +++ b/tests/resources/organization.spec.js @@ -60,4 +60,19 @@ describe('Orgainzation Resource CRUD', function () { ); }).timeout(TEST_WAIT_TIME * 10); }); + + describe('PUT - Resyncs an organization', function () { + it('resyncs organization', async function () { + // add a project to the staging table + await testFixtures.createNewProject(); + + const response = await supertest(app).put(`/v1/organizations/resync`).send({ + orgUid: '1', + }); + + expect(response.body.message).to.equal( + 'Resyncing organization completed', + ); + }).timeout(TEST_WAIT_TIME * 10); + }); });