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] 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, + }, + }, + ], + }, + }, + }, ); } }