Skip to content

Commit

Permalink
feat: update user and organization metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
pajgo committed Oct 22, 2019
1 parent ce39559 commit 5d1c940
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/actions/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const reduce = require('lodash/reduce');
const last = require('lodash/last');

// internal deps
const SetUserMetadata = require('../utils/metadata/update-user-metadata');
const UserMetadata = require('../utils/metadata/user');
const redisKey = require('../utils/key');
const jwt = require('../utils/jwt');
const isDisposable = require('../utils/isDisposable');
Expand Down Expand Up @@ -213,7 +213,7 @@ async function performRegistration({ service, params }) {

await pipeline.exec().then(handlePipeline);

await new SetUserMetadata(service.redis).update({
await new UserMetadata(service.redis).update({
userId,
audience,
metadata: audience.map((metaAudience) => ({
Expand Down
6 changes: 3 additions & 3 deletions src/actions/updateMetadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const omit = require('lodash/omit');
const Promise = require('bluebird');
const UpdateUserMetadata = require('../utils/metadata/update-user-metadata');
const UserMetadata = require('../utils/metadata/user');
const { getUserId } = require('../utils/userData');

/**
Expand All @@ -24,10 +24,10 @@ module.exports = async function updateMetadataAction(request) {
.bind(this, request.params.username)
.then(getUserId);

const updateUserMetadata = new UpdateUserMetadata(this.redis);
const userMetadata = new UserMetadata(this.redis);
const updateParams = { ...omit(request.params, 'username'), userId };

return updateUserMetadata.update(updateParams);
return userMetadata.update(updateParams);
};

module.exports.transports = [require('@microfleet/core').ActionTransport.amqp];
6 changes: 3 additions & 3 deletions src/auth/oauth/utils/attach.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const get = require('lodash/get');
const redisKey = require('../../../utils/key');
const UpdateUserMetadata = require('../../../utils/metadata/update-user-metadata');
const UserMetadata = require('../../../utils/metadata/user');
const handlePipeline = require('../../../utils/pipelineError');
const {
USERS_SSO_TO_ID,
Expand All @@ -25,7 +25,7 @@ module.exports = async function attach(account, user) {

await pipeline.exec().then(handlePipeline);

const updateMetadata = new UpdateUserMetadata(redis);
const userMetadata = new UserMetadata(redis);
const updateParams = {
userId,
audience,
Expand All @@ -35,7 +35,7 @@ module.exports = async function attach(account, user) {
},
},
};
await updateMetadata.update(updateParams);
await userMetadata.update(updateParams);

return profile;
};
6 changes: 3 additions & 3 deletions src/auth/oauth/utils/detach.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Errors = require('common-errors');

const get = require('../../../utils/get-value');
const redisKey = require('../../../utils/key');
const UpdateUserMetadata = require('../../../utils/metadata/update-user-metadata');
const UserMetadata = require('../../../utils/metadata/user');
const handlePipeline = require('../../../utils/pipelineError');

const {
Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = async function detach(provider, userData) {

await pipeline.exec().then(handlePipeline);

const updateMetadata = new UpdateUserMetadata(redis);
const userMetadata = new UserMetadata(redis);
const updateParams = {
userId,
audience,
Expand All @@ -41,5 +41,5 @@ module.exports = async function detach(provider, userData) {
},
};

return updateMetadata.update(updateParams);
return userMetadata.update(updateParams);
};
6 changes: 3 additions & 3 deletions src/custom/cappasity-users-activate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const find = require('lodash/find');
const moment = require('moment');
const UpdateUserMetadata = require('../utils/metadata/update-user-metadata');
const UserMetadata = require('../utils/metadata/user');

/**
* Adds metadata from billing into usermix
Expand All @@ -13,7 +13,7 @@ module.exports = function mixPlan(userId, params) {
const { payments } = config;
const route = [payments.prefix, payments.routes.planGet].join('.');
const id = 'free';
const updateMetadata = new UpdateUserMetadata(this.redis);
const userMetadata = new UserMetadata(this.redis);

return amqp
.publishAndWait(route, id, { timeout: 5000 })
Expand All @@ -37,6 +37,6 @@ module.exports = function mixPlan(userId, params) {
},
};

return updateMetadata.update(updateParams);
return userMetadata.update(updateParams);
});
};
6 changes: 3 additions & 3 deletions src/custom/rfx-create-room-on-activate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const is = require('is');
const Promise = require('bluebird');
const UpdateUserMetadata = require('../utils/metadata/update-user-metadata');
const UserMetadata = require('../utils/metadata/user');

/**
* @param {String} username
Expand All @@ -22,7 +22,7 @@ function createRoom(userId, params, metadata) {
name: `${metadata[audience].stationName} | ${metadata[audience].stationSchool}`,
};

const updateMetadata = new UpdateUserMetadata(this.redis);
const userMetadata = new UserMetadata(this.redis);

return amqp.publishAndWait(route, roomParams, { timeout: 5000 })
.bind(this)
Expand All @@ -37,7 +37,7 @@ function createRoom(userId, params, metadata) {
},
};

return updateMetadata.update(updateParams);
return userMetadata.update(updateParams);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const redisKey = require('../key');
const MetaUpdate = require('./redis/update-metadata');
const { ORGANIZATIONS_METADATA, ORGANIZATIONS_AUDIENCE } = require('../../constants');

class UpdateOrganizationMetadata {
class Organization {
constructor(redis) {
this.redis = redis;
const audienceKeyTemplate = redisKey('{id}', ORGANIZATIONS_AUDIENCE);
Expand All @@ -22,4 +22,4 @@ class UpdateOrganizationMetadata {
}
}

module.exports = UpdateOrganizationMetadata;
module.exports = Organization;
2 changes: 0 additions & 2 deletions src/utils/metadata/redis/update-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ class UpdateMetadata {

const metaOps = rawMetaOps.map((opBlock) => UpdateMetadata.prepareOperations(opBlock));
scriptOpts = { metaOps, ...scriptOpts };

const updateJsonResult = await this.callLuaScript(id, scriptOpts);
console.log(updateJsonResult, scriptOpts);
return UpdateMetadata.processOpUpdateResponse(updateJsonResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const redisKey = require('../key');
const MetaUpdate = require('./redis/update-metadata');
const { USERS_METADATA, USERS_AUDIENCE } = require('../../constants');

class UpdateUserMetadata {
class User {
constructor(redis) {
this.redis = redis;
const audienceKeyTemplate = redisKey('{id}', USERS_AUDIENCE);
Expand All @@ -22,4 +22,4 @@ class UpdateUserMetadata {
}
}

module.exports = UpdateUserMetadata;
module.exports = User;
6 changes: 3 additions & 3 deletions src/utils/organization/registerOrganizationMembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
USERS_ID_FIELD,
} = require('../../constants.js');
const scrypt = require('../scrypt');
const UpdateUserMetadata = require('../metadata/update-user-metadata');
const UserMetadata = require('../metadata/user');

async function registerOrganizationMember(member) {
const { redis, config } = this;
Expand All @@ -36,8 +36,8 @@ async function registerOrganizationMember(member) {
pipeline.hset(USERS_USERNAME_TO_ID, email, userId);
await pipeline.exec().then(handlePipeline);

const updateUserMetadata = new UpdateUserMetadata(redis);
await updateUserMetadata.update({
const userMetadata = new UserMetadata(redis);
await userMetadata.update({
userId,
audience,
metadata: [{
Expand Down
12 changes: 2 additions & 10 deletions src/utils/setOrganizationMetadata.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
/* eslint-disable no-mixed-operators */
const MetaUpdate = require('../utils/metadata/redis/update-metadata');
const redisKey = require('../utils/key.js');

const { ORGANIZATIONS_METADATA, ORGANIZATIONS_AUDIENCE } = require('../constants.js');

const OrganizationMetadata = require('../utils/metadata/organization');

/**
* Updates metadata on a organization object
* @param {Object} opts
* @return {Promise}
*/
async function setOrganizationMetadata(opts) {
const audienceKeyTemplate = redisKey('{id}', ORGANIZATIONS_AUDIENCE);
const metaDataTemplate = redisKey('{id}', ORGANIZATIONS_METADATA, '{audience}');
const metaUpdater = new MetaUpdate(this.redis, metaDataTemplate, audienceKeyTemplate);
const { organizationId, ...restOpts } = opts;
return metaUpdater.update({ id: organizationId, ...restOpts });
return new OrganizationMetadata(this.redis).update(opts);
}

module.exports = setOrganizationMetadata;
11 changes: 10 additions & 1 deletion test/suites/utils/metadata/redis/update-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ describe('#updateMetadata LUA script', function updateMetadataLuaSuite() {
await metaUpdater.update(params);
});

it('sets meta', async () => {
const redisUserMetaKey = `${id}:testMeta:${audience}`;
const userDataAudience = await this.users.redis.hgetall(redisUserMetaKey);
expect(userDataAudience).to.be.deep.equal({ x: '10', c: '"cval"', b: '12' });

const userDataExtraAudience = await this.users.redis.hgetall(`${id}:testMeta:*.extra`);
expect(userDataExtraAudience).to.be.deep.equal({ x: '20', c: '"xval"', b: '22' });
});

it('tracks audienceList', async () => {
const audiencesList = await this.users.redis.smembers(`${id}:audience`);
expect(audiencesList).to.be.deep.equal(['*.localhost', '*.extra']);
Expand All @@ -53,7 +62,7 @@ describe('#updateMetadata LUA script', function updateMetadataLuaSuite() {
],
metadata: [
{
$remove: ['x','c','b'],
$remove: ['x', 'c', 'b'],
},
],
});
Expand Down

0 comments on commit 5d1c940

Please sign in to comment.