From c12eed535b5c6d213a172a5a004b1992085a405d Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 02:33:44 +0530 Subject: [PATCH 01/10] feat: migrate account update to dedicated api --- app/api/account/update/route.tsx | 78 ---------------------------- hooks/useOrgSettings.ts | 74 ++++++++++++++++++-------- hooks/useUser.tsx | 35 ++++++------- lib/accounts/updateAccountProfile.ts | 49 +++++++++++++++++ 4 files changed, 116 insertions(+), 120 deletions(-) delete mode 100644 app/api/account/update/route.tsx create mode 100644 lib/accounts/updateAccountProfile.ts 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..bfffae471 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; @@ -19,8 +21,28 @@ interface OrgData { knowledges?: KnowledgeItem[]; } +const normalizeKnowledges = (value: unknown): KnowledgeItem[] => { + if (!Array.isArray(value)) { + return []; + } + + return value.filter((item): item is KnowledgeItem => { + if (!item || typeof item !== "object") { + return false; + } + + const knowledge = item as Partial; + return ( + typeof knowledge.name === "string" && + typeof knowledge.url === "string" && + typeof knowledge.type === "string" + ); + }); +}; + const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); + const { getAccessToken } = usePrivy(); const queryClient = useQueryClient(); const [orgData, setOrgData] = useState(null); const [name, setName] = useState(""); @@ -70,9 +92,15 @@ const useOrgSettings = (orgId: string | null) => { const data = await response.json(); // Response structure: { status: "success", account: {...} } const account = data.account; - setOrgData(account); + setOrgData({ + id: account?.id || "", + name: account?.name || "", + image: account?.image || "", + instruction: account?.instruction || "", + knowledges: normalizeKnowledges(account?.knowledges), + }); setInstruction(account?.instruction || ""); - setKnowledges(account?.knowledges || []); + setKnowledges(normalizeKnowledges(account?.knowledges)); } } catch (error) { console.error("Error fetching org details:", error); @@ -141,30 +169,32 @@ 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({ + id: data.id, + name: data.name || "", + image: data.image || "", + instruction: data.instruction || "", + knowledges: normalizeKnowledges(data.knowledges), + }); + 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..dbc1808be --- /dev/null +++ b/lib/accounts/updateAccountProfile.ts @@ -0,0 +1,49 @@ +import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; +import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; + +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[]; +}; + +type UpdateAccountProfileResponse = { + data: AccountWithDetails; +}; + +/** + * Updates an account profile using the dedicated accounts API. + */ +export async function updateAccountProfile({ + accessToken, + ...body +}: UpdateAccountProfileArgs): Promise { + 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: UpdateAccountProfileResponse = await response.json(); + return data.data; +} From eba5cf8ddfbb2c1926788e28937e8ddf1817c7ca Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:23:03 +0530 Subject: [PATCH 02/10] refactor: share account knowledge types --- hooks/useOrgSettings.ts | 33 +++++----------------------- lib/accounts/normalizeKnowledges.ts | 20 +++++++++++++++++ lib/accounts/updateAccountProfile.ts | 9 ++------ types/Knowledge.ts | 5 +++++ 4 files changed, 32 insertions(+), 35 deletions(-) create mode 100644 lib/accounts/normalizeKnowledges.ts create mode 100644 types/Knowledge.ts diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index bfffae471..45a97437e 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -6,40 +6,17 @@ 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; - url: string; - type: string; -} +import { normalizeKnowledges } from "@/lib/accounts/normalizeKnowledges"; +import type { Knowledge } from "@/types/Knowledge"; interface OrgData { id: string; name: string; image?: string; instruction?: string; - knowledges?: KnowledgeItem[]; + knowledges?: Knowledge[]; } -const normalizeKnowledges = (value: unknown): KnowledgeItem[] => { - if (!Array.isArray(value)) { - return []; - } - - return value.filter((item): item is KnowledgeItem => { - if (!item || typeof item !== "object") { - return false; - } - - const knowledge = item as Partial; - return ( - typeof knowledge.name === "string" && - typeof knowledge.url === "string" && - typeof knowledge.type === "string" - ); - }); -}; - const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); const { getAccessToken } = usePrivy(); @@ -48,7 +25,7 @@ const useOrgSettings = (orgId: string | null) => { const [name, setName] = useState(""); const [image, setImage] = useState(""); const [instruction, setInstruction] = useState(""); - const [knowledges, setKnowledges] = useState([]); + const [knowledges, setKnowledges] = useState([]); const [isLoading, setIsLoading] = useState(false); const [isSaving, setIsSaving] = useState(false); const [imageUploading, setImageUploading] = useState(false); @@ -141,7 +118,7 @@ const useOrgSettings = (orgId: string | null) => { if (!files) return; setKnowledgeUploading(true); - const newKnowledges: KnowledgeItem[] = []; + const newKnowledges: Knowledge[] = []; try { for (const file of files) { const name = file.name; diff --git a/lib/accounts/normalizeKnowledges.ts b/lib/accounts/normalizeKnowledges.ts new file mode 100644 index 000000000..806568161 --- /dev/null +++ b/lib/accounts/normalizeKnowledges.ts @@ -0,0 +1,20 @@ +import type { Knowledge } from "@/types/Knowledge"; + +export function normalizeKnowledges(value: unknown): Knowledge[] { + if (!Array.isArray(value)) { + return []; + } + + return value.filter((item): item is Knowledge => { + if (!item || typeof item !== "object") { + return false; + } + + const knowledge = item as Partial; + return ( + typeof knowledge.name === "string" && + typeof knowledge.url === "string" && + typeof knowledge.type === "string" + ); + }); +} diff --git a/lib/accounts/updateAccountProfile.ts b/lib/accounts/updateAccountProfile.ts index dbc1808be..a7c75ff27 100644 --- a/lib/accounts/updateAccountProfile.ts +++ b/lib/accounts/updateAccountProfile.ts @@ -1,11 +1,6 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; - -type KnowledgeItem = { - name: string; - url: string; - type: string; -}; +import type { Knowledge } from "@/types/Knowledge"; type UpdateAccountProfileArgs = { accountId: string; @@ -17,7 +12,7 @@ type UpdateAccountProfileArgs = { jobTitle?: string; roleType?: string; companyName?: string; - knowledges?: KnowledgeItem[]; + knowledges?: Knowledge[]; }; type UpdateAccountProfileResponse = { diff --git a/types/Knowledge.ts b/types/Knowledge.ts new file mode 100644 index 000000000..64b9885d3 --- /dev/null +++ b/types/Knowledge.ts @@ -0,0 +1,5 @@ +export interface Knowledge { + url: string; + name: string; + type: string; +} From df826658b17256eb372749677496f9ac1d44937d Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:32:38 +0530 Subject: [PATCH 03/10] refactor: trust account knowledge response shape --- hooks/useOrgSettings.ts | 15 ++++++++++----- lib/supabase/accounts/getAccountWithDetails.ts | 5 ++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index 45a97437e..94b3ca1b0 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -6,7 +6,7 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import useAccountOrganizations from "./useAccountOrganizations"; import { usePrivy } from "@privy-io/react-auth"; import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile"; -import { normalizeKnowledges } from "@/lib/accounts/normalizeKnowledges"; +import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; import type { Knowledge } from "@/types/Knowledge"; interface OrgData { @@ -17,6 +17,11 @@ interface OrgData { knowledges?: Knowledge[]; } +interface GetAccountResponse { + status: "success"; + account: AccountWithDetails; +} + const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); const { getAccessToken } = usePrivy(); @@ -66,7 +71,7 @@ const useOrgSettings = (orgId: string | null) => { `${getClientApiBaseUrl()}/api/accounts/${orgId}` ); if (response.ok) { - const data = await response.json(); + const data: GetAccountResponse = await response.json(); // Response structure: { status: "success", account: {...} } const account = data.account; setOrgData({ @@ -74,10 +79,10 @@ const useOrgSettings = (orgId: string | null) => { name: account?.name || "", image: account?.image || "", instruction: account?.instruction || "", - knowledges: normalizeKnowledges(account?.knowledges), + knowledges: account?.knowledges || [], }); setInstruction(account?.instruction || ""); - setKnowledges(normalizeKnowledges(account?.knowledges)); + setKnowledges(account?.knowledges || []); } } catch (error) { console.error("Error fetching org details:", error); @@ -164,7 +169,7 @@ const useOrgSettings = (orgId: string | null) => { name: data.name || "", image: data.image || "", instruction: data.instruction || "", - knowledges: normalizeKnowledges(data.knowledges), + knowledges: data.knowledges || [], }); await queryClient.invalidateQueries({ queryKey: ["accountOrganizations"] }); return true; diff --git a/lib/supabase/accounts/getAccountWithDetails.ts b/lib/supabase/accounts/getAccountWithDetails.ts index f2d43af6b..b62764b9c 100644 --- a/lib/supabase/accounts/getAccountWithDetails.ts +++ b/lib/supabase/accounts/getAccountWithDetails.ts @@ -1,8 +1,11 @@ import supabase from "../serverClient"; import { Tables } from "@/types/database.types"; +import type { Knowledge } from "@/types/Knowledge"; type Account = Tables<"accounts">; -type AccountInfo = Tables<"account_info">; +type AccountInfo = Omit, "knowledges"> & { + knowledges: Knowledge[] | null; +}; type AccountEmail = Tables<"account_emails">; type AccountWallet = Tables<"account_wallets">; From dbe539fc9a5d67c6826cf22e4850393760fd3238 Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:38:52 +0530 Subject: [PATCH 04/10] Revert "refactor: trust account knowledge response shape" This reverts commit d3d8d3a2b7a6896d8acde12e3827f51777094621. --- hooks/useOrgSettings.ts | 15 +++++---------- lib/supabase/accounts/getAccountWithDetails.ts | 5 +---- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index 94b3ca1b0..45a97437e 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -6,7 +6,7 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import useAccountOrganizations from "./useAccountOrganizations"; import { usePrivy } from "@privy-io/react-auth"; import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile"; -import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; +import { normalizeKnowledges } from "@/lib/accounts/normalizeKnowledges"; import type { Knowledge } from "@/types/Knowledge"; interface OrgData { @@ -17,11 +17,6 @@ interface OrgData { knowledges?: Knowledge[]; } -interface GetAccountResponse { - status: "success"; - account: AccountWithDetails; -} - const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); const { getAccessToken } = usePrivy(); @@ -71,7 +66,7 @@ const useOrgSettings = (orgId: string | null) => { `${getClientApiBaseUrl()}/api/accounts/${orgId}` ); if (response.ok) { - const data: GetAccountResponse = await response.json(); + const data = await response.json(); // Response structure: { status: "success", account: {...} } const account = data.account; setOrgData({ @@ -79,10 +74,10 @@ const useOrgSettings = (orgId: string | null) => { name: account?.name || "", image: account?.image || "", instruction: account?.instruction || "", - knowledges: account?.knowledges || [], + knowledges: normalizeKnowledges(account?.knowledges), }); setInstruction(account?.instruction || ""); - setKnowledges(account?.knowledges || []); + setKnowledges(normalizeKnowledges(account?.knowledges)); } } catch (error) { console.error("Error fetching org details:", error); @@ -169,7 +164,7 @@ const useOrgSettings = (orgId: string | null) => { name: data.name || "", image: data.image || "", instruction: data.instruction || "", - knowledges: data.knowledges || [], + knowledges: normalizeKnowledges(data.knowledges), }); await queryClient.invalidateQueries({ queryKey: ["accountOrganizations"] }); return true; diff --git a/lib/supabase/accounts/getAccountWithDetails.ts b/lib/supabase/accounts/getAccountWithDetails.ts index b62764b9c..f2d43af6b 100644 --- a/lib/supabase/accounts/getAccountWithDetails.ts +++ b/lib/supabase/accounts/getAccountWithDetails.ts @@ -1,11 +1,8 @@ import supabase from "../serverClient"; import { Tables } from "@/types/database.types"; -import type { Knowledge } from "@/types/Knowledge"; type Account = Tables<"accounts">; -type AccountInfo = Omit, "knowledges"> & { - knowledges: Knowledge[] | null; -}; +type AccountInfo = Tables<"account_info">; type AccountEmail = Tables<"account_emails">; type AccountWallet = Tables<"account_wallets">; From 3a0051b6b832c896bbf8b54acbe86cb6069e4b15 Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:39:56 +0530 Subject: [PATCH 05/10] Revert "refactor: share account knowledge types" This reverts commit 2e97b01360767b5c22968e2db30c8b0bd3ac8434. --- hooks/useOrgSettings.ts | 33 +++++++++++++++++++++++----- lib/accounts/normalizeKnowledges.ts | 20 ----------------- lib/accounts/updateAccountProfile.ts | 9 ++++++-- types/Knowledge.ts | 5 ----- 4 files changed, 35 insertions(+), 32 deletions(-) delete mode 100644 lib/accounts/normalizeKnowledges.ts delete mode 100644 types/Knowledge.ts diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index 45a97437e..bfffae471 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -6,17 +6,40 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import useAccountOrganizations from "./useAccountOrganizations"; import { usePrivy } from "@privy-io/react-auth"; import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile"; -import { normalizeKnowledges } from "@/lib/accounts/normalizeKnowledges"; -import type { Knowledge } from "@/types/Knowledge"; + +interface KnowledgeItem { + name: string; + url: string; + type: string; +} interface OrgData { id: string; name: string; image?: string; instruction?: string; - knowledges?: Knowledge[]; + knowledges?: KnowledgeItem[]; } +const normalizeKnowledges = (value: unknown): KnowledgeItem[] => { + if (!Array.isArray(value)) { + return []; + } + + return value.filter((item): item is KnowledgeItem => { + if (!item || typeof item !== "object") { + return false; + } + + const knowledge = item as Partial; + return ( + typeof knowledge.name === "string" && + typeof knowledge.url === "string" && + typeof knowledge.type === "string" + ); + }); +}; + const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); const { getAccessToken } = usePrivy(); @@ -25,7 +48,7 @@ const useOrgSettings = (orgId: string | null) => { const [name, setName] = useState(""); const [image, setImage] = useState(""); const [instruction, setInstruction] = useState(""); - const [knowledges, setKnowledges] = useState([]); + const [knowledges, setKnowledges] = useState([]); const [isLoading, setIsLoading] = useState(false); const [isSaving, setIsSaving] = useState(false); const [imageUploading, setImageUploading] = useState(false); @@ -118,7 +141,7 @@ const useOrgSettings = (orgId: string | null) => { if (!files) return; setKnowledgeUploading(true); - const newKnowledges: Knowledge[] = []; + const newKnowledges: KnowledgeItem[] = []; try { for (const file of files) { const name = file.name; diff --git a/lib/accounts/normalizeKnowledges.ts b/lib/accounts/normalizeKnowledges.ts deleted file mode 100644 index 806568161..000000000 --- a/lib/accounts/normalizeKnowledges.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { Knowledge } from "@/types/Knowledge"; - -export function normalizeKnowledges(value: unknown): Knowledge[] { - if (!Array.isArray(value)) { - return []; - } - - return value.filter((item): item is Knowledge => { - if (!item || typeof item !== "object") { - return false; - } - - const knowledge = item as Partial; - return ( - typeof knowledge.name === "string" && - typeof knowledge.url === "string" && - typeof knowledge.type === "string" - ); - }); -} diff --git a/lib/accounts/updateAccountProfile.ts b/lib/accounts/updateAccountProfile.ts index a7c75ff27..dbc1808be 100644 --- a/lib/accounts/updateAccountProfile.ts +++ b/lib/accounts/updateAccountProfile.ts @@ -1,6 +1,11 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; -import type { Knowledge } from "@/types/Knowledge"; + +type KnowledgeItem = { + name: string; + url: string; + type: string; +}; type UpdateAccountProfileArgs = { accountId: string; @@ -12,7 +17,7 @@ type UpdateAccountProfileArgs = { jobTitle?: string; roleType?: string; companyName?: string; - knowledges?: Knowledge[]; + knowledges?: KnowledgeItem[]; }; type UpdateAccountProfileResponse = { diff --git a/types/Knowledge.ts b/types/Knowledge.ts deleted file mode 100644 index 64b9885d3..000000000 --- a/types/Knowledge.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface Knowledge { - url: string; - name: string; - type: string; -} From a894f5ce80dd3fe533895c2d0bdb4a43e80a1713 Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:46:03 +0530 Subject: [PATCH 06/10] refactor: align org settings response handling --- hooks/useOrgSettings.ts | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index bfffae471..20a5d0250 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -92,13 +92,7 @@ const useOrgSettings = (orgId: string | null) => { const data = await response.json(); // Response structure: { status: "success", account: {...} } const account = data.account; - setOrgData({ - id: account?.id || "", - name: account?.name || "", - image: account?.image || "", - instruction: account?.instruction || "", - knowledges: normalizeKnowledges(account?.knowledges), - }); + setOrgData(account); setInstruction(account?.instruction || ""); setKnowledges(normalizeKnowledges(account?.knowledges)); } @@ -182,13 +176,7 @@ const useOrgSettings = (orgId: string | null) => { instruction, knowledges, }); - setOrgData({ - id: data.id, - name: data.name || "", - image: data.image || "", - instruction: data.instruction || "", - knowledges: normalizeKnowledges(data.knowledges), - }); + setOrgData(data); await queryClient.invalidateQueries({ queryKey: ["accountOrganizations"] }); return true; } finally { From 94299b40e922205a61da01a6a65f03781259429a Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:46:47 +0530 Subject: [PATCH 07/10] refactor: match test branch knowledge handling --- hooks/useOrgSettings.ts | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index 20a5d0250..a4cde19e5 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -21,25 +21,6 @@ interface OrgData { knowledges?: KnowledgeItem[]; } -const normalizeKnowledges = (value: unknown): KnowledgeItem[] => { - if (!Array.isArray(value)) { - return []; - } - - return value.filter((item): item is KnowledgeItem => { - if (!item || typeof item !== "object") { - return false; - } - - const knowledge = item as Partial; - return ( - typeof knowledge.name === "string" && - typeof knowledge.url === "string" && - typeof knowledge.type === "string" - ); - }); -}; - const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); const { getAccessToken } = usePrivy(); @@ -94,7 +75,7 @@ const useOrgSettings = (orgId: string | null) => { const account = data.account; setOrgData(account); setInstruction(account?.instruction || ""); - setKnowledges(normalizeKnowledges(account?.knowledges)); + setKnowledges(account?.knowledges || []); } } catch (error) { console.error("Error fetching org details:", error); From 61bb2978aa283010c839248d28be20ff1d9ac2bb Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:52:52 +0530 Subject: [PATCH 08/10] fix: align org settings state type --- hooks/useOrgSettings.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index a4cde19e5..09bc81329 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -6,6 +6,7 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import useAccountOrganizations from "./useAccountOrganizations"; import { usePrivy } from "@privy-io/react-auth"; import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile"; +import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; interface KnowledgeItem { name: string; @@ -13,19 +14,11 @@ interface KnowledgeItem { type: string; } -interface OrgData { - id: string; - name: string; - image?: string; - instruction?: string; - knowledges?: KnowledgeItem[]; -} - const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); const { getAccessToken } = usePrivy(); const queryClient = useQueryClient(); - const [orgData, setOrgData] = useState(null); + const [orgData, setOrgData] = useState(null); const [name, setName] = useState(""); const [image, setImage] = useState(""); const [instruction, setInstruction] = useState(""); From e60d74b76de9f34ecbefbc29e9452a52c6f466a2 Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:57:51 +0530 Subject: [PATCH 09/10] fix: reduce account helper type surface --- lib/accounts/updateAccountProfile.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/accounts/updateAccountProfile.ts b/lib/accounts/updateAccountProfile.ts index dbc1808be..06dacc850 100644 --- a/lib/accounts/updateAccountProfile.ts +++ b/lib/accounts/updateAccountProfile.ts @@ -1,5 +1,4 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; -import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; type KnowledgeItem = { name: string; @@ -20,17 +19,13 @@ type UpdateAccountProfileArgs = { knowledges?: KnowledgeItem[]; }; -type UpdateAccountProfileResponse = { - data: AccountWithDetails; -}; - /** * Updates an account profile using the dedicated accounts API. */ export async function updateAccountProfile({ accessToken, ...body -}: UpdateAccountProfileArgs): Promise { +}: UpdateAccountProfileArgs) { const response = await fetch(`${getClientApiBaseUrl()}/api/accounts`, { method: "PATCH", headers: { @@ -44,6 +39,6 @@ export async function updateAccountProfile({ throw new Error(`Failed to update account: ${response.status}`); } - const data: UpdateAccountProfileResponse = await response.json(); + const data = await response.json(); return data.data; } From d2cb78b13b366f9cbaa978b6410efb06bae4bb50 Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 7 Apr 2026 03:59:05 +0530 Subject: [PATCH 10/10] refactor: restore org settings local state type --- hooks/useOrgSettings.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hooks/useOrgSettings.ts b/hooks/useOrgSettings.ts index 09bc81329..a4cde19e5 100644 --- a/hooks/useOrgSettings.ts +++ b/hooks/useOrgSettings.ts @@ -6,7 +6,6 @@ import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; import useAccountOrganizations from "./useAccountOrganizations"; import { usePrivy } from "@privy-io/react-auth"; import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile"; -import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails"; interface KnowledgeItem { name: string; @@ -14,11 +13,19 @@ interface KnowledgeItem { type: string; } +interface OrgData { + id: string; + name: string; + image?: string; + instruction?: string; + knowledges?: KnowledgeItem[]; +} + const useOrgSettings = (orgId: string | null) => { const { data: organizations } = useAccountOrganizations(); const { getAccessToken } = usePrivy(); const queryClient = useQueryClient(); - const [orgData, setOrgData] = useState(null); + const [orgData, setOrgData] = useState(null); const [name, setName] = useState(""); const [image, setImage] = useState(""); const [instruction, setInstruction] = useState("");