Skip to content

Commit a66e97a

Browse files
BrRenatjenyapoyarkov
authored andcommitted
feat: added handling custom audience to organization api (#413)
* feat: added handling custom audience to organization api * fix: fix json schema for organization metadata endpoints * feat: added test custom audience for organization
1 parent 0c2d22b commit a66e97a

6 files changed

Lines changed: 47 additions & 9 deletions

File tree

schemas/organization.getMetadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"properties": {
77
"organizationId": {
88
"type": "string"
9+
},
10+
"audience": {
11+
"type": "string",
12+
"minLength": 1
913
}
1014
}
1115
}

schemas/organization.updateMetadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"organizationId": {
99
"type": "string"
1010
},
11+
"audience": {
12+
"type": "string",
13+
"minLength": 1
14+
},
1115
"metadata": {
1216
"$ref": "common.json#/definitions/metadata"
1317
}

src/actions/organization/getMetadata.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const { getOrganizationMetadata, checkOrganizationExists } = require('../../util
1515
* @apiSuccess (Response) {Object} data.attributes.metadata - organization metadata
1616
*/
1717
async function organizationMetadata({ params }) {
18-
const { organizationId } = params;
18+
const { organizationId, audience } = params;
1919

20-
const metadata = await getOrganizationMetadata.call(this, organizationId);
20+
const metadata = await getOrganizationMetadata.call(this, organizationId, audience);
2121
return {
2222
data: {
2323
id: organizationId,

src/actions/organization/updateMetadata.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ const { checkOrganizationExists, getOrganizationMetadata } = require('../../util
2121
*/
2222
async function updateOrganizationMetadata({ params }) {
2323
const { config } = this;
24-
const { metadata, organizationId } = params;
25-
const { audience } = config.organizations;
24+
const { metadata, organizationId, audience } = params;
25+
const { audience: defaultAudience } = config.organizations;
2626

2727
if (metadata) {
2828
await setOrganizationMetadata.call(this, {
2929
organizationId,
30-
audience,
30+
audience: audience || defaultAudience,
3131
metadata,
3232
});
3333
}
3434

35-
const data = await getOrganizationMetadata.call(this, organizationId);
35+
const data = await getOrganizationMetadata.call(this, organizationId, audience);
3636
return {
3737
data: {
3838
id: organizationId,

src/utils/organization/getOrganizationMetadata.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const { ORGANIZATIONS_METADATA } = require('../../constants');
33
const redisKey = require('../key');
44
const JSONParse = require('../safeParse');
55

6-
async function getOrganizationMetadata(organizationId) {
6+
async function getOrganizationMetadata(organizationId, audience) {
77
const { redis, config } = this;
8-
const { audience } = config.organizations;
8+
const { audience: defaultAudience } = config.organizations;
99

10-
const metadata = await redis.hgetall(redisKey(organizationId, ORGANIZATIONS_METADATA, audience));
10+
const metadata = await redis.hgetall(redisKey(organizationId, ORGANIZATIONS_METADATA, audience || defaultAudience));
1111
return mapValues(metadata, JSONParse);
1212
}
1313

test/suites/organization/updateMetadata.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ describe('#update metadata organization', function registerSuite() {
3939
});
4040
});
4141

42+
it('must be able to update and get custom audience organization metadata', async function test() {
43+
const updatedOpts = {
44+
organizationId: this.organization.id,
45+
audience: 'test-audience',
46+
metadata: {
47+
$set: { address: 'test-audience' },
48+
},
49+
};
50+
51+
await this.dispatch('users.organization.updateMetadata', updatedOpts)
52+
.reflect()
53+
.then(inspectPromise(true))
54+
.then((createdOrganization) => {
55+
assert(createdOrganization.data.attributes.address === 'test-audience');
56+
});
57+
58+
const opts = {
59+
organizationId: this.organization.id,
60+
audience: 'test-audience',
61+
};
62+
63+
return this.dispatch('users.organization.getMetadata', opts)
64+
.reflect()
65+
.then(inspectPromise(true))
66+
.then((response) => {
67+
assert.ok(response.data.attributes);
68+
assert(response.data.attributes.address === 'test-audience');
69+
});
70+
});
71+
4272
it('must return organization not found error', async function test() {
4373
const opts = {
4474
organizationId: '1234',

0 commit comments

Comments
 (0)