diff --git a/src/controllers/governance.controller.js b/src/controllers/governance.controller.js index 615dd31e..1db33350 100644 --- a/src/controllers/governance.controller.js +++ b/src/controllers/governance.controller.js @@ -1,187 +1,164 @@ -import _ from 'lodash'; - -import { Governance, Meta } from '../models'; - -import { - assertIsActiveGovernanceBody, - assertIfReadOnlyMode, - assertWalletIsSynced, - assertCanBeGovernanceBody, -} from '../utils/data-assertions'; - -import { getConfig } from '../utils/config-loader'; -import glossaryStub from '../models/governance/glossary.stub.json'; - -const { CHIA_NETWORK } = getConfig().APP; - -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, - }); - } -}; - -export const findGlossary = async (req, res) => { - try { - if (CHIA_NETWORK === 'testnet') { - return res.json(glossaryStub); - } - - const results = await Governance.findOne({ - where: { metaKey: 'glossary' }, - }); - 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, - }); - } -}; - -export const setGlossary = async (req, res) => { - try { - await assertIfReadOnlyMode(); - await assertWalletIsSynced(); - await assertIsActiveGovernanceBody(); - - const glossary = JSON.stringify(req.body); - - await Governance.updateGoveranceBodyData([ - { key: 'glossary', value: glossary }, - ]); - - return res.json({ - message: 'Committed glossary to the datalayer', - }); - } catch (error) { - res.status(400).json({ - message: 'Cant update glossary', - 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: 'governanceBodyId' }, + }); + + 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, + }); + } +}; + +export const setGlossary = async (req, res) => { + try { + await assertIfReadOnlyMode(); + await assertWalletIsSynced(); + await assertIsActiveGovernanceBody(); + + const glossary = JSON.stringify(req.body); + + await Governance.updateGoveranceBodyData([ + { key: 'glossary', value: glossary }, + ]); + + return res.json({ + message: 'Committed glossary to the datalayer', + }); + } catch (error) { + res.status(400).json({ + message: 'Cant update glossary', + error: error.message, + }); + } +}; diff --git a/src/models/governance/governance.model.js b/src/models/governance/governance.model.js index d5285f2a..5c871fe3 100644 --- a/src/models/governance/governance.model.js +++ b/src/models/governance/governance.model.js @@ -1,232 +1,232 @@ -'use strict'; - -import Sequelize from 'sequelize'; -const { Model } = Sequelize; -import { sequelize } from '../../database'; -import { Meta } from '../../models'; -import datalayer from '../../datalayer'; -import { keyValueToChangeList } from '../../utils/datalayer-utils'; -import { getConfig } from '../../utils/config-loader'; -import { logger } from '../../config/logger.cjs'; -import { getDataModelVersion } from '../../utils/helpers'; -import PickListStub from './governance.stub.json'; - -const { GOVERANCE_BODY_ID } = getConfig().GOVERNANCE; - -const { USE_SIMULATOR, CHIA_NETWORK } = getConfig().APP; - -import ModelTypes from './governance.modeltypes.cjs'; - -class Governance extends Model { - static async createGoveranceBody() { - if (GOVERANCE_BODY_ID && GOVERANCE_BODY_ID !== '') { - throw new Error( - 'You are already listening to another governance body. Please clear GOVERANCE_BODY_ID from your env and try again', - ); - } - - const dataModelVersion = getDataModelVersion(); - const goveranceBodyId = await datalayer.createDataLayerStore(); - const governanceVersionId = await datalayer.createDataLayerStore(); - - const revertOrganizationIfFailed = async () => { - logger.info('Reverting Failed Governance Body Creation'); - await Meta.destroy({ where: { metaKey: 'goveranceBodyId' } }); - }; - - // sync the governance store - await datalayer.syncDataLayer( - goveranceBodyId, - { - [dataModelVersion]: governanceVersionId, - }, - revertOrganizationIfFailed, - ); - - const onConfirm = async () => { - logger.info('Organization confirmed, you are ready to go'); - await Meta.upsert({ - metaKey: 'goveranceBodyId', - metaValue: governanceVersionId, - }); - await Meta.upsert({ - metaKey: 'mainGoveranceBodyId', - metaValue: goveranceBodyId, - }); - }; - - if (!USE_SIMULATOR) { - logger.info('Waiting for New Governance Body to be confirmed'); - datalayer.getStoreData( - goveranceBodyId, - onConfirm, - revertOrganizationIfFailed, - ); - } else { - onConfirm(); - } - - return governanceVersionId; - } - - static async upsertGovernanceDownload(governanceData) { - const updates = []; - - if (governanceData.orgList) { - updates.push({ - metaKey: 'orgList', - metaValue: governanceData.orgList, - confirmed: true, - }); - } - - if (governanceData.pickList) { - updates.push({ - metaKey: 'pickList', - metaValue: governanceData.pickList, - confirmed: true, - }); - } else if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { - // this block is just a fallback if the app gets through the upstream checks, - // might be unnecessary - logger.info('SIMULATOR/TESTNET MODE: Using sample picklist'); - updates.push({ - metaKey: 'pickList', - metaValue: JSON.stringify(PickListStub), - confirmed: true, - }); - } - - await Promise.all(updates.map(async (update) => Governance.upsert(update))); - } - - static async sync() { - try { - if (!GOVERANCE_BODY_ID) { - throw new Error('Missing information in env to sync Governance data'); - } - - // If on simulator or testnet, use the stubbed picklist data and return - if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { - logger.info('SIMULATOR/TESTNET MODE: Using sample picklist'); - Governance.upsert({ - metaKey: 'pickList', - metaValue: JSON.stringify(PickListStub), - confirmed: true, - }); - - return; - } - - const governanceData = await datalayer.getSubscribedStoreData( - GOVERANCE_BODY_ID, - ); - - // Check if there is v1, v2, v3 ..... and if not, then we assume this is a legacy goverance table that isnt versioned - const shouldSyncLegacy = !Object.keys(governanceData).some((key) => - /^v?[0-9]+$/.test(key), - ); - - if (shouldSyncLegacy) { - await Governance.upsertGovernanceDownload(governanceData); - } - - // Check if the governance data for this version exists - const dataModelVersion = getDataModelVersion(); - if (governanceData[dataModelVersion]) { - const versionedGovernanceData = await datalayer.getSubscribedStoreData( - governanceData[dataModelVersion], - ); - - await Governance.upsertGovernanceDownload(versionedGovernanceData); - } else { - throw new Error( - `Governance data is not available from this source for ${dataModelVersion} data model.`, - ); - } - } catch (error) { - logger.error('Error Syncing Governance Data', error); - } - } - - static async updateGoveranceBodyData(keyValueArray) { - const goveranceBodyId = await Meta.findOne({ - where: { metaKey: 'goveranceBodyId' }, - raw: true, - }); - - if (!goveranceBodyId) { - throw new Error( - 'There is no Goverance Body that you own that can be edited', - ); - } - - const existingRecords = await Governance.findAll({ raw: true }); - - const changeList = []; - - await Promise.all( - keyValueArray.map(async (keyValue) => { - const valueExists = existingRecords.find( - (record) => record.metaKey === keyValue.key, - ); - - await Governance.upsert({ - metaKey: keyValue.key, - metaValue: keyValue.value, - confirmed: false, - }); - - changeList.push( - ...keyValueToChangeList(keyValue.key, keyValue.value, valueExists), - ); - }), - ); - - const rollbackChangesIfFailed = async () => { - logger.info('Reverting Goverance Records'); - await Governance.destroy({ - where: {}, - truncate: true, - }); - - await Promise.all( - existingRecords.map(async (record) => await Governance.upsert(record)), - ); - }; - - const onConfirm = async () => { - await Promise.all( - keyValueArray.map(async (keyValue) => { - await Governance.upsert({ - metaKey: keyValue.key, - metaValue: keyValue.value, - confirmed: true, - }); - }), - ); - }; - - await datalayer.pushDataLayerChangeList( - goveranceBodyId.metaValue, - changeList, - ); - - datalayer.getStoreData( - goveranceBodyId.metaValue, - onConfirm, - rollbackChangesIfFailed, - ); - } -} - -Governance.init(ModelTypes, { - sequelize, - modelName: 'governance', - freezeTableName: true, - timestamps: false, - createdAt: false, - updatedAt: false, -}); - -export { Governance }; +'use strict'; + +import Sequelize from 'sequelize'; +const { Model } = Sequelize; +import { sequelize } from '../../database'; +import { Meta } from '../../models'; +import datalayer from '../../datalayer'; +import { keyValueToChangeList } from '../../utils/datalayer-utils'; +import { getConfig } from '../../utils/config-loader'; +import { logger } from '../../config/logger.cjs'; +import { getDataModelVersion } from '../../utils/helpers'; +import PickListStub from './governance.stub.json'; + +const { GOVERNANCE_BODY_ID } = getConfig().GOVERNANCE; + +const { USE_SIMULATOR, CHIA_NETWORK } = getConfig().APP; + +import ModelTypes from './governance.modeltypes.cjs'; + +class Governance extends Model { + static async createGoveranceBody() { + if (GOVERNANCE_BODY_ID && GOVERNANCE_BODY_ID !== '') { + throw new Error( + 'You are already listening to another governance body. Please clear GOVERNANCE_BODY_ID from your env and try again', + ); + } + + const dataModelVersion = getDataModelVersion(); + const governanceBodyId = await datalayer.createDataLayerStore(); + const governanceVersionId = await datalayer.createDataLayerStore(); + + const revertOrganizationIfFailed = async () => { + logger.info('Reverting Failed Governance Body Creation'); + await Meta.destroy({ where: { metaKey: 'governanceBodyId' } }); + }; + + // sync the governance store + await datalayer.syncDataLayer( + governanceBodyId, + { + [dataModelVersion]: governanceVersionId, + }, + revertOrganizationIfFailed, + ); + + const onConfirm = async () => { + logger.info('Organization confirmed, you are ready to go'); + await Meta.upsert({ + metaKey: 'governanceBodyId', + metaValue: governanceVersionId, + }); + await Meta.upsert({ + metaKey: 'mainGoveranceBodyId', + metaValue: governanceBodyId, + }); + }; + + if (!USE_SIMULATOR) { + logger.info('Waiting for New Governance Body to be confirmed'); + datalayer.getStoreData( + governanceBodyId, + onConfirm, + revertOrganizationIfFailed, + ); + } else { + onConfirm(); + } + + return governanceVersionId; + } + + static async upsertGovernanceDownload(governanceData) { + const updates = []; + + if (governanceData.orgList) { + updates.push({ + metaKey: 'orgList', + metaValue: governanceData.orgList, + confirmed: true, + }); + } + + if (governanceData.pickList) { + updates.push({ + metaKey: 'pickList', + metaValue: governanceData.pickList, + confirmed: true, + }); + } else if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { + // this block is just a fallback if the app gets through the upstream checks, + // might be unnecessary + logger.info('SIMULATOR/TESTNET MODE: Using sample picklist'); + updates.push({ + metaKey: 'pickList', + metaValue: JSON.stringify(PickListStub), + confirmed: true, + }); + } + + await Promise.all(updates.map(async (update) => Governance.upsert(update))); + } + + static async sync() { + try { + if (!GOVERNANCE_BODY_ID) { + throw new Error('Missing information in env to sync Governance data'); + } + + // If on simulator or testnet, use the stubbed picklist data and return + if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { + logger.info('SIMULATOR/TESTNET MODE: Using sample picklist'); + Governance.upsert({ + metaKey: 'pickList', + metaValue: JSON.stringify(PickListStub), + confirmed: true, + }); + + return; + } + + const governanceData = await datalayer.getSubscribedStoreData( + GOVERNANCE_BODY_ID, + ); + + // Check if there is v1, v2, v3 ..... and if not, then we assume this is a legacy governance table that isnt versioned + const shouldSyncLegacy = !Object.keys(governanceData).some((key) => + /^v?[0-9]+$/.test(key), + ); + + if (shouldSyncLegacy) { + await Governance.upsertGovernanceDownload(governanceData); + } + + // Check if the governance data for this version exists + const dataModelVersion = getDataModelVersion(); + if (governanceData[dataModelVersion]) { + const versionedGovernanceData = await datalayer.getSubscribedStoreData( + governanceData[dataModelVersion], + ); + + await Governance.upsertGovernanceDownload(versionedGovernanceData); + } else { + throw new Error( + `Governance data is not available from this source for ${dataModelVersion} data model.`, + ); + } + } catch (error) { + logger.error('Error Syncing Governance Data', error); + } + } + + static async updateGoveranceBodyData(keyValueArray) { + const governanceBodyId = await Meta.findOne({ + where: { metaKey: 'governanceBodyId' }, + raw: true, + }); + + if (!governanceBodyId) { + throw new Error( + 'There is no Goverance Body that you own that can be edited', + ); + } + + const existingRecords = await Governance.findAll({ raw: true }); + + const changeList = []; + + await Promise.all( + keyValueArray.map(async (keyValue) => { + const valueExists = existingRecords.find( + (record) => record.metaKey === keyValue.key, + ); + + await Governance.upsert({ + metaKey: keyValue.key, + metaValue: keyValue.value, + confirmed: false, + }); + + changeList.push( + ...keyValueToChangeList(keyValue.key, keyValue.value, valueExists), + ); + }), + ); + + const rollbackChangesIfFailed = async () => { + logger.info('Reverting Goverance Records'); + await Governance.destroy({ + where: {}, + truncate: true, + }); + + await Promise.all( + existingRecords.map(async (record) => await Governance.upsert(record)), + ); + }; + + const onConfirm = async () => { + await Promise.all( + keyValueArray.map(async (keyValue) => { + await Governance.upsert({ + metaKey: keyValue.key, + metaValue: keyValue.value, + confirmed: true, + }); + }), + ); + }; + + await datalayer.pushDataLayerChangeList( + governanceBodyId.metaValue, + changeList, + ); + + datalayer.getStoreData( + governanceBodyId.metaValue, + onConfirm, + rollbackChangesIfFailed, + ); + } +} + +Governance.init(ModelTypes, { + sequelize, + modelName: 'governance', + freezeTableName: true, + timestamps: false, + createdAt: false, + updatedAt: false, +}); + +export { Governance }; diff --git a/src/tasks/sync-governance-body.js b/src/tasks/sync-governance-body.js index ab9ce942..fc494286 100644 --- a/src/tasks/sync-governance-body.js +++ b/src/tasks/sync-governance-body.js @@ -9,7 +9,7 @@ import { getConfig } from '../utils/config-loader'; import { logger } from '../config/logger.cjs'; import { Organization } from '../models'; -const { GOVERANCE_BODY_ID } = getConfig().GOVERNANCE; +const { GOVERNANCE_BODY_ID } = getConfig().GOVERNANCE; import dotenv from 'dotenv'; dotenv.config(); @@ -22,12 +22,12 @@ const task = new Task('sync-governance-meta', async () => { await assertWalletIsSynced(); logger.info('Syncing governance data'); - if (GOVERANCE_BODY_ID) { - logger.info(`Governance Config Found ${GOVERANCE_BODY_ID}`); + if (GOVERNANCE_BODY_ID) { + logger.info(`Governance Config Found ${GOVERNANCE_BODY_ID}`); const myOrganization = await Organization.getHomeOrg(); - if (_.get(myOrganization, 'orgUid', '') !== GOVERANCE_BODY_ID) { + if (_.get(myOrganization, 'orgUid', '') !== GOVERNANCE_BODY_ID) { Governance.sync(); } } diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index 46e887b8..ab88c1fa 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -33,7 +33,7 @@ export const assertCanBeGovernanceBody = async () => { export const assertIsActiveGovernanceBody = async () => { const governanceBodyIsSetUp = Meta.findAll({ - where: { metaKey: 'goveranceBodyId' }, + where: { metaKey: 'governanceBodyId' }, }); if (!governanceBodyIsSetUp) { diff --git a/src/utils/data-loaders.js b/src/utils/data-loaders.js index a159dfa2..6a61d191 100644 --- a/src/utils/data-loaders.js +++ b/src/utils/data-loaders.js @@ -1,62 +1,62 @@ -import _ from 'lodash'; - -import request from 'request-promise'; - -import { Governance } from '../models'; -import PickListStub from '../models/governance/governance.stub.json'; -import { getConfig } from '../utils/config-loader'; -import { logger } from '../config/logger.cjs'; - -const { USE_SIMULATOR, CHIA_NETWORK } = getConfig().APP; - -let downloadedPickList = {}; -export const getPicklistValues = () => downloadedPickList; - -export const pullPickListValues = async () => { - if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { - downloadedPickList = PickListStub; - } else { - const goveranceData = await Governance.findOne({ - where: { metaKey: 'pickList' }, - raw: true, - }); - - if (_.get(goveranceData, 'metaValue')) { - downloadedPickList = JSON.parse(goveranceData.metaValue); - } - } - - return downloadedPickList; -}; - -export const getDefaultOrganizationList = async () => { - if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { - return []; - } else { - const goveranceData = await Governance.findOne({ - where: { metaKey: 'orgList' }, - raw: true, - }); - - return JSON.parse(_.get(goveranceData, 'metaValue', '[]')); - } -}; - -export const serverAvailable = async (server, port) => { - const options = { - method: 'GET', - url: `http://${server}:${port}`, - }; - - try { - await request(Object.assign({}, options)); - return true; - } catch (err) { - if (JSON.stringify(err).includes('Python')) { - logger.info(`SERVER IS AVAILABLE ${server}`); - return true; - } else { - return false; - } - } -}; +import _ from 'lodash'; + +import request from 'request-promise'; + +import { Governance } from '../models'; +import PickListStub from '../models/governance/governance.stub.json'; +import { getConfig } from '../utils/config-loader'; +import { logger } from '../config/logger.cjs'; + +const { USE_SIMULATOR, CHIA_NETWORK } = getConfig().APP; + +let downloadedPickList = {}; +export const getPicklistValues = () => downloadedPickList; + +export const pullPickListValues = async () => { + if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { + downloadedPickList = PickListStub; + } else { + const governanceData = await Governance.findOne({ + where: { metaKey: 'pickList' }, + raw: true, + }); + + if (_.get(governanceData, 'metaValue')) { + downloadedPickList = JSON.parse(governanceData.metaValue); + } + } + + return downloadedPickList; +}; + +export const getDefaultOrganizationList = async () => { + if (USE_SIMULATOR || CHIA_NETWORK === 'testnet') { + return []; + } else { + const governanceData = await Governance.findOne({ + where: { metaKey: 'orgList' }, + raw: true, + }); + + return JSON.parse(_.get(governanceData, 'metaValue', '[]')); + } +}; + +export const serverAvailable = async (server, port) => { + const options = { + method: 'GET', + url: `http://${server}:${port}`, + }; + + try { + await request(Object.assign({}, options)); + return true; + } catch (err) { + if (JSON.stringify(err).includes('Python')) { + logger.info(`SERVER IS AVAILABLE ${server}`); + return true; + } else { + return false; + } + } +}; diff --git a/src/utils/defaultConfig.json b/src/utils/defaultConfig.json index e1633de2..0a90f210 100644 --- a/src/utils/defaultConfig.json +++ b/src/utils/defaultConfig.json @@ -17,6 +17,6 @@ "DEFAULT_FEE": 1000000000 }, "GOVERNANCE": { - "GOVERANCE_BODY_ID": "a8eadac331c82f896d6851a032fd8777605a5c60fc523b0edf7eb75adaf3ba81" + "GOVERNANCE_BODY_ID": "a8eadac331c82f896d6851a032fd8777605a5c60fc523b0edf7eb75adaf3ba81" } }