Skip to content

Commit

Permalink
feat: sync remaining count for organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Oct 23, 2023
1 parent 4e0e5ac commit bea5410
Show file tree
Hide file tree
Showing 25 changed files with 173 additions and 75 deletions.
7 changes: 7 additions & 0 deletions src/controllers/governance.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export const isCreated = async (req, res) => {
if (results) {
return res.json({
created: true,
success: true,
});
} else {
return res.json({
created: false,
success: true,
});
}
} catch (error) {
Expand Down Expand Up @@ -116,6 +118,7 @@ export const createGoveranceBody = async (req, res) => {
return res.json({
message:
'Setting up new Governance Body on this node, this can take a few mins',
success: true,
});
} catch (error) {
res.status(400).json({
Expand All @@ -141,6 +144,7 @@ export const setDefaultOrgList = async (req, res) => {

return res.json({
message: 'Committed this new organization list to the datalayer',
success: true,
});
} catch (error) {
console.trace(error);
Expand All @@ -167,6 +171,7 @@ export const setPickList = async (req, res) => {

return res.json({
message: 'Committed this pick list to the datalayer',
success: true,
});
} catch (error) {
res.status(400).json({
Expand All @@ -191,6 +196,7 @@ export const setGlossary = async (req, res) => {

return res.json({
message: 'Committed glossary to the datalayer',
success: true,
});
} catch (error) {
res.status(400).json({
Expand All @@ -206,6 +212,7 @@ export const sync = async (req, res) => {
Governance.sync();
return res.json({
message: 'Syncing Governance Body',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/offer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const importOfferFile = async (req, res) => {

res.json({
message: 'Offer has been imported for review.',
success: true,
});
} catch (error) {
console.trace(error);
Expand Down Expand Up @@ -132,6 +133,7 @@ export const commitImportedOfferFile = async (req, res) => {
res.json({
message: 'Offer Accepted.',
tradeId: response.trade_id,
success: true,
});

await Meta.destroy({
Expand Down
21 changes: 20 additions & 1 deletion src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Sequelize } from 'sequelize';
import { sequelize } from '../database';
import { Organization } from '../models/organizations';

Expand Down Expand Up @@ -62,6 +63,7 @@ export const createV2 = async (req, res) => {
return res.json({
message: 'Your organization already exists.',
orgId: myOrganization.orgUid,
success: false,
});
} else {
const { name } = req.body;
Expand All @@ -81,6 +83,7 @@ export const createV2 = async (req, res) => {
return res.json({
message:
'New organization is currently being created. It can take up to 30 mins. Please do not interrupt this process.',
success: true,
});
}
} catch (error) {
Expand All @@ -104,13 +107,15 @@ export const create = async (req, res) => {
return res.json({
message: 'Your organization already exists.',
orgId: myOrganization.orgUid,
success: false,
});
} else {
const { name, icon } = req.body;
const dataModelVersion = getDataModelVersion();

return res.json({
message: 'New organization created successfully.',
success: true,
orgId: await Organization.createHomeOrganization(
name,
icon,
Expand All @@ -135,13 +140,18 @@ export const resetHomeOrg = async (req, res) => {
await Promise.all([
Organization.destroy({ where: { isHome: true } }),
Staging.destroy({
where: {},
where: {
id: {
[Sequelize.Op.ne]: null,
},
},
truncate: true,
}),
]);

res.json({
message: 'Your home organization was reset, please create a new one.',
success: true,
});
} catch (error) {
res.status(400).json({
Expand All @@ -162,6 +172,7 @@ export const importOrg = async (req, res) => {
res.json({
message:
'Importing and subscribing organization this can take a few mins.',
success: true,
});

return Organization.importOrganization(orgUid);
Expand All @@ -186,6 +197,7 @@ export const importHomeOrg = async (req, res) => {

res.json({
message: 'Importing home organization.',
success: true,
});
} catch (error) {
console.trace(error);
Expand All @@ -207,6 +219,7 @@ export const subscribeToOrganization = async (req, res) => {

return res.json({
message: 'Subscribed to organization',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down Expand Up @@ -242,6 +255,7 @@ export const deleteImportedOrg = async (req, res) => {
return res.json({
message:
'UnSubscribed to organization, you will no longer receive updates.',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down Expand Up @@ -282,6 +296,7 @@ export const unsubscribeToOrganization = async (req, res) => {
return res.json({
message:
'UnSubscribed to organization, you will no longer receive updates.',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down Expand Up @@ -322,6 +337,7 @@ export const resyncOrganization = async (req, res) => {

return res.json({
message: 'Resyncing organization completed',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down Expand Up @@ -367,6 +383,7 @@ export const addMirror = async (req, res) => {
await Organization.addMirror(req.body.storeId, req.body.url);
return res.json({
message: `Mirror added for ${req.body.storeId}.`,
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down Expand Up @@ -404,6 +421,7 @@ export const removeMirror = async (req, res) => {
await Organization.removeMirror(req.body.storeId, req.body.coinId);
return res.json({
message: `Mirror removed for ${req.body.storeId}.`,
success: true,
});
} catch (error) {
res.status(400).json({
Expand All @@ -418,6 +436,7 @@ export const sync = async (req, res) => {
Organization.syncOrganizationMeta();
return res.json({
message: 'Syncing All Organizations Metadata',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const create = async (req, res) => {
res.json({
message: 'Project staged successfully',
uuid,
success: true,
});
} catch (err) {
res.status(400).json({
Expand Down Expand Up @@ -304,6 +305,7 @@ export const updateFromXLS = async (req, res) => {

res.json({
message: 'Updates from xlsx added to staging',
success: true,
});
} catch (error) {
console.trace(error);
Expand Down Expand Up @@ -418,6 +420,7 @@ const update = async (req, res, isTransfer = false) => {

res.json({
message: 'Project update added to staging',
success: true,
});
} catch (err) {
res.status(400).json({
Expand Down Expand Up @@ -452,6 +455,7 @@ export const destroy = async (req, res) => {

res.json({
message: 'Project deleted successfully',
success: true,
});
} catch (err) {
res.status(400).json({
Expand All @@ -474,6 +478,7 @@ export const batchUpload = async (req, res) => {
res.json({
message:
'CSV processing complete, your records have been added to the staging table.',
success: true,
});
} catch (error) {
logger.error('Batch Upload Failed.', error);
Expand Down
17 changes: 15 additions & 2 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';

import { Sequelize } from 'sequelize';
import { Staging } from '../models';

import {
Expand All @@ -22,11 +23,13 @@ export const hasPendingTransactions = async (req, res) => {
res.json({
confirmed: true,
message: 'There are no pending transactions',
success: true,
});
} catch (error) {
res.json({
confirmed: false,
message: 'There are currently pending transactions',
success: true,
});
}
};
Expand Down Expand Up @@ -102,7 +105,10 @@ export const commit = async (req, res) => {
_.get(req, 'body.ids', []),
);

res.json({ message: 'Staging Table committed to full node' });
res.json({
message: 'Staging Table committed to full node',
success: true,
});
} catch (error) {
console.trace(error);
res.status(400).json({
Expand All @@ -125,6 +131,7 @@ export const destroy = async (req, res) => {
});
res.json({
message: 'Deleted from stage',
success: true,
});
} catch (error) {
res.status(400).json({
Expand All @@ -140,11 +147,16 @@ export const clean = async (req, res) => {
await assertIfReadOnlyMode();
await assertHomeOrgExists();
await Staging.destroy({
where: {},
where: {
id: {
[Sequelize.Op.ne]: null,
},
},
truncate: true,
});
res.json({
message: 'Staging Data Cleaned',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down Expand Up @@ -193,6 +205,7 @@ export const retryRecrod = async (req, res) => {
);
res.json({
message: 'Staging record re-staged.',
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down
6 changes: 6 additions & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const create = async (req, res) => {
res.json({
message: 'Unit staged successfully',
uuid,
success: true,
});
} catch (error) {
res.status(400).json({
Expand Down Expand Up @@ -318,6 +319,7 @@ export const updateFromXLS = async (req, res) => {

res.json({
message: 'Updates from xlsx added to staging',
success: true,
});
} catch (error) {
logger.error('Batch Upload Failed.', error);
Expand Down Expand Up @@ -406,6 +408,7 @@ export const update = async (req, res) => {

res.json({
message: 'Unit update added to staging',
success: true,
});
} catch (err) {
res.status(400).json({
Expand Down Expand Up @@ -437,6 +440,7 @@ export const destroy = async (req, res) => {
await Staging.upsert(stagedData);
res.json({
message: 'Unit deleted successfully',
success: true,
});
} catch (err) {
res.status(400).json({
Expand Down Expand Up @@ -524,6 +528,7 @@ export const split = async (req, res) => {

res.json({
message: 'Unit split successful',
success: true,
});
} catch (error) {
res.status(400).json({
Expand All @@ -546,6 +551,7 @@ export const batchUpload = async (req, res) => {
res.json({
message:
'CSV processing complete, your records have been added to the staging table.',
success: true,
});
} catch (error) {
logger.error('Batch Upload Failed.', error);
Expand Down
2 changes: 0 additions & 2 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const mirrorConfig =
(process.env.NODE_ENV || 'local') === 'local' ? 'mirror' : 'mirrorTest';
export const sequelizeMirror = new Sequelize(config[mirrorConfig]);

logger.info('CADT:mirror-database');

const logDebounce = _.debounce(() => {
console.log('Mirror DB not connected');
logger.info('Mirror DB not connected');
Expand Down
2 changes: 1 addition & 1 deletion src/database/migrations/20231020201652-OrgSyncStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
['organizations'].map((table) => {
queryInterface.addColumn(table, 'synced', {
type: Sequelize.BOOLEAN,
allowNull: true,
allowNull: false,
defaultValue: false,
});
}),
Expand Down
23 changes: 23 additions & 0 deletions src/database/migrations/20231020214357-OrgSyncRemainingCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

export default {
async up(queryInterface, Sequelize) {
await Promise.all(
['organizations'].map((table) => {
queryInterface.addColumn(table, 'sync_remaining', {
type: Sequelize.INTEGER,
allowNull: true,
defaultValue: false,
});
}),
);
},

async down(queryInterface) {
await Promise.all(
['organizations'].map((table) => {
queryInterface.removeColumn(table, 'sync_remaining');
}),
);
},
};
Loading

0 comments on commit bea5410

Please sign in to comment.