diff --git a/__tests__/unit/models/module.model.test.ts b/__tests__/unit/models/module.model.test.ts index 78db35e..30efbc8 100644 --- a/__tests__/unit/models/module.model.test.ts +++ b/__tests__/unit/models/module.model.test.ts @@ -6,83 +6,81 @@ import { Types } from 'mongoose'; // setupTestDB(); describe('Module model', () => { - describe('Module validation', () => { - let module: IModule; - beforeEach(() => { - module = { - name: 'hivemind', - community: new Types.ObjectId(), - options: { - platforms: [ - { - - platform: new Types.ObjectId(), - metadata: { - selectedChannels: ['c1', 'c2'], - } - - } - ] - }, - }; - }); + describe('Module validation', () => { + let module: IModule; + beforeEach(() => { + module = { + name: 'hivemind', + community: new Types.ObjectId(), + options: { + platforms: [ + { + platform: new Types.ObjectId(), + metadata: { + selectedChannels: ['c1', 'c2'], + }, + }, + ], + }, + }; + }); - test('should correctly validate a valid module', async () => { - await expect(new Module(module).validate()).resolves.toBeUndefined(); - }); + test('should correctly validate a valid module', async () => { + await expect(new Module(module).validate()).resolves.toBeUndefined(); + }); - // describe('Middlewares', () => { - // test('Pre Remove: should clean up when platform is deleted', async () => { - // const user = new User({ discordId: 'discordId' }); - // await user.save(); + // describe('Middlewares', () => { + // 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 community = new Community({ users: [user._id], name: 'community' }); + // await community.save(); - // const platform = new Platform({ name: 'platform', community: community._id }); - // await platform.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(); - // communityDoc = await Community.findById(community.id); - // expect(communityDoc?.platforms).toEqual([]); - // expect(communityDoc?.roles).toEqual([]); + // const platform = new Platform({ name: 'platform', community: community._id }); + // await platform.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(); + // 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); - // }); + // 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(); + // 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 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 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(), - // }, - // }, - // ]); - // } - // }); - // }); - }); + // const platform = new Platform({ name: 'platform', community: community._id }); + // await platform.save(); + // 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(), + // }, + // }, + // ]); + // } + // }); + // }); + }); }); 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/Module.interface.ts b/src/interfaces/Module.interface.ts index c8623f2..55a22a6 100644 --- a/src/interfaces/Module.interface.ts +++ b/src/interfaces/Module.interface.ts @@ -1,24 +1,24 @@ import { type Model, type Types } from 'mongoose'; export interface IModule { - name: 'hivemind'; - community: Types.ObjectId; - options?: { - platforms: Array<{ - platform: Types.ObjectId; - metadata?: Record; // dynamic object since structure can change - }>; - } + name: 'hivemind'; + community: Types.ObjectId; + options?: { + platforms: Array<{ + platform: Types.ObjectId; + metadata?: Record; // dynamic object since structure can change + }>; + }; } export interface IModuleUpdateBody { - options?: { - platforms: Array<{ - platform: Types.ObjectId; - metadata?: Record; // dynamic object since structure can change - }>; - } + options?: { + platforms: Array<{ + platform: Types.ObjectId; + metadata?: Record; // dynamic object since structure can change + }>; + }; } export interface ModuleModel extends Model { - paginate: (filter: object, options: object) => any; + paginate: (filter: object, options: object) => any; } diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index 652493e..3bfa5fb 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -10,4 +10,4 @@ export * from './Discord.interface'; export * from './Community.interface'; export * from './Platfrom.interface'; export * from './Announcement.interface'; -export * from './Module.interface'; \ No newline at end of file +export * from './Module.interface'; diff --git a/src/models/index.ts b/src/models/index.ts index ae5f370..3d7c110 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -10,4 +10,17 @@ import Community from './Community.model'; import Platform from './Platfrom.model'; import Announcement from './Announcement.model'; import Module from './Module.model'; -export { User, Token, HeatMap, RawInfo, MemberActivity, GuildMember, Channel, Role, Community, Platform, Announcement, Module }; +export { + User, + Token, + HeatMap, + RawInfo, + MemberActivity, + GuildMember, + Channel, + Role, + Community, + Platform, + Announcement, + Module, +}; diff --git a/src/models/schemas/Module.schema.ts b/src/models/schemas/Module.schema.ts index c3b8840..9562226 100644 --- a/src/models/schemas/Module.schema.ts +++ b/src/models/schemas/Module.schema.ts @@ -2,34 +2,36 @@ import { Schema } from 'mongoose'; import { toJSON, paginate } from './plugins'; import { type IModule, type ModuleModel } from '../../interfaces'; -const moduleSchema = new Schema({ +const moduleSchema = new Schema( + { name: { - type: String, - required: true, - enum: ['hivemind'] + type: String, + required: true, + enum: ['hivemind'], }, community: { - type: Schema.Types.ObjectId, - ref: 'Community', - required: true, + type: Schema.Types.ObjectId, + ref: 'Community', + required: true, }, options: { - platforms: [{ - platform: { - type: Schema.Types.ObjectId, - required: true, - ref: 'Platform' - }, - metadata: { - type: Schema.Types.Mixed, - } - }] - } -}, - { timestamps: true } + platforms: [ + { + platform: { + type: Schema.Types.ObjectId, + required: true, + ref: 'Platform', + }, + metadata: { + type: Schema.Types.Mixed, + }, + }, + ], + }, + }, + { timestamps: true }, ); - // Plugins moduleSchema.plugin(toJSON); moduleSchema.plugin(paginate); diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index b7286a1..5f4ee0c 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, Platform, User,Module } from '../index'; +import { Announcement, Community, Platform, User, Module } from '../index'; const platformSchema = new Schema( { @@ -76,8 +76,8 @@ platformSchema.pre('remove', async function (this: Document) { await Community.updateMany({}, { $pull: { roles: { 'source.platformId': platformId } } }, { multi: true }); await Module.updateMany( { 'options.platforms.platformId': platformId }, - { $pull: { 'options.platforms': { platform: platformId } } } -); + { $pull: { 'options.platforms': { platform: platformId } } }, + ); }); platformSchema.pre('save', function () { diff --git a/src/models/schemas/index.ts b/src/models/schemas/index.ts index 8ecc1dd..8bab6bb 100644 --- a/src/models/schemas/index.ts +++ b/src/models/schemas/index.ts @@ -23,5 +23,5 @@ export { platformSchema, announcementSchema, announcementEmitter, - moduleSchema + moduleSchema, };