Skip to content

Commit

Permalink
Feat: Announcement model was added
Browse files Browse the repository at this point in the history
  • Loading branch information
scientiststwin committed Dec 25, 2023
1 parent 3dc1390 commit 8a4f766
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/interfaces/Announcement.interface.ts
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;
}
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './User.interface';
export * from './Discord.interface';
export * from './Community.interface';
export * from './Platfrom.interface';
export * from './Announcement.interface';
5 changes: 5 additions & 0 deletions src/models/Announcement.model.ts
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);
3 changes: 2 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import Channel from './Channel.model';
import Role from './Role.model';
import Community from './Community.model';
import Platform from './Platfrom.model';
export { User, Token, HeatMap, RawInfo, MemberActivity, GuildMember, Channel, Role, Community, Platform };
import Announcement from './Announcement.model';
export { User, Token, HeatMap, RawInfo, MemberActivity, GuildMember, Channel, Role, Community, Platform, Announcement };
67 changes: 67 additions & 0 deletions src/models/schemas/Announcement.schema.ts
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;
2 changes: 2 additions & 0 deletions src/models/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import channelSchema from './Channel.schema';
import roleSchema from './Role.schema';
import communitySchema from './Community.schema';
import platformSchema from './Platform.schema';
import announcementSchema from './Announcement.schema';

export {
userSchema,
Expand All @@ -20,4 +21,5 @@ export {
roleSchema,
communitySchema,
platformSchema,
announcementSchema,
};

0 comments on commit 8a4f766

Please sign in to comment.