From e5025c7b4305621e06030f8c7f5e973216ab53e3 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sat, 21 Oct 2023 15:19:46 -0700 Subject: [PATCH] Added populate admin for listing route payloads --- requests/Switcher API.postman_collection.json | 18 ++++++++++++++ src/api-docs/paths/path-domain.js | 13 ++++++++++ src/routers/config-strategy.js | 1 + src/routers/config.js | 1 + src/routers/domain.js | 17 +++++++++++++ src/routers/group-config.js | 1 + src/services/config-strategy.js | 6 +++++ src/services/config.js | 6 +++++ src/services/group-config.js | 6 +++++ tests/config-strategy.test.js | 1 + tests/config.test.js | 1 + tests/domain.test.js | 24 +++++++++++++++++-- tests/group-config.test.js | 1 + 13 files changed, 94 insertions(+), 2 deletions(-) diff --git a/requests/Switcher API.postman_collection.json b/requests/Switcher API.postman_collection.json index 18d184a..c5f7f93 100644 --- a/requests/Switcher API.postman_collection.json +++ b/requests/Switcher API.postman_collection.json @@ -2806,6 +2806,24 @@ }, "response": [] }, + { + "name": "Domain - Read Collab", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/domain/collaboration", + "host": [ + "{{url}}" + ], + "path": [ + "domain", + "collaboration" + ] + } + }, + "response": [] + }, { "name": "Domain - Read Single", "request": { diff --git a/src/api-docs/paths/path-domain.js b/src/api-docs/paths/path-domain.js index f1a90c6..ba9f436 100644 --- a/src/api-docs/paths/path-domain.js +++ b/src/api-docs/paths/path-domain.js @@ -207,6 +207,19 @@ export default { } } }, + '/doamin/collaboration': { + get: { + tags: ['Domain'], + description: 'Get domains for collaboration', + security: [{ bearerAuth: [] }], + responses: { + '200': { + description: 'Success', + content: commonArraySchemaContent('Domain') + } + } + } + }, '/domain/history/{id}': { get: { tags: ['Domain'], diff --git a/src/routers/config-strategy.js b/src/routers/config-strategy.js index a294f31..b9ee818 100644 --- a/src/routers/config-strategy.js +++ b/src/routers/config-strategy.js @@ -47,6 +47,7 @@ router.get('/configstrategy', auth, [ elements => elements.activated.get(req.query.env ? req.query.env : EnvType.DEFAULT) != undefined); configStrategies = await verifyOwnership(req.admin, configStrategies, config.domain, ActionTypes.READ, RouterTypes.STRATEGY, true); + await Services.populateAdmin(configStrategies); res.send(configStrategies); } catch (e) { diff --git a/src/routers/config.js b/src/routers/config.js index 8502556..c486862 100644 --- a/src/routers/config.js +++ b/src/routers/config.js @@ -53,6 +53,7 @@ router.get('/config', auth, [ let configs = groupConfig.config; configs = await verifyOwnership(req.admin, configs, groupConfig.domain, ActionTypes.READ, RouterTypes.CONFIG, true); + await Services.populateAdmin(configs); if (req.query.fields) { configs = getFields(configs, req.query.fields); diff --git a/src/routers/domain.js b/src/routers/domain.js index 429ec31..6e72dee 100644 --- a/src/routers/domain.js +++ b/src/routers/domain.js @@ -35,9 +35,26 @@ router.get('/domain', auth, async (req, res) => { sort: sortBy(req.query) } }); + + req.admin.domain.forEach(domain => domain.admin = { + _id: req.admin._id, + name: req.admin.name + }); + res.send(req.admin.domain); }); +router.get('/domain/collaboration', auth, async (req, res) => { + await req.admin.populate({ path: 'team_list' }); + + const domains = []; + for (const adm of req.admin.team_list) { + domains.push(await Services.getDomainById(adm.domain.toString(), false, true)); + } + + res.send(domains); +}); + router.get('/domain/:id', auth, [ check('id').isMongoId() ], validate, async (req, res) => { diff --git a/src/routers/group-config.js b/src/routers/group-config.js index 3fc6ef4..d0fb3a8 100644 --- a/src/routers/group-config.js +++ b/src/routers/group-config.js @@ -40,6 +40,7 @@ router.get('/groupconfig', auth, [ let groups = domain.groupConfig; groups = await verifyOwnership(req.admin, groups, domain._id, ActionTypes.READ, RouterTypes.GROUP, true); + await Services.populateAdmin(groups); res.send(groups); } catch (e) { diff --git a/src/services/config-strategy.js b/src/services/config-strategy.js index 6248745..0cfdc9a 100644 --- a/src/services/config-strategy.js +++ b/src/services/config-strategy.js @@ -43,6 +43,12 @@ export async function getStrategies(where, lean = false) { return query.exec(); } +export async function populateAdmin(configStrategies) { + for (const configStrategy of configStrategies) { + await configStrategy.populate({ path: 'admin', select: 'name' }); + } +} + export async function createStrategy(args, admin) { const config = await getConfigById(args.config); diff --git a/src/services/config.js b/src/services/config.js index 3e884cc..610fc4f 100644 --- a/src/services/config.js +++ b/src/services/config.js @@ -55,6 +55,12 @@ export async function getTotalConfigsByDomainId(domain) { return Config.find({ domain }).countDocuments(); } +export async function populateAdmin(configs) { + for (const config of configs) { + await config.populate({ path: 'admin', select: 'name' }); + } +} + export async function createConfig(args, admin) { // validates account plan permissions const group = await getGroupConfigById(args.group); diff --git a/src/services/group-config.js b/src/services/group-config.js index 0091373..f9799f3 100644 --- a/src/services/group-config.js +++ b/src/services/group-config.js @@ -47,6 +47,12 @@ export async function getTotalGroupsByDomainId(domain) { return GroupConfig.find({ domain }).countDocuments(); } +export async function populateAdmin(groups) { + for (const group of groups) { + await group.populate({ path: 'admin', select: 'name' }); + } +} + export async function createGroup(args, admin) { let groupconfig = new GroupConfig({ ...args, diff --git a/tests/config-strategy.test.js b/tests/config-strategy.test.js index f7ebf97..4750fe0 100644 --- a/tests/config-strategy.test.js +++ b/tests/config-strategy.test.js @@ -325,6 +325,7 @@ describe('Testing reading strategies #1', () => { expect(response.body[0].operation).toEqual(configStrategyDocument.operation); expect(String(response.body[0].owner)).toEqual(String(configStrategyDocument.owner)); expect(response.body[0].activated[EnvType.DEFAULT]).toEqual(configStrategyDocument.activated.get(EnvType.DEFAULT)); + expect(response.body[0].admin.name).toEqual(adminMasterAccount.name); // Adding new Config Strategy response = await request(app) diff --git a/tests/config.test.js b/tests/config.test.js index 12d25ae..ed5d4c1 100644 --- a/tests/config.test.js +++ b/tests/config.test.js @@ -98,6 +98,7 @@ describe('Testing fetch configuration info', () => { expect(response.body[0].key).toEqual(config1Document.key); expect(String(response.body[0].owner)).toEqual(String(config1Document.owner)); expect(response.body[0].activated[EnvType.DEFAULT]).toEqual(config1Document.activated.get(EnvType.DEFAULT)); + expect(response.body[0].admin.name).toEqual(adminMasterAccount.name); }); test('CONFIG_SUITE - Should get Config information - only fields (key, activated.default)', async () => { diff --git a/tests/domain.test.js b/tests/domain.test.js index d38b17d..2a3f214 100644 --- a/tests/domain.test.js +++ b/tests/domain.test.js @@ -22,7 +22,8 @@ import { configStrategyId, environment1Id, adminAccountId, - component1Id + component1Id, + teamId } from './fixtures/db_api'; import Component from '../src/models/component'; @@ -88,7 +89,7 @@ describe('Testing fetch Domain info', () => { expect(String(response.body[0]._id)).toEqual(String(domainDocument._id)); expect(response.body[0].name).toEqual(domainDocument.name); expect(String(response.body[0].owner)).toEqual(String(domainDocument.owner)); - expect(response.body[0].token).toEqual(domainDocument.token); + expect(response.body[0].admin.name).toBe(adminMasterAccount.name); // Adding new Domain response = await request(app) @@ -118,6 +119,25 @@ describe('Testing fetch Domain info', () => { expect(response.body.length).toEqual(1); }); + test('DOMAIN_SUITE - Should read domains in which a user is collaborating', async () => { + await request(app) + .patch('/team/member/add/' + teamId) + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send({ + member: adminMasterAccountId + }).expect(200); + + let response = await request(app) + .get('/domain/collaboration') + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send() + .expect(200); + + expect(response.body.length).toEqual(1); + expect(response.body[0].name).toEqual(domainDocument.name); + expect(response.body[0].admin.name).toEqual(adminMasterAccount.name); + }); + test('DOMAIN_SUITE - Should get Domain information by Id', async () => { let response = await request(app) .get('/domain/' + domainId) diff --git a/tests/group-config.test.js b/tests/group-config.test.js index 6487aca..65b3a49 100644 --- a/tests/group-config.test.js +++ b/tests/group-config.test.js @@ -98,6 +98,7 @@ describe('Testing fetch Group info', () => { expect(response.body[0].name).toEqual(groupConfigDocument.name); expect(String(response.body[0].owner)).toEqual(String(groupConfigDocument.owner)); expect(response.body[0].activated[EnvType.DEFAULT]).toEqual(groupConfigDocument.activated.get(EnvType.DEFAULT)); + expect(response.body[0].admin.name).toBe(adminMasterAccount.name); // Adding new Group Config response = await request(app)