Skip to content

Commit

Permalink
feat: adding metadata to org
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2sw committed Sep 2, 2022
1 parent 68ed8a2 commit ddf1d11
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 8 deletions.
38 changes: 38 additions & 0 deletions docs/postman/Organization API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,44 @@
}
},
"response": []
},
{
"name": "add_metadata",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "hello",
"value": "world",
"type": "text"
}
]
},
"url": {
"raw": "http://localhost:31310/v1/organizations/metadata",
"protocol": "http",
"host": [
"localhost"
],
"port": "31310",
"path": [
"v1",
"organizations",
"metadata"
],
"query": [
{
"key": "test",
"value": "hello world",
"disabled": true
}
]
}
},
"response": []
}
]
}
21 changes: 21 additions & 0 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,27 @@ export const resyncOrganization = async (req, res) => {
}
};

export const addMetadata = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertWalletIsSynced();
await assertHomeOrgExists();

Organization.addMetadata(req.body);

return res.json({
message: 'Home org currently being updated, will be completed soon.',
});
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Error adding metadata to your organization',
error: error.message,
});
}
};


export const addMirror = async (req, res) => {
try {
await assertIfReadOnlyMode();
Expand Down
23 changes: 23 additions & 0 deletions src/database/migrations/20220831023546-add-org-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

export default {
async up(queryInterface, Sequelize) {
await Promise.all(
['organizations'].map((table) => {
queryInterface.addColumn(table, 'metadata', {
type: Sequelize.String,
allowNull: true,
defaultValue: false,
});
}),
);
},

async down(queryInterface) {
await Promise.all(
[].map((table) => {
queryInterface.removeColumn(table, 'metadata');
}),
);
},
};
5 changes: 5 additions & 0 deletions src/database/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import AddFiltStoreSubscribedColumnToProject from './20220809182156-AddFileStore
import PopulateUnitsFTS from './20220808192709-populate-units-fts';
import ResetDBForNewSingletons from './20220816155101-reset-db-for-new-singletons';
import AddIsTransferColumn from './20220825124702-add-isTransfer-column';
import AddOrgMetadata from './20220831023546-add-org-metadata';

export const migrations = [
{
Expand Down Expand Up @@ -159,4 +160,8 @@ export const migrations = [
migration: AddIsTransferColumn,
name: '20220825124702-add-isTransfer-column',
},
{
migration: AddOrgMetadata,
name: '20220831023546-add-org-metadata',
},
];
34 changes: 26 additions & 8 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

import Sequelize from 'sequelize';
import _ from 'lodash';

const { Model } = Sequelize;

import { sequelize } from '../../database';

import datalayer from '../../datalayer';

import { logger } from '../../config/logger.cjs';

import { getDefaultOrganizationList } from '../../utils/data-loaders';
Expand Down Expand Up @@ -302,16 +304,23 @@ class Organization extends Model {
await Promise.all(
allSubscribedOrganizations.map((organization) => {
const onResult = (data) => {
const updateData = data.reduce((update, current) => {
// TODO: this needs to pull the v1 record
if (current.key !== 'registryId') {
const updateData = data
.filter(({ dataEntry }) => current.key !== 'registryId' || !dataEntry.key.includes('meta_'))
.reduce((update, current) => {
update[current.key] = current.value;
return update;
}, {});

// will return metadata fields. i.e.: { meta_key1: 'value1', meta_key2: 'value2' }
const metadata = data
.filter(({ dataEntry }) => dataEntry.key.includes('meta_'))
.reduce((update, current) => {
update[current.key] = current.value;
}
return update;
}, {});
return update;
}, {});

Organization.update(
{ ...updateData },
{ ...updateData, metadata },
{
where: { orgUid: organization.orgUid },
},
Expand Down Expand Up @@ -386,6 +395,15 @@ class Organization extends Model {
await datalayer.upsertDataLayer(myOrganization.orgUid, payload);
};

static addMetadata = async (payload) => {
const myOrganization = await Organization.getHomeOrg();

// Prefix keys with "meta_"
const metadata = _.mapKeys(payload, (_value, key) => `meta_${key}`);

await datalayer.upsertDataLayer(myOrganization.orgUid, metadata);
};

static removeMirror = async (storeId, coinId) => {
datalayer.removeMirror(storeId, coinId);
};
Expand Down
7 changes: 7 additions & 0 deletions src/routes/v1/resources/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ OrganizationRouter.post(
},
);

OrganizationRouter.post(
'/metadata',
(req, res) => {
return OrganizationController.addMetadata(req, res);
},
);

export { OrganizationRouter };

0 comments on commit ddf1d11

Please sign in to comment.