Skip to content

Commit

Permalink
feat: add organization resync API
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2sw committed May 6, 2022
1 parent 319e0de commit 825e08c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
};
13 changes: 11 additions & 2 deletions src/routes/v1/resources/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -66,4 +67,12 @@ OrganizationRouter.put(
},
);

OrganizationRouter.put(
'/resync',
validator.body(resyncOrganizationSchema),
(req, res) => {
return OrganizationController.resyncOrganization(req, res);
},
);

export { OrganizationRouter };
4 changes: 4 additions & 0 deletions src/validations/organizations.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
15 changes: 15 additions & 0 deletions tests/resources/organization.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 825e08c

Please sign in to comment.