-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: endpoint to check if governance was created
- Loading branch information
1 parent
1e40b00
commit 5306ac7
Showing
2 changed files
with
201 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,141 @@ | ||
import _ from 'lodash'; | ||
|
||
import { Governance } from '../models'; | ||
|
||
import { | ||
assertIsActiveGovernanceBody, | ||
assertIfReadOnlyMode, | ||
assertWalletIsSynced, | ||
assertCanBeGovernanceBody, | ||
} from '../utils/data-assertions'; | ||
|
||
export const findAll = async (req, res) => { | ||
try { | ||
const results = await Governance.findAll(); | ||
return res.json(results); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retreive Governance Data', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
export const findOrgList = async (req, res) => { | ||
try { | ||
const results = await Governance.findOne({ where: { metaKey: 'orgList' } }); | ||
return res.json(JSON.parse(_.get(results, 'metaValue', {}))); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retreive Governance Data', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
export const findPickList = async (req, res) => { | ||
try { | ||
const results = await Governance.findOne({ | ||
where: { metaKey: 'pickList' }, | ||
}); | ||
return res.json(JSON.parse(_.get(results, 'metaValue', {}))); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retreive Governance Data', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export const createGoveranceBody = async (req, res) => { | ||
try { | ||
await assertIfReadOnlyMode(); | ||
await assertWalletIsSynced(); | ||
await assertCanBeGovernanceBody(); | ||
|
||
Governance.createGoveranceBody(); | ||
|
||
return res.json({ | ||
message: | ||
'Setting up new Governance Body on this node, this can take a few mins', | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Cant create Governance Body', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export const setDefaultOrgList = async (req, res) => { | ||
try { | ||
await assertIfReadOnlyMode(); | ||
await assertWalletIsSynced(); | ||
await assertIsActiveGovernanceBody(); | ||
|
||
const orgList = JSON.stringify(req.body); | ||
|
||
await Governance.updateGoveranceBodyData([ | ||
{ key: 'orgList', value: orgList }, | ||
]); | ||
|
||
return res.json({ | ||
message: 'Committed this new organization list to the datalayer', | ||
}); | ||
} catch (error) { | ||
console.trace(error); | ||
res.status(400).json({ | ||
message: 'Cant update default orgs', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export const setPickList = async (req, res) => { | ||
try { | ||
await assertIfReadOnlyMode(); | ||
await assertWalletIsSynced(); | ||
await assertIsActiveGovernanceBody(); | ||
|
||
const pickList = JSON.stringify(req.body); | ||
|
||
await Governance.updateGoveranceBodyData([ | ||
{ key: 'pickList', value: pickList }, | ||
]); | ||
|
||
return res.json({ | ||
message: 'Committed this pick list to the datalayer', | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Cant update picklist', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
import _ from 'lodash'; | ||
|
||
import { Governance, Meta } from '../models'; | ||
|
||
import { | ||
assertIsActiveGovernanceBody, | ||
assertIfReadOnlyMode, | ||
assertWalletIsSynced, | ||
assertCanBeGovernanceBody, | ||
} from '../utils/data-assertions'; | ||
|
||
export const findAll = async (req, res) => { | ||
try { | ||
const results = await Governance.findAll(); | ||
return res.json(results); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retreive Governance Data', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
export const isCreated = async (req, res) => { | ||
try { | ||
const results = await Meta.findOne({ | ||
where: { metaKey: 'goveranceBodyId' }, | ||
}); | ||
|
||
if (results) { | ||
return res.json({ | ||
created: true, | ||
}); | ||
} else { | ||
return res.json({ | ||
created: false, | ||
}); | ||
} | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retreive Governance Data', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
export const findOrgList = async (req, res) => { | ||
try { | ||
const results = await Governance.findOne({ where: { metaKey: 'orgList' } }); | ||
return res.json(JSON.parse(_.get(results, 'metaValue', {}))); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retreive Governance Data', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
export const findPickList = async (req, res) => { | ||
try { | ||
const results = await Governance.findOne({ | ||
where: { metaKey: 'pickList' }, | ||
}); | ||
return res.json(JSON.parse(_.get(results, 'metaValue', {}))); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Can not retreive Governance Data', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export const createGoveranceBody = async (req, res) => { | ||
try { | ||
await assertIfReadOnlyMode(); | ||
await assertWalletIsSynced(); | ||
await assertCanBeGovernanceBody(); | ||
|
||
Governance.createGoveranceBody(); | ||
|
||
return res.json({ | ||
message: | ||
'Setting up new Governance Body on this node, this can take a few mins', | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Cant create Governance Body', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export const setDefaultOrgList = async (req, res) => { | ||
try { | ||
await assertIfReadOnlyMode(); | ||
await assertWalletIsSynced(); | ||
await assertIsActiveGovernanceBody(); | ||
|
||
const orgList = JSON.stringify(req.body); | ||
|
||
await Governance.updateGoveranceBodyData([ | ||
{ key: 'orgList', value: orgList }, | ||
]); | ||
|
||
return res.json({ | ||
message: 'Committed this new organization list to the datalayer', | ||
}); | ||
} catch (error) { | ||
console.trace(error); | ||
res.status(400).json({ | ||
message: 'Cant update default orgs', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export const setPickList = async (req, res) => { | ||
try { | ||
await assertIfReadOnlyMode(); | ||
await assertWalletIsSynced(); | ||
await assertIsActiveGovernanceBody(); | ||
|
||
const pickList = JSON.stringify(req.body); | ||
|
||
await Governance.updateGoveranceBodyData([ | ||
{ key: 'pickList', value: pickList }, | ||
]); | ||
|
||
return res.json({ | ||
message: 'Committed this pick list to the datalayer', | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ | ||
message: 'Cant update picklist', | ||
error: error.message, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,60 @@ | ||
'use strict'; | ||
|
||
import express from 'express'; | ||
import joiExpress from 'express-joi-validation'; | ||
|
||
import { GovernanceController } from '../../../controllers'; | ||
import { | ||
governanceSubscribeSchema, | ||
setOrgListSchema, | ||
governancePickListSchema, | ||
} from '../../../validations'; | ||
|
||
const validator = joiExpress.createValidator({ passError: true }); | ||
const GovernanceRouter = express.Router(); | ||
|
||
GovernanceRouter.get('/', (req, res) => { | ||
return GovernanceController.findAll(req, res); | ||
}); | ||
|
||
GovernanceRouter.get('/meta/orgList', (req, res) => { | ||
return GovernanceController.findOrgList(req, res); | ||
}); | ||
|
||
GovernanceRouter.get('/meta/pickList', (req, res) => { | ||
return GovernanceController.findPickList(req, res); | ||
}); | ||
|
||
GovernanceRouter.post('/', (req, res) => { | ||
return GovernanceController.createGoveranceBody(req, res); | ||
}); | ||
|
||
GovernanceRouter.post( | ||
'/meta/orgList', | ||
validator.body(setOrgListSchema), | ||
(req, res) => { | ||
return GovernanceController.setDefaultOrgList(req, res); | ||
}, | ||
); | ||
|
||
GovernanceRouter.post( | ||
'/meta/pickList', | ||
validator.body(governancePickListSchema), | ||
(req, res) => { | ||
return GovernanceController.setPickList(req, res); | ||
}, | ||
); | ||
|
||
GovernanceRouter.post( | ||
'/subscribe', | ||
validator.body(governanceSubscribeSchema), | ||
(req, res) => { | ||
return GovernanceController.subscribeToGovernanceBody(req, res); | ||
}, | ||
); | ||
|
||
export { GovernanceRouter }; | ||
'use strict'; | ||
|
||
import express from 'express'; | ||
import joiExpress from 'express-joi-validation'; | ||
|
||
import { GovernanceController } from '../../../controllers'; | ||
import { | ||
governanceSubscribeSchema, | ||
setOrgListSchema, | ||
governancePickListSchema, | ||
} from '../../../validations'; | ||
|
||
const validator = joiExpress.createValidator({ passError: true }); | ||
const GovernanceRouter = express.Router(); | ||
|
||
GovernanceRouter.get('/exists', (req, res) => { | ||
return GovernanceController.isCreated(req, res); | ||
}); | ||
|
||
GovernanceRouter.get('/', (req, res) => { | ||
return GovernanceController.findAll(req, res); | ||
}); | ||
|
||
GovernanceRouter.get('/meta/orgList', (req, res) => { | ||
return GovernanceController.findOrgList(req, res); | ||
}); | ||
|
||
GovernanceRouter.get('/meta/pickList', (req, res) => { | ||
return GovernanceController.findPickList(req, res); | ||
}); | ||
|
||
GovernanceRouter.post('/', (req, res) => { | ||
return GovernanceController.createGoveranceBody(req, res); | ||
}); | ||
|
||
GovernanceRouter.post( | ||
'/meta/orgList', | ||
validator.body(setOrgListSchema), | ||
(req, res) => { | ||
return GovernanceController.setDefaultOrgList(req, res); | ||
}, | ||
); | ||
|
||
GovernanceRouter.post( | ||
'/meta/pickList', | ||
validator.body(governancePickListSchema), | ||
(req, res) => { | ||
return GovernanceController.setPickList(req, res); | ||
}, | ||
); | ||
|
||
GovernanceRouter.post( | ||
'/subscribe', | ||
validator.body(governanceSubscribeSchema), | ||
(req, res) => { | ||
return GovernanceController.subscribeToGovernanceBody(req, res); | ||
}, | ||
); | ||
|
||
export { GovernanceRouter }; |