-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dc1390
commit 8a4f766
Showing
6 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { type Model, type Types } from 'mongoose'; | ||
|
||
type IDiscordOptions = { channelIds: string[] } | { roleIds: string[] } | { userIds: string[] }; | ||
|
||
interface IAnnouncementData<T> { | ||
platform: Types.ObjectId; | ||
template: string; | ||
options: T; | ||
} | ||
|
||
export interface IAnnouncement { | ||
community: Types.ObjectId; | ||
title: string; | ||
scheduledAt: Date; | ||
draft: boolean; | ||
jobId?: number; | ||
// !createdAt: Date; this is automatically added by mongoose | ||
// !updatedAt: Date; this is automatically added by mongoose | ||
createdBy: Types.ObjectId; | ||
updatedBy: Types.ObjectId; | ||
deletedAt?: Date; | ||
deletedBy?: Types.ObjectId; | ||
data: Array<IAnnouncementData<IDiscordOptions>>; | ||
} | ||
|
||
export interface IAnnouncementMethods { | ||
softDelete: () => void; | ||
} | ||
|
||
export interface AnnouncementModel extends Model<IAnnouncement, Record<string, unknown>, IAnnouncementMethods> { | ||
paginate: (filter: object, options: object) => any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { model } from 'mongoose'; | ||
import { announcementSchema } from './schemas'; | ||
import { type IAnnouncement, type AnnouncementModel } from '../interfaces'; | ||
|
||
export default model<IAnnouncement, AnnouncementModel>('Announcement', announcementSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Schema } from 'mongoose'; | ||
import { type IAnnouncement, type AnnouncementModel } from '../../interfaces/Announcement.interface'; | ||
import { type ObjectId } from 'mongodb'; | ||
import { paginate, toJSON } from './plugins'; | ||
|
||
const announcementDataSchema = new Schema( | ||
{ | ||
platform: { | ||
type: Schema.Types.ObjectId, | ||
required: true, | ||
}, | ||
template: { | ||
type: String, | ||
required: true, | ||
}, | ||
options: { | ||
type: Schema.Types.Mixed, | ||
}, | ||
}, | ||
{ _id: false }, | ||
); | ||
|
||
const AnnouncementSchema = new Schema<IAnnouncement, AnnouncementModel>( | ||
{ | ||
community: { | ||
type: Schema.Types.ObjectId, | ||
required: true, | ||
}, | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
scheduledAt: { | ||
type: Date, | ||
required: true, | ||
}, | ||
draft: { | ||
type: Boolean, | ||
required: true, | ||
}, | ||
jobId: Number, | ||
createdBy: { | ||
type: Schema.Types.ObjectId, | ||
required: true, | ||
}, | ||
updatedBy: { | ||
type: Schema.Types.ObjectId, | ||
required: true, | ||
}, | ||
deletedAt: Date, | ||
deletedBy: Schema.Types.ObjectId, | ||
data: [announcementDataSchema], | ||
}, | ||
{ timestamps: true }, | ||
); | ||
|
||
AnnouncementSchema.method('softDelete', async function softDelete(userId: ObjectId) { | ||
this.deletedAt = Date.now(); | ||
this.deletedBy = userId; | ||
await this.save(); | ||
}); | ||
|
||
// Plugins | ||
AnnouncementSchema.plugin(toJSON); | ||
AnnouncementSchema.plugin(paginate); | ||
|
||
export default AnnouncementSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters