edit local user fix

This commit is contained in:
echo 2026-03-18 12:37:21 +01:00
parent bb9c675421
commit 7043487fc2
4 changed files with 35 additions and 24 deletions

Binary file not shown.

View File

@ -387,7 +387,6 @@ export async function PUT(request: NextRequest) {
// Update Clerk publicMetadata (role/gymId) to propagate via webhook // Update Clerk publicMetadata (role/gymId) to propagate via webhook
// Note: Only update metadata when a change is requested // Note: Only update metadata when a change is requested
try {
const client = await clerkClient(); const client = await clerkClient();
const publicMetadata: Record<string, unknown> = {}; const publicMetadata: Record<string, unknown> = {};
log.debug("Preparing Clerk metadata update", { log.debug("Preparing Clerk metadata update", {
@ -405,6 +404,10 @@ export async function PUT(request: NextRequest) {
if (Object.keys(publicMetadata).length > 0) { if (Object.keys(publicMetadata).length > 0) {
log.debug("Updating Clerk user metadata", { publicMetadata }); log.debug("Updating Clerk user metadata", { publicMetadata });
try {
// Check if user exists in Clerk first
await client.users.getUser(id);
const clerkResult = await client.users.updateUser(id, { const clerkResult = await client.users.updateUser(id, {
publicMetadata, publicMetadata,
}); });
@ -413,14 +416,20 @@ export async function PUT(request: NextRequest) {
role: clerkResult.publicMetadata?.role, role: clerkResult.publicMetadata?.role,
gymId: clerkResult.publicMetadata?.gymId, gymId: clerkResult.publicMetadata?.gymId,
}); });
} else {
log.debug("No Clerk metadata changes requested");
}
} catch (clerkErr: any) { } catch (clerkErr: any) {
log.error("Clerk metadata update failed", clerkErr, { userId: id }); // User might not exist in Clerk yet - that's OK for local users
return internalErrorResponse( if (
"Failed to update role/gym in identity provider", clerkErr?.status === 404 ||
clerkErr?.errors?.[0]?.code === "resource_not_found"
) {
log.debug(
"User not found in Clerk, skipping metadata update (local-only user)",
); );
} else {
log.error("Clerk metadata update failed", clerkErr, { userId: id });
// Don't fail the whole request - local DB update can still proceed
}
}
} }
// Update local DB for immediate UI feedback (webhook will also sync) // Update local DB for immediate UI feedback (webhook will also sync)

View File

@ -26,6 +26,7 @@ import {
type InvitationOptionsData, type InvitationOptionsData,
} from "@/lib/validation/user-schemas"; } from "@/lib/validation/user-schemas";
import { ChevronLeft, ChevronRight, Check } from "lucide-react"; import { ChevronLeft, ChevronRight, Check } from "lucide-react";
import { useGyms } from "@/hooks/use-api";
interface CreateUserModalProps { interface CreateUserModalProps {
open: boolean; open: boolean;
@ -45,6 +46,7 @@ export function CreateUserModal({
const [basicInfo, setBasicInfo] = useState<BasicInfoData | null>(null); const [basicInfo, setBasicInfo] = useState<BasicInfoData | null>(null);
const [clientInfo, setClientInfo] = useState<ClientInfoData | null>(null); const [clientInfo, setClientInfo] = useState<ClientInfoData | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false);
const { data: gyms = [] } = useGyms();
// Step 1: Role Selection Form // Step 1: Role Selection Form
const roleForm = useForm<RoleSelectionData>({ const roleForm = useForm<RoleSelectionData>({

View File

@ -170,7 +170,7 @@ export function useGyms() {
queryKey: ["gyms"], queryKey: ["gyms"],
queryFn: () => queryFn: () =>
fetchApi<{ data: { gyms: Gym[] } }>("/api/gyms").then( fetchApi<{ data: { gyms: Gym[] } }>("/api/gyms").then(
(res) => res.data?.gyms ?? [], (res) => res.data?.gyms ?? (Array.isArray(res) ? res : []),
), ),
}); });
} }