feat: add AI settings routes, storage, UI card, and migration
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
39
apps/Backend/src/routes/ai-settings.ts
Normal file
39
apps/Backend/src/routes/ai-settings.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import express, { Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// GET /api/ai/settings
|
||||
router.get("/settings", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const settings = await storage.getAiSettings(userId);
|
||||
if (!settings) return res.status(200).json(null);
|
||||
|
||||
return res.status(200).json({ id: settings.id, apiKey: settings.apiKey });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to fetch AI settings", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/ai/settings
|
||||
router.put("/settings", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const { apiKey } = req.body;
|
||||
if (!apiKey?.trim()) {
|
||||
return res.status(400).json({ message: "apiKey is required" });
|
||||
}
|
||||
|
||||
const settings = await storage.upsertAiSettings(userId, apiKey.trim());
|
||||
return res.status(200).json({ id: settings.id, apiKey: settings.apiKey });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to save AI settings", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user