From 9609e266ce346e1106d1522892c934284883245f Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Mon, 17 Nov 2025 02:50:29 +0000 Subject: [PATCH] completionId: cgen-6cbf9a545d894ec395b227551a54e127 cgen-6cbf9a545d894ec395b227551a54e127 --- api/creators.ts | 70 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/api/creators.ts b/api/creators.ts index 20d918f2..a8e5935a 100644 --- a/api/creators.ts +++ b/api/creators.ts @@ -216,21 +216,65 @@ export async function updateCreatorProfile(req: Request, userId: string) { spotify_profile_url, } = body; + // Validate required fields if provided + if (username !== undefined && username !== null) { + if (typeof username !== "string" || username.trim().length === 0) { + return new Response( + JSON.stringify({ error: "Username must be a non-empty string" }), + { status: 400, headers: { "Content-Type": "application/json" } }, + ); + } + + // Check if username is being changed to something that already exists + const { data: currentCreator } = await supabase + .from("aethex_creators") + .select("username") + .eq("user_id", userId) + .single(); + + const normalizedUsername = username.trim().toLowerCase(); + + if ( + currentCreator && + currentCreator.username !== normalizedUsername + ) { + // Username is being changed, check if new username exists + const { data: existingCreator } = await supabase + .from("aethex_creators") + .select("id") + .eq("username", normalizedUsername) + .single(); + + if (existingCreator) { + return new Response( + JSON.stringify({ error: "Username is already taken" }), + { status: 409, headers: { "Content-Type": "application/json" } }, + ); + } + } + } + + const updateData: any = { + updated_at: new Date().toISOString(), + }; + + // Only update fields that are provided + if (username !== undefined && username !== null) { + updateData.username = username.trim().toLowerCase(); + } + if (bio !== undefined) updateData.bio = bio; + if (skills !== undefined) updateData.skills = skills; + if (avatar_url !== undefined) updateData.avatar_url = avatar_url; + if (experience_level !== undefined) updateData.experience_level = experience_level; + if (primary_arm !== undefined) updateData.primary_arm = primary_arm; + if (arm_affiliations !== undefined) updateData.arm_affiliations = arm_affiliations; + if (is_discoverable !== undefined) updateData.is_discoverable = is_discoverable; + if (allow_recommendations !== undefined) updateData.allow_recommendations = allow_recommendations; + if (spotify_profile_url !== undefined) updateData.spotify_profile_url = spotify_profile_url; + const { data, error } = await supabase .from("aethex_creators") - .update({ - username, - bio, - skills, - avatar_url, - experience_level, - primary_arm, - arm_affiliations, - is_discoverable, - allow_recommendations, - spotify_profile_url, - updated_at: new Date().toISOString(), - }) + .update(updateData) .eq("user_id", userId) .select() .single();