import { Injectable, OnModuleInit } from '@nestjs/common'; import { generateShortId } from '../../common/utils/id.util'; export interface Notification { id: string; userId: string; type: string; title: string; body: string; read: boolean; createdAt: Date; } @Injectable() export class NotificationsRepository implements OnModuleInit { private notifications: Notification[] = []; onModuleInit() { const demos: Partial[] = [ { title: '欢迎使用', body: '欢迎来到知习,开始你的学习之旅!', type: 'system' }, { title: '复习提醒', body: '你有 5 张卡片需要复习', type: 'review_due' }, ]; for (const demo of demos) { this.notifications.push({ id: generateShortId(), userId: '1', type: demo.type || 'system', title: demo.title || '', body: demo.body || '', read: false, createdAt: new Date(), }); } } async findAll(): Promise { return [...this.notifications].sort( (a, b) => b.createdAt.getTime() - a.createdAt.getTime(), ); } async create(data: Partial): Promise { const notification: Notification = { id: data.id || generateShortId(), userId: data.userId || 'anonymous', type: data.type || 'system', title: data.title || '', body: data.body || '', read: false, createdAt: new Date(), }; this.notifications.push(notification); return notification; } async findById(id: string): Promise { return this.notifications.find((n) => n.id === id); } async markRead(id: string): Promise { const n = this.notifications.find((x) => x.id === id); if (!n) return undefined; n.read = true; return n; } }