75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
import { Schema, model, type HydratedDocument, type Types } from 'mongoose';
|
|
|
|
/** Тип уведомления о действиях с новостью. */
|
|
export type NotificationType = 'news.created' | 'news.updated' | 'news.deleted' | 'news.scheduled' | 'news.published';
|
|
|
|
export interface INotification {
|
|
/** Получатель уведомления. */
|
|
user: Types.ObjectId;
|
|
/** Кто совершил действие (может совпадать с user). */
|
|
actor: Types.ObjectId | null;
|
|
/** Тип события. */
|
|
type: NotificationType;
|
|
/** Ссылка на новость (если применимо). */
|
|
news: Types.ObjectId | null;
|
|
/** Заголовок уведомления. */
|
|
title: string;
|
|
/** Тело сообщения. */
|
|
message: string;
|
|
/** Прочитано ли. */
|
|
isRead: boolean;
|
|
createdAt: Date;
|
|
}
|
|
|
|
export type NotificationDocument = HydratedDocument<INotification>;
|
|
|
|
const notificationSchema = new Schema<INotification>(
|
|
{
|
|
user: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'User',
|
|
required: [true, 'Получатель уведомления обязателен'],
|
|
},
|
|
actor: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'User',
|
|
// Могут совпадать с user (однопользовательский сценарий) или
|
|
// быть null (системные уведомления). Mongoose кастит string → ObjectId.
|
|
default: null,
|
|
},
|
|
type: {
|
|
type: String,
|
|
enum: ['news.created', 'news.updated', 'news.deleted', 'news.scheduled', 'news.published'],
|
|
required: [true, 'Тип уведомления обязателен'],
|
|
},
|
|
news: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: 'News',
|
|
default: null,
|
|
},
|
|
title: {
|
|
type: String,
|
|
required: [true, 'Заголовок уведомления обязателен'],
|
|
trim: true,
|
|
},
|
|
message: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
isRead: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
},
|
|
{
|
|
timestamps: { createdAt: true, updatedAt: false },
|
|
},
|
|
);
|
|
|
|
// Индекс для быстрого поиска непрочитанных
|
|
notificationSchema.index({ user: 1, isRead: 1, createdAt: -1 });
|
|
|
|
const Notification = model<INotification>('Notification', notificationSchema);
|
|
|
|
export default Notification;
|