diff --git a/__tests__/fixtures/discord/channels.fixture.ts b/__tests__/fixtures/discord/channels.fixture.ts index d0250d93..1011a41e 100644 --- a/__tests__/fixtures/discord/channels.fixture.ts +++ b/__tests__/fixtures/discord/channels.fixture.ts @@ -14,6 +14,7 @@ export const discordChannel1: IChannel = { }, ], deletedAt: null, + type: 0 }; export const discordChannel2: IChannel = { @@ -35,6 +36,8 @@ export const discordChannel2: IChannel = { }, ], deletedAt: null, + type: 0 + }; export const discordChannel3: IChannel = { @@ -56,6 +59,7 @@ export const discordChannel3: IChannel = { }, ], deletedAt: null, + type: 0 }; export const discordChannel4: IChannel = { @@ -63,6 +67,7 @@ export const discordChannel4: IChannel = { name: 'Channel 4', parentId: null, deletedAt: null, + type: 0 }; export const discordChannel5: IChannel = { @@ -70,6 +75,7 @@ export const discordChannel5: IChannel = { name: 'Channel 5', parentId: '987654321098765432', deletedAt: new Date(), + type: 0 }; export const insertChannels = async function (channels: Array, connection: Connection) { diff --git a/__tests__/fixtures/discord/guildMember.fixture.ts b/__tests__/fixtures/discord/guildMember.fixture.ts index ff3c28c2..2dca1f9c 100644 --- a/__tests__/fixtures/discord/guildMember.fixture.ts +++ b/__tests__/fixtures/discord/guildMember.fixture.ts @@ -49,7 +49,7 @@ export const discordGuildMember4 = { }; export const discordGuildMember5 = { - discordId: '444444444', + discordId: '66666666', username: 'User4', avatar: 'AvatarLink', roles: ['652345789987654321'], diff --git a/__tests__/integration/memberActivity.test.ts b/__tests__/integration/memberActivity.test.ts index 061d42a1..59346483 100644 --- a/__tests__/integration/memberActivity.test.ts +++ b/__tests__/integration/memberActivity.test.ts @@ -1,7 +1,7 @@ import request from 'supertest'; import httpStatus from 'http-status'; import app from '../../src/app'; -import setupTestDB from '../utils/setupTestDB'; +import setupTestDB, { cleanUpTenantDatabases } from '../utils/setupTestDB'; import { userOne, insertUsers } from '../fixtures/user.fixture'; import { userOneAccessToken } from '../fixtures/token.fixture'; import { @@ -30,16 +30,8 @@ import { Connection } from 'mongoose'; setupTestDB(); describe('member-activity routes', () => { - let connection: Connection; - beforeAll(async () => { - connection = await DatabaseManager.getInstance().getTenantDb('connection-1'); - }); - afterAll(async () => { - await connection.close(); - }); beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + cleanUpTenantDatabases() userOne.communities = [communityOne._id]; communityOne.users = [userOne._id]; communityOne.platforms = [platformOne._id, platformTwo._id, platformFour._id]; @@ -48,9 +40,9 @@ describe('member-activity routes', () => { platformFour.community = communityOne._id; }); describe('POST /api/v1/member-activity/:platformId/active-members-composition-line-graph', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and active members composition line graph data if req data is ok', async () => { await insertCommunities([communityOne]); @@ -143,9 +135,9 @@ describe('member-activity routes', () => { }); describe('POST /api/v1/member-activity/:platformId/disengaged-members-composition-line-graph', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and disengaged members composition line graph data if req data is ok', async () => { await insertCommunities([communityOne]); @@ -234,9 +226,9 @@ describe('member-activity routes', () => { }); describe('POST /api/v1/member-activity/:platformId/active-members-onboarding-line-graph', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and active members onboarding line graph data if req data is ok', async () => { await insertCommunities([communityOne]); @@ -322,9 +314,9 @@ describe('member-activity routes', () => { }); describe('POST /api/v1/member-activity/:platformId/inactive-members-line-graph', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and inactive members line graph data if req data is ok', async () => { await insertCommunities([communityOne]); @@ -482,9 +474,9 @@ describe('member-activity routes', () => { // }) describe('GET /api/v1/member-activity/:platformId/fragmentation-score', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and fragmentation score if req data is ok', async () => { await insertCommunities([communityOne]); @@ -639,9 +631,9 @@ describe('member-activity routes', () => { }); describe('GET /api/v1/member-activity/:platformId/decentralisation-score', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and decentralisation score if req data is ok', async () => { await insertCommunities([communityOne]); @@ -816,9 +808,9 @@ describe('member-activity routes', () => { }); describe('POST /api/v1/member-activity/:platformId/active-members-composition-table', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and apply the default query options', async () => { await insertCommunities([communityOne]); @@ -1266,9 +1258,9 @@ describe('member-activity routes', () => { }); describe('POST /api/v1/member-activity/:platformId/active-members-onboarding-table', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and apply the default query options', async () => { await insertUsers([userOne]); @@ -1719,9 +1711,9 @@ describe('member-activity routes', () => { }); describe('POST /api/v1/member-activity/:platformId/disengaged-members-composition-table', () => { + let connection: Connection; beforeEach(async () => { - await connection.collection('guildmembers').deleteMany({}); - await connection.collection('roles').deleteMany({}); + connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id); }); test('should return 200 and apply the default query options', async () => { await insertUsers([userOne]); diff --git a/__tests__/utils/setupTestDB.ts b/__tests__/utils/setupTestDB.ts index d84779a4..64364a54 100644 --- a/__tests__/utils/setupTestDB.ts +++ b/__tests__/utils/setupTestDB.ts @@ -2,6 +2,20 @@ import mongoose from 'mongoose'; import config from '../../src/config'; import RabbitMQ, { MBConnection, Queue } from '@togethercrew.dev/tc-messagebroker'; import Redis from 'ioredis'; +import { platformOne, platformTwo, platformThree, platformFour } from '../fixtures/platform.fixture'; +import { DatabaseManager } from '@togethercrew.dev/db'; + +export const cleanUpTenantDatabases = async () => { + const tenantIds = [platformOne.metadata?.id, platformTwo.metadata?.id, platformThree.metadata?.id, platformFour.metadata?.id]; + + for (const tenantId of tenantIds) { + const connection = await DatabaseManager.getInstance().getTenantDb(tenantId); + await Promise.all( + Object.values(connection.collections).map(async (collection) => collection.deleteMany({})), + ); + } +}; + const setupTestDB = () => { beforeAll(async () => { mongoose.set('strictQuery', false); @@ -11,9 +25,12 @@ const setupTestDB = () => { }); beforeEach(async () => { - await Promise.all( - Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), - ); + await Promise.all([ + // Clean up the main database + ...Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), + // Clean up tenant databases + cleanUpTenantDatabases(), + ]); }); afterAll(async () => { diff --git a/src/controllers/platform.controller.ts b/src/controllers/platform.controller.ts index 4a5b4c33..a924047d 100644 --- a/src/controllers/platform.controller.ts +++ b/src/controllers/platform.controller.ts @@ -218,8 +218,8 @@ const requestAccess = catchAsync(async function (req: ISessionRequest, res: Resp discord.scopes.connectGuild, permissionsValueNumber, state, - id, // Pass the Guild ID for which access is requested - true, // Assuming you want to disable guild selection + id, + true, ); res.redirect(discordUrl); }