Fix updateUserXPAndLevel to handle missing database fields gracefully
cgen-d3549eb875f84ca286f89b1db2d35a35
This commit is contained in:
parent
854ac3b37e
commit
1d737acd6b
1 changed files with 17 additions and 11 deletions
|
|
@ -331,27 +331,33 @@ export const aethexAchievementService = {
|
||||||
|
|
||||||
async updateUserXPAndLevel(userId: string, xpGained: number): Promise<void> {
|
async updateUserXPAndLevel(userId: string, xpGained: number): Promise<void> {
|
||||||
// Get current user data
|
// Get current user data
|
||||||
const { data: profile } = await supabase
|
const { data: profile, error } = await supabase
|
||||||
.from("user_profiles")
|
.from("user_profiles")
|
||||||
.select("total_xp, level, loyalty_points")
|
.select("total_xp, level, loyalty_points")
|
||||||
.eq("id", userId)
|
.eq("id", userId)
|
||||||
.single();
|
.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 newTotalXP = (profile.total_xp || 0) + xpGained;
|
||||||
const newLevel = Math.floor(newTotalXP / 1000) + 1; // 1000 XP per level
|
const newLevel = Math.floor(newTotalXP / 1000) + 1; // 1000 XP per level
|
||||||
const newLoyaltyPoints = (profile.loyalty_points || 0) + xpGained;
|
const newLoyaltyPoints = (profile.loyalty_points || 0) + xpGained;
|
||||||
|
|
||||||
// Update profile
|
// Update profile (only update existing fields)
|
||||||
await supabase
|
const updates: any = {};
|
||||||
.from("user_profiles")
|
if ('total_xp' in profile) updates.total_xp = newTotalXP;
|
||||||
.update({
|
if ('level' in profile) updates.level = newLevel;
|
||||||
total_xp: newTotalXP,
|
if ('loyalty_points' in profile) updates.loyalty_points = newLoyaltyPoints;
|
||||||
level: newLevel,
|
|
||||||
loyalty_points: newLoyaltyPoints,
|
if (Object.keys(updates).length > 0) {
|
||||||
})
|
await supabase
|
||||||
.eq("id", userId);
|
.from("user_profiles")
|
||||||
|
.update(updates)
|
||||||
|
.eq("id", userId);
|
||||||
|
}
|
||||||
|
|
||||||
// Check for level-up achievements
|
// Check for level-up achievements
|
||||||
if (newLevel > (profile.level || 1)) {
|
if (newLevel > (profile.level || 1)) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue