Initial commit: deardiary project setup
This commit is contained in:
69
backend/src/routes/settings.ts
Normal file
69
backend/src/routes/settings.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { Hono } from 'hono';
|
||||
import { HonoEnv } from '../lib/types';
|
||||
|
||||
export const settingsRoutes = new Hono<HonoEnv>();
|
||||
|
||||
settingsRoutes.get('/', async (c) => {
|
||||
const userId = c.get('userId');
|
||||
const prisma = c.get('prisma');
|
||||
|
||||
const settings = await prisma.settings.findUnique({
|
||||
where: { userId },
|
||||
});
|
||||
|
||||
if (!settings) {
|
||||
const newSettings = await prisma.settings.create({
|
||||
data: { userId },
|
||||
});
|
||||
return c.json({ data: newSettings, error: null });
|
||||
}
|
||||
|
||||
return c.json({ data: settings, error: null });
|
||||
});
|
||||
|
||||
settingsRoutes.put('/', async (c) => {
|
||||
const userId = c.get('userId');
|
||||
const prisma = c.get('prisma');
|
||||
|
||||
const body = await c.req.json();
|
||||
const { aiProvider, aiApiKey, aiModel, aiBaseUrl, journalPrompt, language } = body;
|
||||
|
||||
const data: Record<string, unknown> = {};
|
||||
if (aiProvider !== undefined) data.aiProvider = aiProvider;
|
||||
if (aiApiKey !== undefined) data.aiApiKey = aiApiKey;
|
||||
if (aiModel !== undefined) data.aiModel = aiModel || 'gpt-4';
|
||||
if (aiBaseUrl !== undefined) data.aiBaseUrl = aiBaseUrl;
|
||||
if (journalPrompt !== undefined) data.journalPrompt = journalPrompt;
|
||||
if (language !== undefined) data.language = language;
|
||||
|
||||
const settings = await prisma.settings.upsert({
|
||||
where: { userId },
|
||||
create: { userId, ...data },
|
||||
update: data,
|
||||
});
|
||||
|
||||
return c.json({ data: settings, error: null });
|
||||
});
|
||||
|
||||
settingsRoutes.post('/validate-key', async (c) => {
|
||||
const body = await c.req.json();
|
||||
const { provider, apiKey, baseUrl } = body;
|
||||
|
||||
if (!provider || !apiKey) {
|
||||
return c.json({ data: null, error: { code: 'VALIDATION_ERROR', message: 'provider and apiKey are required' } }, 400);
|
||||
}
|
||||
|
||||
const { createAIProvider } = await import('../services/ai/provider');
|
||||
|
||||
try {
|
||||
const aiProvider = createAIProvider({
|
||||
provider,
|
||||
apiKey,
|
||||
baseUrl,
|
||||
});
|
||||
const valid = await aiProvider.validate?.();
|
||||
return c.json({ data: { valid: true }, error: null });
|
||||
} catch {
|
||||
return c.json({ data: { valid: false }, error: null });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user