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 Mar 14, 2024
1 parent b128e61 commit e070968
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 70 deletions.
3 changes: 1 addition & 2 deletions __tests__/unit/models/community.mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 13 additions & 12 deletions __tests__/unit/models/platform.model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -55,24 +54,26 @@ 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();
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(),
}
}]);
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(),
},
},
]);
}
});
});
Expand Down
1 change: 0 additions & 1 deletion __tests__/unit/models/user.model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('User model', () => {

const communityDoc = await Community.findById(community._id);
expect(communityDoc?.users).not.toContain(user._id);

});
});
});
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;
6 changes: 3 additions & 3 deletions src/interfaces/Community.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export interface ICommunityRoles {
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[]
roles?: ICommunityRoles[];
}

export interface ICommunityUpdateBody {
Expand All @@ -24,7 +24,7 @@ export interface ICommunityUpdateBody {
users?: Types.ObjectId[];
platforms?: Types.ObjectId[];
tcaAt?: Date;
roles?: ICommunityRoles[]
roles?: ICommunityRoles[];
}

export interface CommunityModel extends Model<ICommunity> {
Expand Down
50 changes: 27 additions & 23 deletions src/models/schemas/Community.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,38 @@ const communitySchema = new Schema<ICommunity, CommunityModel>(
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 },
);
Expand Down
32 changes: 15 additions & 17 deletions src/models/schemas/Platform.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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,
},
},
],
},
},
},
);
}
}
Expand Down

0 comments on commit e070968

Please sign in to comment.