Skip to content

Commit 4def4a3

Browse files
authored
Merge pull request #141 from TogetherCrew/feature/announcements
Feature/announcements
2 parents 6a7a4d5 + 397fcbb commit 4def4a3

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@togethercrew.dev/db",
3-
"version": "3.0.21",
3+
"version": "3.0.22",
44
"description": "All interactions with DB",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/interfaces/Announcement.interface.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface IAnnouncementData<T> {
66
platform: Types.ObjectId;
77
template: string;
88
options: T;
9+
deletedAt?: Date;
910
}
1011

1112
export interface IAnnouncement {
@@ -25,6 +26,8 @@ export interface IAnnouncement {
2526

2627
export interface IAnnouncementMethods {
2728
softDelete: () => void;
29+
logicalStaffBeforeSoftDelete?: (document: IAnnouncement) => void;
30+
logicalStaffBeforeRemove?: (document: IAnnouncement) => void;
2831
}
2932

3033
export interface AnnouncementModel extends Model<IAnnouncement, Record<string, unknown>, IAnnouncementMethods> {

src/models/schemas/Announcement.schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const announcementDataSchema = new Schema(
1717
options: {
1818
type: Schema.Types.Mixed,
1919
},
20+
deletedAt: Date,
2021
},
2122
{ _id: false },
2223
);
@@ -57,11 +58,21 @@ const AnnouncementSchema = new Schema<IAnnouncement, AnnouncementModel>(
5758
);
5859

5960
AnnouncementSchema.method('softDelete', async function softDelete(userId: ObjectId) {
61+
if (this?.logicalStaffBeforeSoftDelete !== undefined) {
62+
await this.logicalStaffBeforeSoftDelete(this);
63+
}
64+
6065
this.deletedAt = Date.now();
6166
this.deletedBy = userId;
6267
await this.save();
6368
});
6469

70+
AnnouncementSchema.pre('remove', async function (this: any) {
71+
if (this?.logicalStaffBeforeRemove !== undefined) {
72+
await this.logicalStaffBeforeRemove(this);
73+
}
74+
});
75+
6576
// Plugins
6677
AnnouncementSchema.plugin(toJSON);
6778
AnnouncementSchema.plugin(paginate);

src/models/schemas/Community.schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Schema, type Document } from 'mongoose';
22
import { toJSON, paginate } from './plugins';
33
import { type ICommunity, type CommunityModel } from '../../interfaces';
4-
import { User, Platform } from '../index';
4+
import { User, Platform, Announcement } from '../index';
55

66
const communitySchema = new Schema<ICommunity, CommunityModel>(
77
{
@@ -43,5 +43,6 @@ communitySchema.pre('remove', async function (this: Document) {
4343

4444
await User.updateMany({ communities: communityId }, { $pull: { communities: communityId } });
4545
await Platform.deleteMany({ community: communityId });
46+
await Announcement.deleteMany({ community: communityId });
4647
});
4748
export default communitySchema;

src/models/schemas/Platform.schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Schema, type Document } from 'mongoose';
22
import { toJSON, paginate } from './plugins';
33
import { type IPlatform, type PlatformModel } from '../../interfaces';
4-
import { Community } from '../index';
4+
import { Announcement, Community } from '../index';
55

66
const platformSchema = new Schema<IPlatform, PlatformModel>(
77
{
@@ -37,6 +37,7 @@ platformSchema.plugin(paginate);
3737
platformSchema.pre('remove', async function (this: Document) {
3838
const platformId = this._id;
3939
await Community.updateOne({ platforms: platformId }, { $pull: { platforms: platformId } });
40+
await Announcement.updateMany({ 'data.platform': platformId }, { $pull: { data: { platform: platformId } } });
4041
});
4142

4243
export default platformSchema;

0 commit comments

Comments
 (0)