Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Chia-Network/climate-warehouse i…
Browse files Browse the repository at this point in the history
…nto develop
  • Loading branch information
MichaelTaylor3D committed Sep 9, 2022
2 parents 576ed00 + 439ec24 commit 2cca00b
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 487 deletions.
351 changes: 164 additions & 187 deletions src/controllers/governance.controller.js
Original file line number Diff line number Diff line change
@@ -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,
});
}
};
Loading

0 comments on commit 2cca00b

Please sign in to comment.