diff --git a/app/api/account/add_artist/route.tsx b/app/api/account/add_artist/route.tsx deleted file mode 100644 index 4443f7f72..000000000 --- a/app/api/account/add_artist/route.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import supabase from "@/lib/supabase/serverClient"; -import { NextRequest } from "next/server"; - -export async function GET(req: NextRequest) { - const email = req.nextUrl.searchParams.get("email"); - const artistId = req.nextUrl.searchParams.get("artistId"); - try { - const { data: found } = await supabase - .from("accounts") - .select("*") - .eq("email", email); - - if (found?.length) { - const artistIds = found[0].artistIds; - if (artistIds.includes(artistId)) - return Response.json({ success: true }, { status: 200 }); - await supabase - .from("accounts") - .update({ - ...found[0], - artistIds: [...artistIds, artistId], - }) - .eq("id", found[0].id); - return Response.json({ success: true }, { status: 200 }); - } - return Response.json({ message: "Not found account." }, { status: 400 }); - } catch (error) { - console.error(error); - const message = error instanceof Error ? error.message : "failed"; - return Response.json({ message }, { status: 400 }); - } -} - -export const dynamic = "force-dynamic"; -export const fetchCache = "force-no-store"; -export const revalidate = 0; diff --git a/app/api/account/route.tsx b/app/api/account/route.tsx deleted file mode 100644 index 11f6d6faf..000000000 --- a/app/api/account/route.tsx +++ /dev/null @@ -1,122 +0,0 @@ -import { NextRequest } from "next/server"; -import getAccountByEmail from "@/lib/supabase/accounts/getAccountByEmail"; -import { getAccountByWallet } from "@/lib/supabase/accounts/getAccountByWallet"; -import { getAccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; -import insertAccount from "@/lib/supabase/accounts/insertAccount"; -import insertAccountEmail from "@/lib/supabase/account_emails/insertAccountEmail"; -import { insertAccountWallet } from "@/lib/supabase/accounts/insertAccountWallet"; -import { initializeAccountCredits } from "@/lib/supabase/credits_usage/initializeAccountCredits"; -import assignAccountToOrg from "@/lib/organizations/assignAccountToOrg"; -import { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; - -export async function POST(req: NextRequest) { - const body = await req.json(); - const email = body.email; - const wallet = body.wallet; - - try { - // If email is provided, check account_emails - if (email) { - try { - const emailRecord = await getAccountByEmail(email); - if (emailRecord?.account_id) { - // Assign to org based on email domain (idempotent) - await assignAccountToOrg(emailRecord.account_id, email); - - const accountData = await getAccountWithDetails( - emailRecord.account_id - ); - return Response.json({ data: accountData }, { status: 200 }); - } - } catch (error) { - console.log( - "Email not found, creating new account:", - error instanceof Error ? error.message : "Unknown error" - ); - // Continue to wallet check if email not found - } - } - - if (wallet) { - try { - const account = await getAccountByWallet(wallet); - - // Destructure to avoid ID conflicts - const accountInfo = account.account_info?.[0]; - const accountEmail = account.account_emails?.[0]; - const accountWallet = account.account_wallets?.[0]; - - const { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - id: _infoId, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - account_id: _accountId, - ...info - } = accountInfo || {}; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { id: _walletId, ...walletData } = accountWallet || {}; - const { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - id: _emailId, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - account_id: _emailAccountId, - ...email - } = accountEmail || {}; - - const accountData = { - id: account.id, // Keep the ACCOUNT id - account_id: account.id, // Also set account_id for consistency - name: account.name, - ...info, - ...walletData, - ...email, - } as AccountWithDetails; - return Response.json({ data: accountData }, { status: 200 }); - } catch (error) { - console.log( - "Wallet not found, checking email:", - error instanceof Error ? error.message : "Unknown error" - ); - // Continue to create new account if wallet not found - } - } - - const newAccount = await insertAccount({ name: "" }); - - if (!newAccount) { - throw new Error("Failed to create account"); - } - - if (email) { - await insertAccountEmail(newAccount.id, email); - // Assign new account to org based on email domain - await assignAccountToOrg(newAccount.id, email); - } - - if (wallet) { - await insertAccountWallet(newAccount.id, wallet); - } - - await initializeAccountCredits(newAccount.id); - - const newAccountData = { - id: newAccount.id, - account_id: newAccount.id, - email: email || "", - wallet: wallet || "", - image: "", - instruction: "", - organization: "", - }; - - return Response.json({ data: newAccountData }, { status: 200 }); - } catch (error) { - console.error(error); - const message = error instanceof Error ? error.message : "failed"; - return Response.json({ message }, { status: 400 }); - } -} - -export const dynamic = "force-dynamic"; -export const fetchCache = "force-no-store"; -export const revalidate = 0; diff --git a/app/api/account/update/route.tsx b/app/api/account/update/route.tsx deleted file mode 100644 index b3e8fecf5..000000000 --- a/app/api/account/update/route.tsx +++ /dev/null @@ -1,78 +0,0 @@ -import { NextRequest } from "next/server"; -import getAccountById from "@/lib/supabase/accounts/getAccountById"; -import updateAccount from "@/lib/supabase/accounts/updateAccount"; -import insertAccountInfo from "@/lib/supabase/account_info/insertAccountInfo"; -import updateAccountInfo from "@/lib/supabase/account_info/updateAccountInfo"; - -export async function POST(req: NextRequest) { - const body = await req.json(); - const { instruction, name, organization, accountId, image, jobTitle, roleType, companyName, knowledges } = body; - - try { - const found = await getAccountById(accountId); - if (!found) { - return Response.json({ data: null }, { status: 400 }); - } - - await updateAccount(accountId, { name }); - const accountInfoRow = found.account_info?.[0]; - if (!accountInfoRow) { - await insertAccountInfo({ - organization, - image, - instruction, - account_id: accountId, - job_title: jobTitle, - role_type: roleType, - company_name: companyName, - knowledges, - }); - } else { - await updateAccountInfo(accountId, { - organization, - image, - instruction, - job_title: jobTitle, - role_type: roleType, - company_name: companyName, - knowledges, - }); - } - - // Fetch the updated account with all joined info - const updated = await getAccountById(accountId); - - // Spread account_info, account_wallets, account_emails into top-level - // Destructure to avoid ID conflicts (exclude id and account_id from all spreads for consistency) - const accountInfo = updated?.account_info?.[0]; - const accountWallet = updated?.account_wallets?.[0]; - const accountEmail = updated?.account_emails?.[0]; - - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { id: _infoId, account_id: _accountId, ...info } = accountInfo || {}; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { id: _walletId, ...wallet } = accountWallet || {}; - // account_emails only has 'email' field (no id to exclude) - const email = accountEmail || {}; - - const response = { - data: { - id: updated?.id, // Keep the ACCOUNT id - account_id: updated?.id, // Also set account_id for consistency - name: updated?.name, - ...info, - ...wallet, - ...email, - }, - }; - return Response.json(response, { status: 200 }); - } catch (error) { - console.error(error); - const message = error instanceof Error ? error.message : "failed"; - return Response.json({ message }, { status: 400 }); - } -} - -export const dynamic = "force-dynamic"; -export const fetchCache = "force-no-store"; -export const revalidate = 0; diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index d40846edd..ce91da1b8 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -141,8 +141,8 @@ const useOrgSettings = (orgId: string | null) => { setIsSaving(true); try { - const response = await fetch("/api/account/update", { - method: "POST", + const response = await fetch(`${NEW_API_BASE_URL}/api/accounts`, { + method: "PATCH", body: JSON.stringify({ accountId: orgId, name, diff --git a/hooks/useUser.tsx b/hooks/useUser.tsx index c2e31c206..b2899d164 100644 --- a/hooks/useUser.tsx +++ b/hooks/useUser.tsx @@ -7,6 +7,7 @@ import { uploadFile } from "@/lib/arweave/uploadFile"; import { useAccount } from "wagmi"; import { toast } from "sonner"; import { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; +import { NEW_API_BASE_URL } from "@/lib/consts"; const useUser = () => { const { login, user, logout } = usePrivy(); @@ -63,8 +64,8 @@ const useUser = () => { setUpdating(true); try { - const response = await fetch("/api/account/update", { - method: "POST", + const response = await fetch(`${NEW_API_BASE_URL}/api/accounts`, { + method: "PATCH", body: JSON.stringify({ instruction, organization, @@ -129,7 +130,7 @@ const useUser = () => { "Content-Type": "application/json", }, }; - const response = await fetch("/api/account", config); + const response = await fetch(`${NEW_API_BASE_URL}/api/accounts`, config); if (!response.ok) { throw new Error(