-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* org member remove
- Loading branch information
Showing
16 changed files
with
504 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const RedisOrgMember = require('./redis/member'); | ||
|
||
/** | ||
* Class handing Organization Member data | ||
*/ | ||
class OrganizationMember { | ||
/** | ||
* @param {ioredis}redis | ||
* @param {String|Number}memberId | ||
* @param {String|Number}orgId | ||
*/ | ||
constructor(redis, memberId, orgId) { | ||
this.backend = new RedisOrgMember(redis); | ||
this.id = memberId; | ||
this.orgId = orgId; | ||
} | ||
|
||
/** | ||
* Deletes Organization Member record | ||
* @returns {*} | ||
*/ | ||
delete() { | ||
return this.backend.delete(this.orgId, this.id); | ||
} | ||
|
||
getOrganizationMemberKey(memberId = this.id, orgId = this.orgId) { | ||
return RedisOrgMember.getRedisKey(orgId, memberId); | ||
} | ||
|
||
static using(redis, memberId, orgId) { | ||
return new OrganizationMember(redis, memberId, orgId); | ||
} | ||
} | ||
|
||
module.exports = OrganizationMember; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const mapValues = require('lodash/mapValues'); | ||
const assert = require('assert'); | ||
const { isRedis } = require('../../../asserts/redis'); | ||
const isValidId = require('../../../asserts/id'); | ||
|
||
const { | ||
ORGANIZATIONS_MEMBERS, | ||
} = require('../../../../constants'); | ||
|
||
const JSONStringify = (data) => JSON.stringify(data); | ||
|
||
/** | ||
* Class handling Organization member Redis backend | ||
*/ | ||
class OrganizationMember { | ||
/** | ||
* @param {ioredis|Pipeline}redis | ||
*/ | ||
constructor(redis) { | ||
assert(isRedis(redis), 'must be a valid redis instance'); | ||
this.redis = redis; | ||
} | ||
|
||
/** | ||
* Generates Organization member Redis key | ||
* @param {String|Number}orgId | ||
* @param {String|Number}memberId | ||
* @returns {string} | ||
*/ | ||
static getRedisKey(orgId, memberId) { | ||
assert(isValidId(orgId), 'must be valid organization id'); | ||
assert(isValidId(memberId), 'must be valid member id'); | ||
return `${orgId}!${ORGANIZATIONS_MEMBERS}!${memberId}`; | ||
} | ||
|
||
/** | ||
* Deletes Organization Member key | ||
* @param {String|Number}orgId | ||
* @param {String|Number}memberId | ||
* @param {ioredis|Pipeline}[redis] | ||
* @returns {*} | ||
*/ | ||
delete(orgId, memberId, redis = this.redis) { | ||
assert(isRedis(redis), 'must be a valid redis instance'); | ||
return redis.del(OrganizationMember.getRedisKey(orgId, memberId)); | ||
} | ||
|
||
/** | ||
* Updates Organization member hash contents | ||
* @param {String|Number}orgId | ||
* @param {String|Number}memberId | ||
* @param {Object}params | ||
* @param redis | ||
* @returns {*} | ||
*/ | ||
update(orgId, memberId, params, redis = this.redis) { | ||
assert(isRedis(redis), 'must be a valid redis instance'); | ||
return redis.hmset(OrganizationMember.getRedisKey(orgId, memberId), OrganizationMember.stringify(params)); | ||
} | ||
|
||
static stringify(params) { | ||
return mapValues(params, JSONStringify); | ||
} | ||
} | ||
|
||
module.exports = OrganizationMember; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const Promise = require('bluebird'); | ||
const RedisOrganization = require('./redis/organization'); | ||
const OrganizationMember = require('./member/member'); | ||
|
||
/** | ||
* Class Handing Organization actions | ||
*/ | ||
class Organization { | ||
id = undefined; | ||
|
||
constructor(redis, orgId) { | ||
this.redis = redis; | ||
this.id = orgId; | ||
this.backend = new RedisOrganization(redis); | ||
} | ||
|
||
removeMember(memberId) { | ||
const orgMember = OrganizationMember.using(this.redis, memberId, this.id); | ||
const memberKey = orgMember.getOrganizationMemberKey(); | ||
return Promise.all([ | ||
orgMember.delete(), | ||
this.backend.removeMember(this.id, memberKey, this.redis), | ||
]); | ||
} | ||
|
||
static filterIds(obj) { | ||
const ids = []; | ||
const re = /^\d+$/; | ||
for (const [key] of Object.entries(obj)) { | ||
if (re.test(key)) ids.push(key); | ||
} | ||
return ids; | ||
} | ||
|
||
static using(orgId, redis) { | ||
const org = new Organization(redis, orgId); | ||
return org; | ||
} | ||
} | ||
|
||
module.exports = Organization; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const assert = require('assert'); | ||
const { isRedis } = require('../../asserts/redis'); | ||
const isValidId = require('../../asserts/id'); | ||
const isNotEmptyString = require('../../asserts/string-not-empty'); | ||
|
||
const { ORGANIZATIONS_MEMBERS } = require('../../../constants'); | ||
|
||
/** | ||
* Class handles Organization Data Redis Backend | ||
*/ | ||
class Organization { | ||
/** | ||
* @param {ioredis|Pipeline}redis | ||
*/ | ||
constructor(redis) { | ||
assert(isRedis(redis), 'must be a valid redis instance'); | ||
this.redis = redis; | ||
} | ||
|
||
/** | ||
* Gets Key with Organization information | ||
* @param {String|Number}orgId | ||
* @returns {string} | ||
*/ | ||
static getMembersKey(orgId) { | ||
assert(isValidId(orgId), 'must be valid organization id'); | ||
return `${orgId}!${ORGANIZATIONS_MEMBERS}`; | ||
} | ||
|
||
/** | ||
* Deletes provided member key from Organization Members list | ||
* @param {String|Number}orgId | ||
* @param {String}memberKey | ||
* @param {ioredis|Pipeline}[redis] | ||
* @returns {*} | ||
*/ | ||
removeMember(orgId, memberKey, redis = this.redis) { | ||
assert(isNotEmptyString(memberKey), 'must be not epty string'); | ||
return redis.zrem(Organization.getMembersKey(orgId), memberKey); | ||
} | ||
} | ||
|
||
module.exports = Organization; |
Oops, something went wrong.