feat: AI API Setting page with 4 provider sections and toggles
Add OpenAI, Claude AI, and DentalManagement AI sections to the AI API Setting page, each with a masked API key input and an on/off toggle (defaulting to off). Rename sidebar label from "Google AI Settings" to "AI API Setting". Add provider-key and provider-enabled backend endpoints and extend the AiSettings schema with 6 new fields. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -15,9 +15,16 @@ router.get("/settings", async (req: Request, res: Response): Promise<any> => {
|
||||
if (!settings) return res.status(200).json(null);
|
||||
|
||||
return res.status(200).json({
|
||||
id: settings.id,
|
||||
apiKey: settings.apiKey,
|
||||
openPhoneReply: settings.openPhoneReply ?? false,
|
||||
id: settings.id,
|
||||
apiKey: settings.apiKey,
|
||||
aiEnabled: settings.aiEnabled ?? true,
|
||||
openAiKey: settings.openAiKey ?? "",
|
||||
openAiEnabled: settings.openAiEnabled ?? false,
|
||||
claudeAiKey: settings.claudeAiKey ?? "",
|
||||
claudeAiEnabled: settings.claudeAiEnabled ?? false,
|
||||
dentalMgmtKey: settings.dentalMgmtKey ?? "",
|
||||
dentalMgmtEnabled: settings.dentalMgmtEnabled ?? false,
|
||||
openPhoneReply: settings.openPhoneReply ?? false,
|
||||
});
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to fetch AI settings", details: String(err) });
|
||||
@@ -30,18 +37,78 @@ router.put("/settings", async (req: Request, res: Response): Promise<any> => {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const { apiKey } = req.body;
|
||||
const { apiKey, aiEnabled } = 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 });
|
||||
const settings = await storage.upsertAiSettings(userId, apiKey.trim(), aiEnabled);
|
||||
return res.status(200).json({ id: settings.id, apiKey: settings.apiKey, aiEnabled: settings.aiEnabled ?? true });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to save AI settings", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/ai/enabled
|
||||
router.put("/enabled", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const { aiEnabled } = req.body;
|
||||
if (typeof aiEnabled !== "boolean") {
|
||||
return res.status(400).json({ message: "aiEnabled must be a boolean" });
|
||||
}
|
||||
|
||||
await storage.setAiEnabled(userId, aiEnabled);
|
||||
return res.status(200).json({ aiEnabled });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to save AI enabled setting", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/ai/provider-key
|
||||
router.put("/provider-key", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const { provider, apiKey } = req.body;
|
||||
if (!["openAi", "claudeAi", "dentalMgmt"].includes(provider)) {
|
||||
return res.status(400).json({ message: "Invalid provider" });
|
||||
}
|
||||
if (!apiKey?.trim()) {
|
||||
return res.status(400).json({ message: "apiKey is required" });
|
||||
}
|
||||
|
||||
await storage.upsertProviderKey(userId, provider, apiKey.trim());
|
||||
return res.status(200).json({ provider, apiKey: apiKey.trim() });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to save provider key", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/ai/provider-enabled
|
||||
router.put("/provider-enabled", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const { provider, enabled } = req.body;
|
||||
if (!["openAi", "claudeAi", "dentalMgmt"].includes(provider)) {
|
||||
return res.status(400).json({ message: "Invalid provider" });
|
||||
}
|
||||
if (typeof enabled !== "boolean") {
|
||||
return res.status(400).json({ message: "enabled must be a boolean" });
|
||||
}
|
||||
|
||||
await storage.setProviderEnabled(userId, provider, enabled);
|
||||
return res.status(200).json({ provider, enabled });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to save provider enabled setting", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/ai/advanced-settings
|
||||
router.get("/advanced-settings", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
|
||||
@@ -5,11 +5,37 @@ export const aiSettingsStorage = {
|
||||
return db.aiSettings.findUnique({ where: { userId } });
|
||||
},
|
||||
|
||||
async upsertAiSettings(userId: number, apiKey: string) {
|
||||
async upsertAiSettings(userId: number, apiKey: string, aiEnabled?: boolean) {
|
||||
return db.aiSettings.upsert({
|
||||
where: { userId },
|
||||
update: { apiKey },
|
||||
create: { userId, apiKey },
|
||||
update: { apiKey, ...(aiEnabled !== undefined && { aiEnabled }) },
|
||||
create: { userId, apiKey, aiEnabled: aiEnabled ?? true },
|
||||
});
|
||||
},
|
||||
|
||||
async setAiEnabled(userId: number, enabled: boolean): Promise<void> {
|
||||
await db.aiSettings.upsert({
|
||||
where: { userId },
|
||||
update: { aiEnabled: enabled },
|
||||
create: { userId, apiKey: "", aiEnabled: enabled },
|
||||
});
|
||||
},
|
||||
|
||||
async upsertProviderKey(userId: number, provider: "openAi" | "claudeAi" | "dentalMgmt", key: string): Promise<void> {
|
||||
const field = provider === "openAi" ? "openAiKey" : provider === "claudeAi" ? "claudeAiKey" : "dentalMgmtKey";
|
||||
await db.aiSettings.upsert({
|
||||
where: { userId },
|
||||
update: { [field]: key },
|
||||
create: { userId, apiKey: "", [field]: key },
|
||||
});
|
||||
},
|
||||
|
||||
async setProviderEnabled(userId: number, provider: "openAi" | "claudeAi" | "dentalMgmt", enabled: boolean): Promise<void> {
|
||||
const field = provider === "openAi" ? "openAiEnabled" : provider === "claudeAi" ? "claudeAiEnabled" : "dentalMgmtEnabled";
|
||||
await db.aiSettings.upsert({
|
||||
where: { userId },
|
||||
update: { [field]: enabled },
|
||||
create: { userId, apiKey: "", [field]: enabled, openAiEnabled: false, claudeAiEnabled: false, dentalMgmtEnabled: false },
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user