diff --git a/src/server/index.ts b/src/server/index.ts index c718588..fdd9411 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -5,6 +5,7 @@ import { cors } from 'hono/cors'; import dotenv from 'dotenv'; import vehiclesRouter from './routes/vehicles.js'; import mileageRouter from './routes/mileage/index.js'; +import schedulingRouter from './routes/scheduling/index.js'; import authRouter from './auth/login.js'; import { authMiddleware } from './auth/middleware.js'; @@ -22,6 +23,7 @@ app.use('/api/*', authMiddleware); app.route('/api/vehicles', vehiclesRouter); app.route('/api/mileage', mileageRouter); +app.route('/api/scheduling', schedulingRouter); app.get('/api/health', (c) => c.json({ status: 'ok', time: new Date().toISOString() })); diff --git a/src/server/routes/scheduling/index.ts b/src/server/routes/scheduling/index.ts new file mode 100644 index 0000000..1233612 --- /dev/null +++ b/src/server/routes/scheduling/index.ts @@ -0,0 +1,10 @@ +import { Hono } from 'hono'; +import suggestionsRouter from './suggestions.js'; +import notifyRouter from './notify.js'; + +const app = new Hono(); + +app.route('/suggestions', suggestionsRouter); +app.route('/notify', notifyRouter); + +export default app; diff --git a/src/server/routes/scheduling/notify.ts b/src/server/routes/scheduling/notify.ts new file mode 100644 index 0000000..49cbfd0 --- /dev/null +++ b/src/server/routes/scheduling/notify.ts @@ -0,0 +1,41 @@ +import { Hono } from 'hono'; +import type { AuthUser } from '../../auth/types.js'; +import type { NotifyRequest } from './types.js'; + +const app = new Hono(); + +// In-memory set of processed suggestion IDs +const processedSuggestions = new Set(); + +export function isProcessed(suggestionId: string): boolean { + return processedSuggestions.has(suggestionId); +} + +app.post('/', async (c) => { + try { + const body = await c.req.json(); + const { suggestionId, currentPlate, candidatePlate } = body; + + if (!suggestionId || !currentPlate || !candidatePlate) { + return c.json({ success: false, message: '缺少必要参数' }, 400); + } + + if (processedSuggestions.has(suggestionId)) { + return c.json({ success: false, message: '该建议已处理' }, 409); + } + + const user = (c as any).get('user') as AuthUser | undefined; + const operator = user?.userName || '未知'; + + console.log(`[scheduling:notify] operator=${operator} suggestion=${suggestionId} current=${currentPlate} candidate=${candidatePlate}`); + + processedSuggestions.add(suggestionId); + + return c.json({ success: true, message: `替换通知已发送:${currentPlate} → ${candidatePlate}` }); + } catch (e: unknown) { + console.error('scheduling notify error:', e); + return c.json({ success: false, message: '发送通知失败' }, 500); + } +}); + +export default app;