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 f6d57759c..a4cde19e5 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -4,6 +4,8 @@ import { uploadFile } from "@/lib/arweave/uploadFile"; import { getFileMimeType } from "@/utils/getFileMimeType"; import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import useAccountOrganizations from "./useAccountOrganizations"; +import { usePrivy } from "@privy-io/react-auth"; +import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile"; interface KnowledgeItem { name: string; @@ -21,6 +23,7 @@ interface OrgData { const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); + const { getAccessToken } = usePrivy(); const queryClient = useQueryClient(); const [orgData, setOrgData] = useState(null); const [name, setName] = useState(""); @@ -141,30 +144,26 @@ const useOrgSettings = (orgId: string | null) => { setIsSaving(true); try { - const response = await fetch("/api/account/update", { - method: "POST", - body: JSON.stringify({ - accountId: orgId, - name, - image, - instruction, - knowledges, - }), - headers: { "Content-Type": "application/json" }, - }); - - if (response.ok) { - const data = await response.json(); - setOrgData(data.data); - // Invalidate org list cache so sidebar shows updated image/name immediately - await queryClient.invalidateQueries({ queryKey: ["accountOrganizations"] }); - return true; + const accessToken = await getAccessToken(); + if (!accessToken) { + return false; } - return false; + + const data = await updateAccountProfile({ + accessToken, + accountId: orgId, + name, + image, + instruction, + knowledges, + }); + setOrgData(data); + await queryClient.invalidateQueries({ queryKey: ["accountOrganizations"] }); + return true; } finally { setIsSaving(false); } - }, [orgId, name, image, instruction, knowledges, queryClient]); + }, [orgId, name, image, instruction, knowledges, queryClient, getAccessToken]); return { orgData, diff --git a/hooks/useUser.tsx b/hooks/useUser.tsx index 6c2b8ea0c..81e4532ad 100644 --- a/hooks/useUser.tsx +++ b/hooks/useUser.tsx @@ -8,6 +8,7 @@ import { useAccount } from "wagmi"; import { toast } from "sonner"; import { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; import { fetchOrCreateAccount } from "@/lib/accounts/fetchOrCreateAccount"; +import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile"; const useUser = () => { const { login, user, logout, getAccessToken } = usePrivy(); @@ -64,29 +65,23 @@ const useUser = () => { setUpdating(true); try { - const response = await fetch("/api/account/update", { - method: "POST", - body: JSON.stringify({ - instruction, - organization, - name, - image, - jobTitle, - roleType, - companyName, - accountId: userData.account_id, - }), - headers: { - "Content-Type": "application/json", - }, - }); - - if (!response.ok) { + const accessToken = await getAccessToken(); + if (!accessToken) { return; } - const data = await response.json(); - setUserData(data.data); + const data = await updateAccountProfile({ + accessToken, + accountId: userData.account_id, + instruction, + organization, + name, + image, + jobTitle, + roleType, + companyName, + }); + setUserData(data); setIsModalOpen(false); } catch { // Error handled silently diff --git a/lib/accounts/updateAccountProfile.ts b/lib/accounts/updateAccountProfile.ts new file mode 100644 index 000000000..06dacc850 --- /dev/null +++ b/lib/accounts/updateAccountProfile.ts @@ -0,0 +1,44 @@ +import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; + +type KnowledgeItem = { + name: string; + url: string; + type: string; +}; + +type UpdateAccountProfileArgs = { + accountId: string; + accessToken: string; + name?: string; + instruction?: string; + organization?: string; + image?: string; + jobTitle?: string; + roleType?: string; + companyName?: string; + knowledges?: KnowledgeItem[]; +}; + +/** + * Updates an account profile using the dedicated accounts API. + */ +export async function updateAccountProfile({ + accessToken, + ...body +}: UpdateAccountProfileArgs) { + const response = await fetch(`${getClientApiBaseUrl()}/api/accounts`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + }, + body: JSON.stringify(body), + }); + + if (!response.ok) { + throw new Error(`Failed to update account: ${response.status}`); + } + + const data = await response.json(); + return data.data; +}