60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { Schema, model, type HydratedDocument, type Types } from 'mongoose';
|
||
|
||
export type NewsStatus = 'draft' | 'scheduled' | 'published';
|
||
|
||
export interface INews {
|
||
title: string;
|
||
content: string;
|
||
status: NewsStatus;
|
||
author: Types.ObjectId;
|
||
scheduledAt: Date | null;
|
||
publishedAt: Date | null;
|
||
createdAt: Date;
|
||
updatedAt: Date;
|
||
}
|
||
|
||
export type NewsDocument = HydratedDocument<INews>;
|
||
|
||
const newsSchema = new Schema<INews>(
|
||
{
|
||
title: {
|
||
type: String,
|
||
required: [true, 'Название новости обязательно'],
|
||
trim: true,
|
||
maxlength: [200, 'Название не должно превышать 200 символов'],
|
||
},
|
||
content: {
|
||
type: String,
|
||
default: '',
|
||
},
|
||
status: {
|
||
type: String,
|
||
enum: ['draft', 'scheduled', 'published'],
|
||
default: 'draft',
|
||
},
|
||
author: {
|
||
type: Schema.Types.ObjectId,
|
||
ref: 'User',
|
||
required: [true, 'Автор новости обязателен'],
|
||
},
|
||
scheduledAt: {
|
||
type: Date,
|
||
default: null,
|
||
},
|
||
publishedAt: {
|
||
type: Date,
|
||
default: null,
|
||
},
|
||
},
|
||
{
|
||
timestamps: true,
|
||
},
|
||
);
|
||
|
||
// Индекс для scheduler: поиск scheduled новостей с прошлым scheduledAt
|
||
newsSchema.index({ status: 1, scheduledAt: 1 });
|
||
|
||
const News = model<INews>('News', newsSchema);
|
||
|
||
export default News;
|