From 1d737acd6bb10af3ba30646e678a4fbb9e775409 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sat, 16 Aug 2025 04:12:10 +0000 Subject: [PATCH] Fix updateUserXPAndLevel to handle missing database fields gracefully cgen-d3549eb875f84ca286f89b1db2d35a35 --- client/lib/aethex-database-adapter.ts | 28 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/client/lib/aethex-database-adapter.ts b/client/lib/aethex-database-adapter.ts index 3fb93945..b0067ef2 100644 --- a/client/lib/aethex-database-adapter.ts +++ b/client/lib/aethex-database-adapter.ts @@ -331,27 +331,33 @@ export const aethexAchievementService = { async updateUserXPAndLevel(userId: string, xpGained: number): Promise { // Get current user data - const { data: profile } = await supabase + const { data: profile, error } = await supabase .from("user_profiles") .select("total_xp, level, loyalty_points") .eq("id", userId) .single(); - if (!profile) return; + if (error || !profile) { + console.log("Profile not found or missing XP fields, skipping XP update"); + return; + } const newTotalXP = (profile.total_xp || 0) + xpGained; const newLevel = Math.floor(newTotalXP / 1000) + 1; // 1000 XP per level const newLoyaltyPoints = (profile.loyalty_points || 0) + xpGained; - // Update profile - await supabase - .from("user_profiles") - .update({ - total_xp: newTotalXP, - level: newLevel, - loyalty_points: newLoyaltyPoints, - }) - .eq("id", userId); + // Update profile (only update existing fields) + const updates: any = {}; + if ('total_xp' in profile) updates.total_xp = newTotalXP; + if ('level' in profile) updates.level = newLevel; + if ('loyalty_points' in profile) updates.loyalty_points = newLoyaltyPoints; + + if (Object.keys(updates).length > 0) { + await supabase + .from("user_profiles") + .update(updates) + .eq("id", userId); + } // Check for level-up achievements if (newLevel > (profile.level || 1)) {