|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; |
| 3 | +import { selectAccountByEmail } from "@/lib/supabase/account_emails/selectAccountByEmail"; |
| 4 | +import { selectAccountByWallet } from "@/lib/supabase/account_wallets/selectAccountByWallet"; |
| 5 | +import { getAccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; |
| 6 | +import { insertAccount } from "@/lib/supabase/accounts/insertAccount"; |
| 7 | +import { insertAccountEmail } from "@/lib/supabase/account_emails/insertAccountEmail"; |
| 8 | +import { insertAccountWallet } from "@/lib/supabase/account_wallets/insertAccountWallet"; |
| 9 | +import { insertCreditsUsage } from "@/lib/supabase/credits_usage/insertCreditsUsage"; |
| 10 | +import { assignAccountToOrg } from "@/lib/organizations/assignAccountToOrg"; |
| 11 | +import type { CreateAccountBody } from "./validateCreateAccountBody"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Account data response shape. |
| 15 | + */ |
| 16 | +export type AccountDataResponse = { |
| 17 | + id: string; |
| 18 | + account_id: string; |
| 19 | + name?: string; |
| 20 | + email?: string; |
| 21 | + wallet?: string; |
| 22 | + image?: string; |
| 23 | + instruction?: string; |
| 24 | + organization?: string; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Handles POST /api/accounts - Create or retrieve account by email/wallet. |
| 29 | + * |
| 30 | + * @param body - Validated request body with email and/or wallet |
| 31 | + * @returns NextResponse with account data |
| 32 | + */ |
| 33 | +export async function createAccountHandler(body: CreateAccountBody): Promise<NextResponse> { |
| 34 | + const { email, wallet } = body; |
| 35 | + |
| 36 | + try { |
| 37 | + // If email is provided, check account_emails |
| 38 | + if (email) { |
| 39 | + const emailRecord = await selectAccountByEmail(email); |
| 40 | + if (emailRecord?.account_id) { |
| 41 | + // Assign to org based on email domain (idempotent) |
| 42 | + await assignAccountToOrg(emailRecord.account_id, email); |
| 43 | + |
| 44 | + const accountData = await getAccountWithDetails(emailRecord.account_id); |
| 45 | + if (accountData) { |
| 46 | + return NextResponse.json( |
| 47 | + { data: accountData }, |
| 48 | + { status: 200, headers: getCorsHeaders() }, |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // If wallet is provided, check account_wallets |
| 55 | + if (wallet) { |
| 56 | + try { |
| 57 | + const account = await selectAccountByWallet(wallet); |
| 58 | + |
| 59 | + // Flatten the nested relations into a single object |
| 60 | + const accountInfo = account.account_info?.[0]; |
| 61 | + const accountEmail = account.account_emails?.[0]; |
| 62 | + const accountWallet = account.account_wallets?.[0]; |
| 63 | + |
| 64 | + const accountData: AccountDataResponse = { |
| 65 | + id: account.id, |
| 66 | + account_id: account.id, |
| 67 | + name: account.name || undefined, |
| 68 | + image: accountInfo?.image || undefined, |
| 69 | + instruction: accountInfo?.instruction || undefined, |
| 70 | + organization: accountInfo?.organization || undefined, |
| 71 | + email: accountEmail?.email || undefined, |
| 72 | + wallet: accountWallet?.wallet || undefined, |
| 73 | + }; |
| 74 | + |
| 75 | + return NextResponse.json({ data: accountData }, { status: 200, headers: getCorsHeaders() }); |
| 76 | + } catch { |
| 77 | + // Wallet not found, continue to create new account |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Create new account |
| 82 | + const newAccount = await insertAccount({ name: "" }); |
| 83 | + |
| 84 | + if (email) { |
| 85 | + await insertAccountEmail(newAccount.id, email); |
| 86 | + // Assign new account to org based on email domain |
| 87 | + await assignAccountToOrg(newAccount.id, email); |
| 88 | + } |
| 89 | + |
| 90 | + if (wallet) { |
| 91 | + await insertAccountWallet(newAccount.id, wallet); |
| 92 | + } |
| 93 | + |
| 94 | + await insertCreditsUsage(newAccount.id); |
| 95 | + |
| 96 | + const newAccountData: AccountDataResponse = { |
| 97 | + id: newAccount.id, |
| 98 | + account_id: newAccount.id, |
| 99 | + email: email || "", |
| 100 | + wallet: wallet || "", |
| 101 | + image: "", |
| 102 | + instruction: "", |
| 103 | + organization: "", |
| 104 | + }; |
| 105 | + |
| 106 | + return NextResponse.json({ data: newAccountData }, { status: 200, headers: getCorsHeaders() }); |
| 107 | + } catch (error) { |
| 108 | + const message = error instanceof Error ? error.message : "failed"; |
| 109 | + return NextResponse.json({ message }, { status: 400, headers: getCorsHeaders() }); |
| 110 | + } |
| 111 | +} |
0 commit comments