From 3ac7b196ad849785f0e77204039ff9448284ae84 Mon Sep 17 00:00:00 2001 From: Mohammad Twin Date: Wed, 3 Jan 2024 15:29:28 +0400 Subject: [PATCH 1/4] feat: deletedAt was added to each option of announcement --- src/interfaces/Announcement.interface.ts | 1 + src/models/schemas/Announcement.schema.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/interfaces/Announcement.interface.ts b/src/interfaces/Announcement.interface.ts index f9922db..a6270a4 100644 --- a/src/interfaces/Announcement.interface.ts +++ b/src/interfaces/Announcement.interface.ts @@ -6,6 +6,7 @@ interface IAnnouncementData { platform: Types.ObjectId; template: string; options: T; + deletedAt?: Date; } export interface IAnnouncement { diff --git a/src/models/schemas/Announcement.schema.ts b/src/models/schemas/Announcement.schema.ts index 30fdcff..9d81a59 100644 --- a/src/models/schemas/Announcement.schema.ts +++ b/src/models/schemas/Announcement.schema.ts @@ -17,6 +17,7 @@ const announcementDataSchema = new Schema( options: { type: Schema.Types.Mixed, }, + deletedAt: Date, }, { _id: false }, ); From 04eb0a8e32a20839025a8c143f7826016c98c5f2 Mon Sep 17 00:00:00 2001 From: Mohammad Twin Date: Wed, 3 Jan 2024 15:32:52 +0400 Subject: [PATCH 2/4] feat: hard deleted managed over the Community and Platform --- src/interfaces/Announcement.interface.ts | 2 ++ src/models/schemas/Announcement.schema.ts | 10 ++++++++++ src/models/schemas/Community.schema.ts | 3 ++- src/models/schemas/Platform.schema.ts | 6 +++++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/interfaces/Announcement.interface.ts b/src/interfaces/Announcement.interface.ts index a6270a4..773db59 100644 --- a/src/interfaces/Announcement.interface.ts +++ b/src/interfaces/Announcement.interface.ts @@ -26,6 +26,8 @@ export interface IAnnouncement { export interface IAnnouncementMethods { softDelete: () => void; + logicalStaffBeforeSoftDelete?: (document: IAnnouncement) => void; + logicalStaffBeforeRemove?: (document: IAnnouncement) => void; } export interface AnnouncementModel extends Model, IAnnouncementMethods> { diff --git a/src/models/schemas/Announcement.schema.ts b/src/models/schemas/Announcement.schema.ts index 9d81a59..2ce9856 100644 --- a/src/models/schemas/Announcement.schema.ts +++ b/src/models/schemas/Announcement.schema.ts @@ -58,11 +58,21 @@ const AnnouncementSchema = new Schema( ); AnnouncementSchema.method('softDelete', async function softDelete(userId: ObjectId) { + if(this?.logicalStaffBeforeSoftDelete !== undefined){ + await this.logicalStaffBeforeSoftDelete(this); + } + this.deletedAt = Date.now(); this.deletedBy = userId; await this.save(); }); +AnnouncementSchema.pre('remove', async function (this: any) { + if(this?.logicalStaffBeforeRemove !== undefined){ + await this.logicalStaffBeforeRemove(this); + } +}) + // Plugins AnnouncementSchema.plugin(toJSON); AnnouncementSchema.plugin(paginate); diff --git a/src/models/schemas/Community.schema.ts b/src/models/schemas/Community.schema.ts index 5ef7371..029cacf 100644 --- a/src/models/schemas/Community.schema.ts +++ b/src/models/schemas/Community.schema.ts @@ -1,7 +1,7 @@ import { Schema, type Document } from 'mongoose'; import { toJSON, paginate } from './plugins'; import { type ICommunity, type CommunityModel } from '../../interfaces'; -import { User, Platform } from '../index'; +import { User, Platform, Announcement } from '../index'; const communitySchema = new Schema( { @@ -43,5 +43,6 @@ communitySchema.pre('remove', async function (this: Document) { await User.updateMany({ communities: communityId }, { $pull: { communities: communityId } }); await Platform.deleteMany({ community: communityId }); + await Announcement.deleteMany({ community: communityId }); }); export default communitySchema; diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index 913e613..fba78db 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -1,7 +1,7 @@ import { Schema, type Document } from 'mongoose'; import { toJSON, paginate } from './plugins'; import { type IPlatform, type PlatformModel } from '../../interfaces'; -import { Community } from '../index'; +import { Announcement, Community } from '../index'; const platformSchema = new Schema( { @@ -37,6 +37,10 @@ platformSchema.plugin(paginate); platformSchema.pre('remove', async function (this: Document) { const platformId = this._id; await Community.updateOne({ platforms: platformId }, { $pull: { platforms: platformId } }); + await Announcement.updateMany( + { "data.platform": platformId }, + { $pull: { data: { platform : platformId} } } + ); }); export default platformSchema; From ef2188c55a15a80e56da3080a09ef405eb9a9d4e Mon Sep 17 00:00:00 2001 From: Mohammad Twin Date: Wed, 3 Jan 2024 15:36:06 +0400 Subject: [PATCH 3/4] feat: version increased --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5210cd3..e0cefa7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@togethercrew.dev/db", - "version": "3.0.21", + "version": "3.0.22", "description": "All interactions with DB", "main": "./dist/index.js", "types": "./dist/index.d.ts", From 397fcbbe9a392ba006b111b8227ed7a29a207549 Mon Sep 17 00:00:00 2001 From: Mohammad Twin Date: Wed, 3 Jan 2024 15:46:45 +0400 Subject: [PATCH 4/4] fix: aliged code with Prettier --- src/models/schemas/Announcement.schema.ts | 6 +++--- src/models/schemas/Platform.schema.ts | 5 +---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/models/schemas/Announcement.schema.ts b/src/models/schemas/Announcement.schema.ts index 2ce9856..541a335 100644 --- a/src/models/schemas/Announcement.schema.ts +++ b/src/models/schemas/Announcement.schema.ts @@ -58,7 +58,7 @@ const AnnouncementSchema = new Schema( ); AnnouncementSchema.method('softDelete', async function softDelete(userId: ObjectId) { - if(this?.logicalStaffBeforeSoftDelete !== undefined){ + if (this?.logicalStaffBeforeSoftDelete !== undefined) { await this.logicalStaffBeforeSoftDelete(this); } @@ -68,10 +68,10 @@ AnnouncementSchema.method('softDelete', async function softDelete(userId: Object }); AnnouncementSchema.pre('remove', async function (this: any) { - if(this?.logicalStaffBeforeRemove !== undefined){ + if (this?.logicalStaffBeforeRemove !== undefined) { await this.logicalStaffBeforeRemove(this); } -}) +}); // Plugins AnnouncementSchema.plugin(toJSON); diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index fba78db..c153d95 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -37,10 +37,7 @@ platformSchema.plugin(paginate); platformSchema.pre('remove', async function (this: Document) { const platformId = this._id; await Community.updateOne({ platforms: platformId }, { $pull: { platforms: platformId } }); - await Announcement.updateMany( - { "data.platform": platformId }, - { $pull: { data: { platform : platformId} } } - ); + await Announcement.updateMany({ 'data.platform': platformId }, { $pull: { data: { platform: platformId } } }); }); export default platformSchema;