feat: 添加priority字段支持

- Event模型添加priority字段
- routes/events.ts支持priority的创建和更新
- Prisma schema同步更新
- 添加数据库自动迁移

🤖 Generated with Claude Code
This commit is contained in:
ddshi 2026-02-05 17:52:33 +08:00
parent 60fdd4ec2b
commit 96da6b6e42
2 changed files with 32 additions and 4 deletions

View File

@ -39,6 +39,7 @@ model Event {
repeat_type String @default("none") // 'yearly' | 'monthly' | 'none' (String for SQLite)
is_holiday Boolean @default(false) // Only for anniversaries
is_completed Boolean @default(false) // Only for reminders
priority String @default("none") // 'none' | 'red' | 'green' | 'yellow' (String for SQLite)
created_at DateTime @default(now())
updated_at DateTime @updatedAt

View File

@ -1,9 +1,28 @@
import { Router, Request, Response } from 'express';
import { z } from 'zod';
import db from '../lib/db';
import db, { dbPath } from '../lib/db';
import { authenticateToken, AuthenticatedRequest } from '../middleware/auth';
import { asyncHandler } from '../middleware/errorHandler';
// Initialize database: Add priority column if it doesn't exist
const initDb = async () => {
try {
await db.execute({
sql: `ALTER TABLE events ADD COLUMN priority TEXT DEFAULT 'none'`,
});
console.log('✓ Priority column added to events table');
} catch (error: any) {
if (error.message?.includes('duplicate column name') || error.message?.includes('already exists')) {
console.log('✓ Priority column already exists');
} else {
console.error('Error adding priority column:', error.message);
}
}
};
// Run initialization
initDb();
const router = Router();
// Validation schemas - date can be empty string for reminders
@ -19,6 +38,7 @@ const createEventSchema = z.object({
next_reminder_date: z.string().optional().nullable(), // 下一次提醒日期
is_holiday: z.boolean().default(false),
is_completed: z.boolean().default(false),
priority: z.enum(['none', 'red', 'green', 'yellow']).default('none'),
});
const updateEventSchema = createEventSchema.partial();
@ -40,6 +60,7 @@ interface EventRow {
next_reminder_date: string | null; // 下一次提醒日期
is_holiday: number;
is_completed: number;
priority: string; // 优先级none, red, green, yellow
created_at: string;
updated_at: string;
}
@ -58,6 +79,7 @@ function formatEvent(event: EventRow) {
next_reminder_date: event.next_reminder_date,
is_holiday: Boolean(event.is_holiday),
is_completed: Boolean(event.is_completed),
priority: event.priority as 'none' | 'red' | 'green' | 'yellow',
created_at: event.created_at,
updated_at: event.updated_at,
};
@ -119,9 +141,9 @@ router.post(
const nextReminderDate = data.next_reminder_date || null;
await db.execute({
sql: `INSERT INTO events (id, user_id, type, title, content, date, is_lunar, repeat_type, repeat_interval, next_reminder_date, is_holiday, is_completed, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, datetime('now'), datetime('now'))`,
args: [eventId, req.user!.userId, data.type, data.title, data.content || null, dateValue, data.is_lunar ? 1 : 0, data.repeat_type, repeatInterval, nextReminderDate, data.is_holiday ? 1 : 0],
sql: `INSERT INTO events (id, user_id, type, title, content, date, is_lunar, repeat_type, repeat_interval, next_reminder_date, is_holiday, is_completed, priority, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, datetime('now'), datetime('now'))`,
args: [eventId, req.user!.userId, data.type, data.title, data.content || null, dateValue, data.is_lunar ? 1 : 0, data.repeat_type, repeatInterval, nextReminderDate, data.is_holiday ? 1 : 0, data.priority],
});
const result = await db.execute({
@ -201,6 +223,11 @@ router.put(
args.push(data.is_completed ? 1 : 0);
}
if (data.priority) {
updates.push('priority = ?');
args.push(data.priority);
}
if (updates.length > 0) {
updates.push('updated_at = datetime(\'now\')');
args.push(req.params.id);