From 12127ff4ef59b01923c726929adf8fb2b75012c1 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Tue, 30 Sep 2025 22:54:48 +0000 Subject: [PATCH] Remove mock fallback from updateUserXPAndLevel cgen-32de4238eaaa40f789eac3bbc17d4fe1 --- client/lib/aethex-database-adapter.ts | 90 +++++++++++++-------------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/client/lib/aethex-database-adapter.ts b/client/lib/aethex-database-adapter.ts index 9371a65f..cdd95618 100644 --- a/client/lib/aethex-database-adapter.ts +++ b/client/lib/aethex-database-adapter.ts @@ -598,6 +598,8 @@ export const aethexAchievementService = { }, async updateUserXPAndLevel(userId: string, xpGained: number): Promise { + ensureSupabase(); + try { const { data: profile, error } = await supabase .from("user_profiles") @@ -605,58 +607,52 @@ export const aethexAchievementService = { .eq("id", userId) .single(); - if (!error && profile) { - const newTotalXP = ((profile as any).total_xp || 0) + xpGained; - const newLevel = Math.floor(newTotalXP / 1000) + 1; - const newLoyaltyPoints = - ((profile as any).loyalty_points || 0) + xpGained; - - const updates: any = {}; - if ("total_xp" in (profile as any)) updates.total_xp = newTotalXP; - if ("level" in (profile as any)) updates.level = newLevel; - if ("loyalty_points" in (profile as any)) - updates.loyalty_points = newLoyaltyPoints; - - if (Object.keys(updates).length > 0) { - await supabase.from("user_profiles").update(updates).eq("id", userId); - } - - if (newLevel > ((profile as any).level || 1) && newLevel >= 5) { - // Try to find Level Master by name, fall back to default id - try { - const { data } = await supabase - .from("achievements") - .select("id") - .eq("name", "Level Master") - .single(); - const id = (data as any)?.id || "ach_level_master"; - await this.awardAchievement(userId, id); - } catch { - await this.awardAchievement(userId, "ach_level_master"); - } + if (error) { + if (isTableMissing(error)) { + throw new Error( + "Supabase table \"user_profiles\" is missing. Please run the required migrations.", + ); } + console.warn("Unable to load profile for XP update:", error); return; } - } catch {} - // Local fallback using mock profile persistence - try { - const current = await mockAuth.getUserProfile(userId as any); - const total_xp = ((current as any)?.total_xp || 0) + xpGained; - const level = Math.floor(total_xp / 1000) + 1; - const loyalty_points = ((current as any)?.loyalty_points || 0) + xpGained; - await mockAuth.updateProfile( - userId as any, - { - total_xp, - level, - loyalty_points, - } as any, - ); - if (level > ((current as any)?.level || 1) && level >= 5) { - await this.awardAchievement(userId, "ach_level_master"); + if (!profile) { + console.warn("No profile found while updating XP for", userId); + return; } - } catch {} + + const currentProfile: any = profile; + const newTotalXP = (currentProfile.total_xp || 0) + xpGained; + const newLevel = Math.floor(newTotalXP / 1000) + 1; + const newLoyaltyPoints = (currentProfile.loyalty_points || 0) + xpGained; + + const updates: Record = {}; + if ("total_xp" in currentProfile) updates.total_xp = newTotalXP; + if ("level" in currentProfile) updates.level = newLevel; + if ("loyalty_points" in currentProfile) + updates.loyalty_points = newLoyaltyPoints; + + if (Object.keys(updates).length > 0) { + await supabase.from("user_profiles").update(updates).eq("id", userId); + } + + if (newLevel > (currentProfile.level || 1) && newLevel >= 5) { + try { + const { data } = await supabase + .from("achievements") + .select("id") + .eq("name", "Level Master") + .single(); + const id = (data as any)?.id || "ach_level_master"; + await this.awardAchievement(userId, id); + } catch { + await this.awardAchievement(userId, "ach_level_master"); + } + } + } catch (error) { + console.warn("Failed to update XP and level:", error); + } }, async checkAndAwardOnboardingAchievement(userId: string): Promise {