From 61cc83e3e4fbd632bad8efb3416cbdaad6e43a62 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Wed, 13 Mar 2024 12:08:25 +0400 Subject: [PATCH 1/9] add Role interface to Community Interface --- src/interfaces/Community.interface.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/interfaces/Community.interface.ts b/src/interfaces/Community.interface.ts index 68eb85e..2b229aa 100644 --- a/src/interfaces/Community.interface.ts +++ b/src/interfaces/Community.interface.ts @@ -1,11 +1,21 @@ import { type Model, type Types } from 'mongoose'; +export interface ICommunityRoles { + roleType: 'view' | 'admin'; + source: { + platform: 'discord'; + identifierType: 'member' | 'role'; + identifierValues: string[]; + platformId: Types.ObjectId; + } +} export interface ICommunity { name: string; avatarURL?: string; users: Types.ObjectId[]; platforms?: Types.ObjectId[]; tcaAt?: Date; + roles?: ICommunityRoles[] } export interface ICommunityUpdateBody { @@ -14,6 +24,7 @@ export interface ICommunityUpdateBody { users?: Types.ObjectId[]; platforms?: Types.ObjectId[]; tcaAt?: Date; + roles?: ICommunityRoles[] } export interface CommunityModel extends Model { From 24631a43cfe5dee49a534852d07cd4a729b55c6f Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Wed, 13 Mar 2024 12:45:19 +0400 Subject: [PATCH 2/9] add Roles field to Community schema --- src/models/schemas/Community.schema.ts | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/models/schemas/Community.schema.ts b/src/models/schemas/Community.schema.ts index 029cacf..244571b 100644 --- a/src/models/schemas/Community.schema.ts +++ b/src/models/schemas/Community.schema.ts @@ -29,8 +29,35 @@ const communitySchema = new Schema( tcaAt: { type: Date, }, + roles: [{ + roleType: { + type: String, + enum: ['view', 'admin'], + required: true, + }, + source: { + platform: { + type: String, + enum: ['discord'], + required: true, + }, + identifierType: { + type: String, + enum: ['member', 'role'], + required: true, + }, + identifierValues: [{ + type: String, + required: true, + }], + platformId: { + type: Schema.Types.ObjectId, + ref: 'Platform', + required: true, + }, + }, + }] }, - { timestamps: true }, ); From 8c0317cad1971da3be5958e08b7345bcdbebb88a Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Wed, 13 Mar 2024 13:00:40 +0400 Subject: [PATCH 3/9] update the Readme and package version --- README.md | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d2b1a11..68b1bd3 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Community { users?: [Types.ObjectId], platforms?: [Types.ObjectId], tcaAt?: Date; + roles?: ICommunityRoles[]; } ``` diff --git a/package.json b/package.json index 0df7cd2..5cfc002 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@togethercrew.dev/db", - "version": "3.0.32", + "version": "3.0.33", "description": "All interactions with DB", "main": "./dist/index.js", "types": "./dist/index.d.ts", From cdc5650115c6ee07978f8a52f614604363f4d3c3 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:50:03 +0400 Subject: [PATCH 4/9] add middleware tests --- __tests__/unit/models/community.mode.test.ts | 41 +++++++------- __tests__/unit/models/platform.model.test.ts | 59 ++++++++++++++------ __tests__/unit/models/user.model.test.ts | 27 +++++---- __tests__/utils/setupTestDB.ts | 34 +++++------ package.json | 2 +- src/config/index.ts | 6 +- src/models/schemas/Platform.schema.ts | 34 ++++++++++- 7 files changed, 128 insertions(+), 75 deletions(-) diff --git a/__tests__/unit/models/community.mode.test.ts b/__tests__/unit/models/community.mode.test.ts index d5dde25..0de8e30 100644 --- a/__tests__/unit/models/community.mode.test.ts +++ b/__tests__/unit/models/community.mode.test.ts @@ -1,9 +1,9 @@ import { Community, User, Platform } from '../../../src/models'; import { ICommunity } from '../../../src/interfaces'; import { Types } from 'mongoose'; -// import setupTestDB from '../../utils/setupTestDB'; +import setupTestDB from '../../utils/setupTestDB'; -// setupTestDB(); +setupTestDB(); describe('Community model', () => { describe('Community validation', () => { @@ -20,31 +20,30 @@ describe('Community model', () => { test('should correctly validate a valid community', async () => { await expect(new Community(community).validate()).resolves.toBeUndefined(); }); + describe('Middlewares', () => { - // describe('Cascade deletes', () => { + test('Pre Remove: should clean up when community is deleted', async () => { + const user = new User({ discordId: 'discordId' }); + await user.save(); - // test('should clean up when community is deleted', async () => { - // const user = new User({ discordId: 'discordId' }); - // await user.save(); + const community = new Community({ users: [user._id], name: 'community' }); + await community.save(); + user.communities?.push(community._id) - // const community = new Community({ users: [user._id], name: 'community' }); - // await community.save(); - // user.communities?.push(community._id) + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); - // const platform = new Platform({ name: 'platform', community: community._id }); - // await platform.save(); + community.platforms?.push(platform._id); + await community.save(); - // community.platforms?.push(platform._id); - // await community.save(); + await community.remove(); - // await community.remove(); + const userDoc = await User.findById(user._id); + expect(userDoc?.communities).not.toContain(community._id); - // const userDoc = await User.findById(user._id); - // expect(userDoc?.communities).not.toContain(community._id); - - // const platformDoc = await Platform.findById(platform._id); - // expect(platformDoc).toBe(null); - // }); - // }); + const platformDoc = await Platform.findById(platform._id); + expect(platformDoc).toBe(null); + }); + }); }); }); diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index 1a8a160..beb543d 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -1,9 +1,9 @@ -import { Platform, Community } from '../../../src/models'; +import { Platform, Community ,User} from '../../../src/models'; import { IPlatform } from '../../../src/interfaces'; import { Types } from 'mongoose'; -// import setupTestDB from '../../utils/setupTestDB'; +import setupTestDB from '../../utils/setupTestDB'; -// setupTestDB(); +setupTestDB(); describe('Platform model', () => { describe('Platform validation', () => { @@ -24,27 +24,50 @@ describe('Platform model', () => { await expect(new Platform(platform).validate()).resolves.toBeUndefined(); }); - // describe('Cascade deletes', () => { + describe('Middlewares', () => { - // test('should clean up when community is deleted', async () => { - // const community = new Community({ users: [new Types.ObjectId()], name: 'community' }); - // await community.save(); + test('Pre Remove: should clean up when community is deleted', async () => { + const community = new Community({ users: [new Types.ObjectId()], name: 'community' }); + await community.save(); - // const platform = new Platform({ name: 'platform', community: community._id }); - // await platform.save(); + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); - // community.platforms?.push(platform._id); - // await community.save(); + community.platforms?.push(platform._id); + await community.save(); - // await platform.remove(); + await platform.remove(); - // const communityDoc = await Community.findById(community._id); + const communityDoc = await Community.findById(community._id); - // expect(communityDoc?.platforms).not.toContain(platform._id); + expect(communityDoc?.platforms).not.toContain(platform._id); - // const platformDoc = await Platform.findById(platform._id); - // expect(platformDoc).toBe(null); - // }); - // }); + const platformDoc = await Platform.findById(platform._id); + expect(platformDoc).toBe(null); + }); + + test('Post Save: should add platformId to the community and admin role for the creator of community', async () => { + const user = new User({ discordId: 'discordId' }); + await user.save(); + + const community = new Community({ users: [user._id], name: 'community' }); + await community.save(); + user.communities?.push(community._id) + + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); + + community.platforms?.push(platform._id); + await community.save(); + + await community.remove(); + + const userDoc = await User.findById(user._id); + expect(userDoc?.communities).not.toContain(community._id); + + const platformDoc = await Platform.findById(platform._id); + expect(platformDoc).toBe(null); + }); + }); }); }); diff --git a/__tests__/unit/models/user.model.test.ts b/__tests__/unit/models/user.model.test.ts index d450f46..3df43ac 100644 --- a/__tests__/unit/models/user.model.test.ts +++ b/__tests__/unit/models/user.model.test.ts @@ -1,9 +1,9 @@ import { User, Community } from '../../../src/models'; import { IUser } from '../../../src/interfaces'; import { Types } from 'mongoose'; -// import setupTestDB from '../../utils/setupTestDB'; +import setupTestDB from '../../utils/setupTestDB'; -// setupTestDB(); +setupTestDB(); describe('User model', () => { describe('User validation', () => { @@ -22,20 +22,19 @@ describe('User model', () => { }); }); - // describe('Cascade deletes', () => { + describe('Middlewares', () => { + test('Pre Remove: should remove user reference from community when user is deleted', async () => { + const user = new User({ discordId: 'discordId' }); + await user.save(); - // test('should remove user reference from community when user is deleted', async () => { - // const user = new User({ discordId: 'discordId' }); - // await user.save(); + const community = new Community({ users: [user._id], name: 'community' }); + await community.save(); - // const community = new Community({ users: [user._id], name: 'community' }); - // await community.save(); + await user.remove(); - // await user.remove(); + const communityDoc = await Community.findById(community._id); + expect(communityDoc?.users).not.toContain(user._id); - // const communityDoc = await Community.findById(community._id); - // expect(communityDoc?.users).not.toContain(user._id); - - // }); - // }); + }); + }); }); diff --git a/__tests__/utils/setupTestDB.ts b/__tests__/utils/setupTestDB.ts index 19f8197..cd498c5 100644 --- a/__tests__/utils/setupTestDB.ts +++ b/__tests__/utils/setupTestDB.ts @@ -1,21 +1,21 @@ -// import mongoose from 'mongoose'; -// import config from '../../src/config'; +import mongoose from 'mongoose'; +import config from '../../src/config'; -// const setupTestDB = () => { -// beforeAll(async () => { -// mongoose.set('strictQuery', false); -// await mongoose.connect(config.mongoose.serverURL); -// }); +const setupTestDB = () => { + beforeAll(async () => { + mongoose.set('strictQuery', false); + await mongoose.connect(config.mongoose.serverURL); + }); -// beforeEach(async () => { -// await Promise.all( -// Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), -// ); -// }); + beforeEach(async () => { + await Promise.all( + Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), + ); + }); -// afterAll(async () => { -// await mongoose.disconnect(); -// }); -// }; + afterAll(async () => { + await mongoose.disconnect(); + }); +}; -// export default setupTestDB; +export default setupTestDB; diff --git a/package.json b/package.json index 5cfc002..9c04c8f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "tsc", "start": "node ./dist/index.js", "dev": "nodemon ./src/index.ts", - "test": "jest --detectOpenHandles", + "test": "env-cmd -f ./src/config/test.env jest --runInBand --detectOpenHandles", "format": "prettier --write \"src/**/*.ts\" \"__tests__/**/*.ts\" \"*.ts\" ", "prepublishOnly": "npm test", "version": "npm run format && git add -A src", diff --git a/src/config/index.ts b/src/config/index.ts index c87cb55..340ff8b 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -19,8 +19,8 @@ if (error != null) { export default { mongoose: { - serverURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}/${envVars.DB_NAME}`, - botURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}`, - dbURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}`, + serverURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}/${envVars.DB_NAME}?authSource=admin`, + botURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}?authSource=admin`, + dbURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}?authSource=admin`, }, }; diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index 60d32fa..9f12142 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -1,7 +1,7 @@ import { Schema, type Document, Types } from 'mongoose'; import { toJSON, paginate } from './plugins'; import { type IPlatform, type PlatformModel } from '../../interfaces'; -import { Announcement, Community } from '../index'; +import { Announcement, Community, Platform, User } from '../index'; const platformSchema = new Schema( { @@ -75,4 +75,36 @@ platformSchema.pre('remove', async function (this: Document) { await announcementDeletion(platformId); }); +platformSchema.post('save', async function () { + const platformId = this._id; + const platform = await Platform.findById(platformId); + if (platform !== null) { + const community = await Community.findById(platform.community); + if (community !== null) { + const user = await User.findById(community?.users[0]); + if (user !== null) { + await Community.updateOne( + { _id: community._id }, + { + $addToSet: { + platforms: platform._id, + roles: { + $each: [{ + roleType: 'admin', + source: { + platform: 'discord', + identifierType: 'member', + identifierValues: [user.discordId], + platformId: platform._id, + } + }] + } + } + } + ); + } + } + } +}); + export default platformSchema; From 9a3dcf84c2a2d29d3d4bb1dafc80e2daa2ecbed9 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:57:33 +0400 Subject: [PATCH 5/9] update docker files to run db tests --- Dockerfile | 4 ++-- docker-compose.test.yml | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 132338a..784c2a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,11 @@ COPY . . RUN npm ci FROM base AS test -CMD [ "npx", "jest", "--coverage" ] +CMD [ "npx", "jest", "--runInBand", "--coverage" ] FROM base AS build RUN npm run build FROM build AS prod RUN npm ci --omit=dev -CMD ["npm", "run", "start"] \ No newline at end of file +CMD ["npm", "run", "start"] diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 5749631..ce4e14c 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -1,5 +1,4 @@ ---- -version: '3.9' +version: "3.9" services: app: @@ -7,5 +6,28 @@ services: context: . target: test dockerfile: Dockerfile + environment: + - DB_HOST=mongo + - DB_PORT=27017 + - DB_USER=root + - DB_PASSWORD=pass + - DB_NAME=RnDAO volumes: - ./coverage:/project/coverage + depends_on: + mongo: + condition: service_healthy + mongo: + image: "mongo:5.0.10" + environment: + - MONGO_INITDB_DATABASE=RnDAO + - MONGO_INITDB_ROOT_USERNAME=root + - MONGO_INITDB_ROOT_PASSWORD=pass + healthcheck: + test: echo 'db.stats().ok' | mongosh localhost:27017/test --quiet + interval: 60s + timeout: 10s + retries: 2 + start_period: 40s + + From c75413a0d7b963732845e2c982d8a55823a08206 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:30:54 +0400 Subject: [PATCH 6/9] update platform tests --- __tests__/unit/models/platform.model.test.ts | 51 +++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index beb543d..fb2c091 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -1,4 +1,4 @@ -import { Platform, Community ,User} from '../../../src/models'; +import { Platform, Community, User } from '../../../src/models'; import { IPlatform } from '../../../src/interfaces'; import { Types } from 'mongoose'; import setupTestDB from '../../utils/setupTestDB'; @@ -26,22 +26,23 @@ describe('Platform model', () => { describe('Middlewares', () => { - test('Pre Remove: should clean up when community is deleted', async () => { - const community = new Community({ users: [new Types.ObjectId()], name: 'community' }); + test('Pre Remove: should clean up when platform is deleted', async () => { + const user = new User({ discordId: 'discordId' }); + await user.save(); + + const community = new Community({ users: [user._id], name: 'community' }); await community.save(); const platform = new Platform({ name: 'platform', community: community._id }); await platform.save(); - - community.platforms?.push(platform._id); - await community.save(); - + let communityDoc = await Community.findById(community.id); + if (communityDoc?.platforms) { + const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; + expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); + } await platform.remove(); - - const communityDoc = await Community.findById(community._id); - - expect(communityDoc?.platforms).not.toContain(platform._id); - + communityDoc = await Community.findById(community.id); + expect(communityDoc?.platforms).toEqual([]); const platformDoc = await Platform.findById(platform._id); expect(platformDoc).toBe(null); }); @@ -56,17 +57,21 @@ describe('Platform model', () => { const platform = new Platform({ name: 'platform', community: community._id }); await platform.save(); - - community.platforms?.push(platform._id); - await community.save(); - - await community.remove(); - - const userDoc = await User.findById(user._id); - expect(userDoc?.communities).not.toContain(community._id); - - const platformDoc = await Platform.findById(platform._id); - expect(platformDoc).toBe(null); + const communityDoc = await Community.findById(community.id); + if (communityDoc?.platforms && communityDoc?.roles) { + const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; + expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); + expect(JSON.parse(JSON.stringify(communityDoc.roles))).toEqual([{ + _id: expect.anything(), + roleType: 'admin', + source: { + platform: 'discord', + identifierType: 'member', + identifierValues: [user.discordId], + platformId: platform._id.toHexString(), + } + }]); + } }); }); }); From 3f45c86536d8084dba9aa9c238e540dbdb486013 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:31:47 +0400 Subject: [PATCH 7/9] update community tests --- __tests__/unit/models/community.mode.test.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/__tests__/unit/models/community.mode.test.ts b/__tests__/unit/models/community.mode.test.ts index 0de8e30..2036c19 100644 --- a/__tests__/unit/models/community.mode.test.ts +++ b/__tests__/unit/models/community.mode.test.ts @@ -33,9 +33,6 @@ describe('Community model', () => { const platform = new Platform({ name: 'platform', community: community._id }); await platform.save(); - community.platforms?.push(platform._id); - await community.save(); - await community.remove(); const userDoc = await User.findById(user._id); From b128e61a291667ddd40da9742cd6ebe4a99aec0f Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:40:45 +0400 Subject: [PATCH 8/9] add autoDelete roles if platform deleted + added tests --- __tests__/unit/models/platform.model.test.ts | 2 ++ src/models/schemas/Platform.schema.ts | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index fb2c091..b8163fa 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -43,6 +43,8 @@ describe('Platform model', () => { await platform.remove(); communityDoc = await Community.findById(community.id); expect(communityDoc?.platforms).toEqual([]); + expect(communityDoc?.roles).toEqual([]); + const platformDoc = await Platform.findById(platform._id); expect(platformDoc).toBe(null); }); diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index 9f12142..7e158e6 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -73,6 +73,11 @@ platformSchema.pre('remove', async function (this: Document) { const platformId = this._id; await Community.updateOne({ platforms: platformId }, { $pull: { platforms: platformId } }); await announcementDeletion(platformId); + await Community.updateMany( + {}, + { $pull: { roles: { 'source.platformId': platformId } } }, + { multi: true } + ) }); platformSchema.post('save', async function () { From e070968c44a27e6b1949ea314ba93792fcf81b7b Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:40:58 +0400 Subject: [PATCH 9/9] format the code --- __tests__/unit/models/community.mode.test.ts | 3 +- __tests__/unit/models/platform.model.test.ts | 25 +++++----- __tests__/unit/models/user.model.test.ts | 1 - __tests__/utils/setupTestDB.ts | 24 +++++----- src/interfaces/Community.interface.ts | 6 +-- src/models/schemas/Community.schema.ts | 50 +++++++++++--------- src/models/schemas/Platform.schema.ts | 32 ++++++------- 7 files changed, 71 insertions(+), 70 deletions(-) diff --git a/__tests__/unit/models/community.mode.test.ts b/__tests__/unit/models/community.mode.test.ts index 2036c19..ef7806f 100644 --- a/__tests__/unit/models/community.mode.test.ts +++ b/__tests__/unit/models/community.mode.test.ts @@ -21,14 +21,13 @@ describe('Community model', () => { await expect(new Community(community).validate()).resolves.toBeUndefined(); }); describe('Middlewares', () => { - test('Pre Remove: should clean up when community is deleted', async () => { const user = new User({ discordId: 'discordId' }); await user.save(); const community = new Community({ users: [user._id], name: 'community' }); await community.save(); - user.communities?.push(community._id) + user.communities?.push(community._id); const platform = new Platform({ name: 'platform', community: community._id }); await platform.save(); diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index b8163fa..0ded583 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -25,7 +25,6 @@ describe('Platform model', () => { }); describe('Middlewares', () => { - test('Pre Remove: should clean up when platform is deleted', async () => { const user = new User({ discordId: 'discordId' }); await user.save(); @@ -55,7 +54,7 @@ describe('Platform model', () => { const community = new Community({ users: [user._id], name: 'community' }); await community.save(); - user.communities?.push(community._id) + user.communities?.push(community._id); const platform = new Platform({ name: 'platform', community: community._id }); await platform.save(); @@ -63,16 +62,18 @@ describe('Platform model', () => { if (communityDoc?.platforms && communityDoc?.roles) { const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); - expect(JSON.parse(JSON.stringify(communityDoc.roles))).toEqual([{ - _id: expect.anything(), - roleType: 'admin', - source: { - platform: 'discord', - identifierType: 'member', - identifierValues: [user.discordId], - platformId: platform._id.toHexString(), - } - }]); + expect(JSON.parse(JSON.stringify(communityDoc.roles))).toEqual([ + { + _id: expect.anything(), + roleType: 'admin', + source: { + platform: 'discord', + identifierType: 'member', + identifierValues: [user.discordId], + platformId: platform._id.toHexString(), + }, + }, + ]); } }); }); diff --git a/__tests__/unit/models/user.model.test.ts b/__tests__/unit/models/user.model.test.ts index 3df43ac..0b97d4d 100644 --- a/__tests__/unit/models/user.model.test.ts +++ b/__tests__/unit/models/user.model.test.ts @@ -34,7 +34,6 @@ describe('User model', () => { const communityDoc = await Community.findById(community._id); expect(communityDoc?.users).not.toContain(user._id); - }); }); }); diff --git a/__tests__/utils/setupTestDB.ts b/__tests__/utils/setupTestDB.ts index cd498c5..840c340 100644 --- a/__tests__/utils/setupTestDB.ts +++ b/__tests__/utils/setupTestDB.ts @@ -2,20 +2,20 @@ import mongoose from 'mongoose'; import config from '../../src/config'; const setupTestDB = () => { - beforeAll(async () => { - mongoose.set('strictQuery', false); - await mongoose.connect(config.mongoose.serverURL); - }); + beforeAll(async () => { + mongoose.set('strictQuery', false); + await mongoose.connect(config.mongoose.serverURL); + }); - beforeEach(async () => { - await Promise.all( - Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), - ); - }); + beforeEach(async () => { + await Promise.all( + Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), + ); + }); - afterAll(async () => { - await mongoose.disconnect(); - }); + afterAll(async () => { + await mongoose.disconnect(); + }); }; export default setupTestDB; diff --git a/src/interfaces/Community.interface.ts b/src/interfaces/Community.interface.ts index 2b229aa..52b37dd 100644 --- a/src/interfaces/Community.interface.ts +++ b/src/interfaces/Community.interface.ts @@ -7,7 +7,7 @@ export interface ICommunityRoles { identifierType: 'member' | 'role'; identifierValues: string[]; platformId: Types.ObjectId; - } + }; } export interface ICommunity { name: string; @@ -15,7 +15,7 @@ export interface ICommunity { users: Types.ObjectId[]; platforms?: Types.ObjectId[]; tcaAt?: Date; - roles?: ICommunityRoles[] + roles?: ICommunityRoles[]; } export interface ICommunityUpdateBody { @@ -24,7 +24,7 @@ export interface ICommunityUpdateBody { users?: Types.ObjectId[]; platforms?: Types.ObjectId[]; tcaAt?: Date; - roles?: ICommunityRoles[] + roles?: ICommunityRoles[]; } export interface CommunityModel extends Model { diff --git a/src/models/schemas/Community.schema.ts b/src/models/schemas/Community.schema.ts index 244571b..21753dc 100644 --- a/src/models/schemas/Community.schema.ts +++ b/src/models/schemas/Community.schema.ts @@ -29,34 +29,38 @@ const communitySchema = new Schema( tcaAt: { type: Date, }, - roles: [{ - roleType: { - type: String, - enum: ['view', 'admin'], - required: true, - }, - source: { - platform: { - type: String, - enum: ['discord'], - required: true, - }, - identifierType: { + roles: [ + { + roleType: { type: String, - enum: ['member', 'role'], + enum: ['view', 'admin'], required: true, }, - identifierValues: [{ - type: String, - required: true, - }], - platformId: { - type: Schema.Types.ObjectId, - ref: 'Platform', - required: true, + source: { + platform: { + type: String, + enum: ['discord'], + required: true, + }, + identifierType: { + type: String, + enum: ['member', 'role'], + required: true, + }, + identifierValues: [ + { + type: String, + required: true, + }, + ], + platformId: { + type: Schema.Types.ObjectId, + ref: 'Platform', + required: true, + }, }, }, - }] + ], }, { timestamps: true }, ); diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index 7e158e6..844f800 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -73,11 +73,7 @@ platformSchema.pre('remove', async function (this: Document) { const platformId = this._id; await Community.updateOne({ platforms: platformId }, { $pull: { platforms: platformId } }); await announcementDeletion(platformId); - await Community.updateMany( - {}, - { $pull: { roles: { 'source.platformId': platformId } } }, - { multi: true } - ) + await Community.updateMany({}, { $pull: { roles: { 'source.platformId': platformId } } }, { multi: true }); }); platformSchema.post('save', async function () { @@ -94,18 +90,20 @@ platformSchema.post('save', async function () { $addToSet: { platforms: platform._id, roles: { - $each: [{ - roleType: 'admin', - source: { - platform: 'discord', - identifierType: 'member', - identifierValues: [user.discordId], - platformId: platform._id, - } - }] - } - } - } + $each: [ + { + roleType: 'admin', + source: { + platform: 'discord', + identifierType: 'member', + identifierValues: [user.discordId], + platformId: platform._id, + }, + }, + ], + }, + }, + }, ); } }