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] 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 () {