|
| 1 | +import { auth } from "@/lib/auth"; |
| 2 | +import { prisma } from "@kbnet/db"; |
| 3 | +import { headers } from "next/headers"; |
| 4 | +import { NextResponse } from "next/server"; |
| 5 | +import { encryptConfig } from "@kbnet/shared/encryptor"; |
| 6 | + |
| 7 | +type SetupRequestBody = { |
| 8 | + mindsdb: boolean; |
| 9 | + googleAI: boolean; |
| 10 | + apiKey?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +async function handleGoogleAIIntegration( |
| 14 | + userId: string, |
| 15 | + googleAI: boolean, |
| 16 | + apiKey?: string, |
| 17 | + existingIntegration?: any |
| 18 | +) { |
| 19 | + // Case 1: Enabling Google AI |
| 20 | + if (googleAI) { |
| 21 | + // Case 1a: New integration - API key required |
| 22 | + if (!existingIntegration && !apiKey) { |
| 23 | + throw new Error("API key is required for new Google AI integration"); |
| 24 | + } |
| 25 | + |
| 26 | + // Case 1b: Existing integration - just enable if no new key |
| 27 | + if (existingIntegration && !apiKey) { |
| 28 | + return prisma.integration.update({ |
| 29 | + where: { |
| 30 | + userId_type: { |
| 31 | + userId, |
| 32 | + type: "GOOGLE", |
| 33 | + }, |
| 34 | + }, |
| 35 | + data: { enabled: true }, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + // Case 1c: New key provided (for both new and existing) |
| 40 | + const { encrypted, iv, authTag } = encryptConfig( |
| 41 | + JSON.stringify({ apiKey }) |
| 42 | + ); |
| 43 | + |
| 44 | + const integrationData = { |
| 45 | + config: encrypted, |
| 46 | + encryptionIV: iv, |
| 47 | + encryptionAuthTag: authTag, |
| 48 | + enabled: true, |
| 49 | + }; |
| 50 | + |
| 51 | + return existingIntegration |
| 52 | + ? prisma.integration.update({ |
| 53 | + where: { |
| 54 | + userId_type: { |
| 55 | + userId, |
| 56 | + type: "GOOGLE", |
| 57 | + }, |
| 58 | + }, |
| 59 | + data: integrationData, |
| 60 | + }) |
| 61 | + : prisma.integration.create({ |
| 62 | + data: { |
| 63 | + ...integrationData, |
| 64 | + userId, |
| 65 | + type: "GOOGLE", |
| 66 | + }, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + // Case 2: Disabling Google AI |
| 71 | + if (existingIntegration) { |
| 72 | + return prisma.integration.update({ |
| 73 | + where: { |
| 74 | + userId_type: { |
| 75 | + userId, |
| 76 | + type: "GOOGLE", |
| 77 | + }, |
| 78 | + }, |
| 79 | + data: { enabled: false }, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | +} |
| 85 | + |
| 86 | +async function updateUserSettings(userId: string, settings: SetupRequestBody) { |
| 87 | + return prisma.user.update({ |
| 88 | + where: { id: userId }, |
| 89 | + data: { |
| 90 | + useMindsDB: settings.mindsdb, |
| 91 | + useBYO: settings.googleAI, |
| 92 | + }, |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +export async function POST(request: Request) { |
| 97 | + try { |
| 98 | + // 1. Authentication |
| 99 | + const session = await auth.api.getSession({ |
| 100 | + headers: await headers(), |
| 101 | + }); |
| 102 | + |
| 103 | + if (!session?.user?.id) { |
| 104 | + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 105 | + } |
| 106 | + |
| 107 | + // 2. Get request body |
| 108 | + const body: SetupRequestBody = await request.json(); |
| 109 | + |
| 110 | + // 3. Get existing integration |
| 111 | + const existingIntegration = await prisma.integration.findUnique({ |
| 112 | + where: { |
| 113 | + userId_type: { |
| 114 | + userId: session.user.id, |
| 115 | + type: "GOOGLE", |
| 116 | + }, |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + // 4. Handle Google AI integration |
| 121 | + try { |
| 122 | + await handleGoogleAIIntegration( |
| 123 | + session.user.id, |
| 124 | + body.googleAI, |
| 125 | + body.apiKey, |
| 126 | + existingIntegration |
| 127 | + ); |
| 128 | + } catch (error) { |
| 129 | + return NextResponse.json( |
| 130 | + { error: (error as Error).message }, |
| 131 | + { status: 400 } |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + // 5. Update user settings |
| 136 | + await updateUserSettings(session.user.id, body); |
| 137 | + |
| 138 | + return NextResponse.json({ success: true }); |
| 139 | + } catch (error) { |
| 140 | + console.error("Error creating/updating settings:", error); |
| 141 | + return NextResponse.json( |
| 142 | + { error: "Failed to create/update settings" }, |
| 143 | + { status: 500 } |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
0 commit comments