diff --git a/src/config/config.cjs b/src/config/config.cjs index 628bc031..fff1639f 100644 --- a/src/config/config.cjs +++ b/src/config/config.cjs @@ -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`, ); @@ -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'); @@ -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: { diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index d7ebf4a9..2793055a 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -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.', diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index 09ccf00b..3a1388c7 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -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( diff --git a/src/routes/v1/resources/organization.js b/src/routes/v1/resources/organization.js index 0aabeaca..8be7bd79 100644 --- a/src/routes/v1/resources/organization.js +++ b/src/routes/v1/resources/organization.js @@ -5,6 +5,7 @@ import joiExpress from 'express-joi-validation'; import { OrganizationController } from '../../../controllers'; import { + newOrganizationSchema, unsubscribeOrganizationSchema, subscribeOrganizationSchema, importOrganizationSchema, @@ -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); }); diff --git a/src/tasks/sync-audit-table.js b/src/tasks/sync-audit-table.js index 63607ac3..c3a35d9f 100644 --- a/src/tasks/sync-audit-table.js +++ b/src/tasks/sync-audit-table.js @@ -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, diff --git a/src/tasks/sync-default-organizations.js b/src/tasks/sync-default-organizations.js index 68ecca0a..ef25ed5f 100644 --- a/src/tasks/sync-default-organizations.js +++ b/src/tasks/sync-default-organizations.js @@ -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) { diff --git a/src/tasks/sync-organization-meta.js b/src/tasks/sync-organization-meta.js index cd4c7b2c..1e3e5c7c 100644 --- a/src/tasks/sync-organization-meta.js +++ b/src/tasks/sync-organization-meta.js @@ -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) { diff --git a/src/utils/config-loader.js b/src/utils/config-loader.js index 1442d1d3..d148575a 100644 --- a/src/utils/config-loader.js +++ b/src/utils/config-loader.js @@ -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}`); } }); diff --git a/src/validations/organizations.validations.js b/src/validations/organizations.validations.js index 83fb7e36..e88f2d63 100644 --- a/src/validations/organizations.validations.js +++ b/src/validations/organizations.validations.js @@ -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({ diff --git a/tests/resources/organization.spec.js b/tests/resources/organization.spec.js index 9980928c..88cd357b 100644 --- a/tests/resources/organization.spec.js +++ b/tests/resources/organization.spec.js @@ -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( @@ -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);