Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/Announcement.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface IAnnouncementData<T> {
platform: Types.ObjectId;
template: string;
options: T;
deletedAt?: Date;
}

export interface IAnnouncement {
Expand All @@ -25,6 +26,8 @@ export interface IAnnouncement {

export interface IAnnouncementMethods {
softDelete: () => void;
logicalStaffBeforeSoftDelete?: (document: IAnnouncement) => void;
logicalStaffBeforeRemove?: (document: IAnnouncement) => void;
}

export interface AnnouncementModel extends Model<IAnnouncement, Record<string, unknown>, IAnnouncementMethods> {
Expand Down
11 changes: 11 additions & 0 deletions src/models/schemas/Announcement.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const announcementDataSchema = new Schema(
options: {
type: Schema.Types.Mixed,
},
deletedAt: Date,
},
{ _id: false },
);
Expand Down Expand Up @@ -57,11 +58,21 @@ const AnnouncementSchema = new Schema<IAnnouncement, AnnouncementModel>(
);

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);
Expand Down
3 changes: 2 additions & 1 deletion src/models/schemas/Community.schema.ts
Original file line number Diff line number Diff line change
@@ -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<ICommunity, CommunityModel>(
{
Expand Down Expand Up @@ -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;
3 changes: 2 additions & 1 deletion src/models/schemas/Platform.schema.ts
Original file line number Diff line number Diff line change
@@ -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<IPlatform, PlatformModel>(
{
Expand Down Expand Up @@ -37,6 +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 } } });
});

export default platformSchema;