Skip to content

Commit

Permalink
format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Apr 10, 2024
1 parent 32721d3 commit bc9e55d
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 125 deletions.
140 changes: 69 additions & 71 deletions __tests__/unit/models/module.model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
// },
// },
// ]);
// }
// });
// });
});
});
24 changes: 12 additions & 12 deletions __tests__/utils/setupTestDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
30 changes: 15 additions & 15 deletions src/interfaces/Module.interface.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>; // dynamic object since structure can change
}>;
}
name: 'hivemind';
community: Types.ObjectId;
options?: {
platforms: Array<{
platform: Types.ObjectId;
metadata?: Record<string, any>; // dynamic object since structure can change
}>;
};
}
export interface IModuleUpdateBody {
options?: {
platforms: Array<{
platform: Types.ObjectId;
metadata?: Record<string, any>; // dynamic object since structure can change
}>;
}
options?: {
platforms: Array<{
platform: Types.ObjectId;
metadata?: Record<string, any>; // dynamic object since structure can change
}>;
};
}

export interface ModuleModel extends Model<IModule> {
paginate: (filter: object, options: object) => any;
paginate: (filter: object, options: object) => any;
}
2 changes: 1 addition & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export * from './Discord.interface';
export * from './Community.interface';
export * from './Platfrom.interface';
export * from './Announcement.interface';
export * from './Module.interface';
export * from './Module.interface';
15 changes: 14 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
44 changes: 23 additions & 21 deletions src/models/schemas/Module.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@ import { Schema } from 'mongoose';
import { toJSON, paginate } from './plugins';
import { type IModule, type ModuleModel } from '../../interfaces';

const moduleSchema = new Schema<IModule, ModuleModel>({
const moduleSchema = new Schema<IModule, ModuleModel>(
{
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);
Expand Down
6 changes: 3 additions & 3 deletions src/models/schemas/Platform.schema.ts
Original file line number Diff line number Diff line change
@@ -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<IPlatform, PlatformModel>(
{
Expand Down Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion src/models/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export {
platformSchema,
announcementSchema,
announcementEmitter,
moduleSchema
moduleSchema,
};

0 comments on commit bc9e55d

Please sign in to comment.