Phase 3: News CRUD — model, endpoints, ownership, интеграционные тесты

This commit is contained in:
Suslov-MD
2026-08-16 06:37:27 +00:00
parent 391b8dff88
commit 7c89a93442
9 changed files with 431 additions and 3 deletions
+61
View File
@@ -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;