Skip to content

Commit

Permalink
fix: simulator in yml file
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Apr 26, 2022
1 parent 5d93fd6 commit 304b1b4
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/config/config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const persistanceFolder = `${homeDir}/.chia/climate-warehouse`;

// Adding this duplicate function here because im having trouble
// importing it in from utils folder
const getConfig = () => {
const getConfig = _.memoize(() => {
const configFile = path.resolve(
`${homeDir}/.chia/climate-warehouse/config.yaml`,
);
Expand All @@ -25,7 +25,7 @@ const getConfig = () => {
// if it still doesnt exist that means we are in an env without write permissions
// so just load the default en
if (typeof process.env.USE_SIMULATOR === 'string') {
defaultConfig.APP.USE_SIMULATOR = process.env.USE_SIMULATOR === 'true';
defaultConfig.APP.USE_SIMULATOR = true;
}

console.log('Cant write config file, falling back to defaults');
Expand All @@ -37,14 +37,14 @@ const getConfig = () => {
const yml = yaml.load(fs.readFileSync(configFile, 'utf8'));

if (typeof process.env.USE_SIMULATOR === 'string') {
yml.APP.USE_SIMULATOR = process.env.USE_SIMULATOR === 'true';
yml.APP.USE_SIMULATOR = true;
}

return yml;
} catch (e) {
console.log(e, `Config file not found at ${configFile}`);
}
};
});

module.exports = {
local: {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const createV2 = async (req, res) => {

const { name } = req.body;
const buffer = req.files.file.data;
const icon = buffer.toString('base64');
const icon = `data:image/png;base64, ${buffer.toString('base64')}`;

return res.json({
message: 'New organization created successfully.',
Expand Down
4 changes: 1 addition & 3 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ class Organization extends Model {
};

const onFail = () => {
throw new Error(
`Unable to sync metadata from ${organization.orgUid}`,
);
log(`Unable to sync metadata from ${organization.orgUid}`);
};

datalayer.getStoreIfUpdated(
Expand Down
9 changes: 9 additions & 0 deletions src/routes/v1/resources/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import joiExpress from 'express-joi-validation';

import { OrganizationController } from '../../../controllers';
import {
newOrganizationSchema,
unsubscribeOrganizationSchema,
subscribeOrganizationSchema,
importOrganizationSchema,
Expand All @@ -21,6 +22,14 @@ OrganizationRouter.delete('/', (req, res) => {
return OrganizationController.resetHomeOrg(req, res);
});

OrganizationRouter.post(
'/',
validator.body(newOrganizationSchema),
(req, res) => {
return OrganizationController.create(req, res);
},
);

OrganizationRouter.post('/create', (req, res) => {
return OrganizationController.createV2(req, res);
});
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const task = new Task('sync-audit', async () => {
await assertWalletIsSynced();

log('Syncing Audit Information');
if (process.env.USE_SIMULATOR === 'false') {
if (!USE_SIMULATOR) {
const organizations = await Organization.findAll({
where: { subscribed: true },
raw: true,
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/sync-default-organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const task = new Task('sync-default-organizations', async () => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();
if (USE_SIMULATOR === 'false') {
if (!USE_SIMULATOR) {
Organization.subscribeToDefaultOrganizations();
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/sync-organization-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const task = new Task('sync-organization-meta', async () => {
await assertDataLayerAvailable();
await assertWalletIsSynced();
log('Syncing subscribed organizations');
if (USE_SIMULATOR === 'false') {
if (!USE_SIMULATOR) {
Organization.syncOrganizationMeta();
}
} catch (error) {
Expand Down
20 changes: 15 additions & 5 deletions src/utils/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,27 @@ export const getConfig = _.memoize(() => {
} catch (err) {
// if it still doesnt exist that means we are in an env without write permissions
// so just load the default env
if (typeof process.env.USE_SIMULATOR === 'string') {
defaultConfig.APP.USE_SIMULATOR =
_.get(process, 'env.USE_SIMULATOR', 'false') === 'true';
if (process.env.USE_SIMULATOR) {
defaultConfig.APP.USE_SIMULATOR = true;
defaultConfig.APP.CHIA_NETWORK = 'testnet';
}

return yaml.load(yaml.dump(defaultConfig));
}
}

return yaml.load(fs.readFileSync(configFile, 'utf8'));
try {
const yml = yaml.load(fs.readFileSync(configFile, 'utf8'));

if (typeof process.env.USE_SIMULATOR === 'string') {
yml.APP.USE_SIMULATOR = true;
}

return yml;
} catch (e) {
console.log(e, `Config file not found at ${configFile}`);
}
} catch (e) {
console.log(`Config file not found at ${configFile}`.e);
console.log(`Config file not found at ${configFile}`);
}
});
2 changes: 1 addition & 1 deletion src/validations/organizations.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Joi from 'joi';

export const newOrganizationSchema = Joi.object({
name: Joi.string().required(),
icon: Joi.string().required(),
file: Joi.string().required(),
});

export const importOrganizationSchema = Joi.object({
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/organization.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Orgainzation Resource CRUD', function () {

const response = await supertest(app).post(`/v1/organizations`).send({
name: orgName,
icon: 'https://climate-warehouse.s3.us-west-2.amazonaws.com/public/orgs/me.svg',
file: 'https://climate-warehouse.s3.us-west-2.amazonaws.com/public/orgs/me.svg',
});

expect(response.body.message).to.equal(
Expand All @@ -32,7 +32,7 @@ describe('Orgainzation Resource CRUD', function () {
const response = await supertest(app).get(`/v1/organizations`).send();

expect(Object.values(response.body)[0].name).to.equal(orgName);
expect(Object.values(response.body)[0].icon).to.equal(
expect(Object.values(response.body)[0].file).to.equal(
'https://climate-warehouse.s3.us-west-2.amazonaws.com/public/orgs/me.svg',
);
}).timeout(TEST_WAIT_TIME * 10);
Expand Down

0 comments on commit 304b1b4

Please sign in to comment.