Phase 3: News CRUD — model, endpoints, ownership, интеграционные тесты
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
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,
|
||||
toJSON: { virtuals: true },
|
||||
toObject: { virtuals: true },
|
||||
},
|
||||
);
|
||||
|
||||
// Индекс для scheduler: поиск scheduled новостей с прошлым scheduledAt
|
||||
newsSchema.index({ status: 1, scheduledAt: 1 });
|
||||
|
||||
const News = model<INews>('News', newsSchema);
|
||||
|
||||
export default News;
|
||||
Reference in New Issue
Block a user