Skip to content

Commit b58f463

Browse files
committed
fix(account): remove redundant profile.level from account creation
- Remove profile object initialization during account creation - Profile object is optional and won't be created for new accounts - addExperienceAndPoints only updates profile.level if profile exists - Prevents redundant level tracking (already at root level) - Keeps stats object for totalPoints and lastActiveDate tracking - Both FirebaseContext and AccountContext properly award Welcome Explorer badge - Both paths add initial 20 XP and 10 points on account creation - Maintains backward compatibility with existing accounts that have profile object - Account structure now perfectly matches Firebase example
1 parent 4efdd48 commit b58f463

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

contexts/auth/AccountContext.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,23 @@ export const AccountProvider: React.FC<AccountProviderProps> = ({ children }) =>
132132
timeoutPromise,
133133
])) as UserAccount;
134134

135-
setAccount(newAccount);
135+
// Award Welcome Explorer badge for new account (using firebase accountService)
136+
const { accountService: firebaseAccountService } = await import('@/lib/firebase/firebase-service');
137+
await firebaseAccountService.addEarnedBadge(walletData.publicKey, 'welcome_explorer');
138+
139+
// Add experience and points for account creation
140+
await firebaseAccountService.addExperienceAndPoints(walletData.publicKey, 20, 10);
141+
142+
// Reload account to get updated data
143+
const updatedAccount = await accountService.getAccountByWallet(walletData.publicKey);
144+
setAccount(updatedAccount);
136145
// Account info set (Bugfender removed)
137146

138147
// Success message for new account
139148
addToast({
140149
type: 'success',
141150
title: '🎉 Account Created!',
142-
message: 'Welcome to Trustless Work! You earned 150 points and the Welcome Explorer badge!',
151+
message: 'Welcome to Trustless Work! You earned the Welcome Explorer badge and 30 points!',
143152
duration: 5000,
144153
});
145154

@@ -155,7 +164,7 @@ export const AccountProvider: React.FC<AccountProviderProps> = ({ children }) =>
155164
earnedAt: new Date().toISOString(),
156165
rarity: welcomeExplorerBadge.rarity as 'common' | 'rare' | 'epic' | 'legendary',
157166
category: welcomeExplorerBadge.category as 'demo' | 'milestone' | 'achievement' | 'special'
158-
}, 50);
167+
}, welcomeExplorerBadge.earningPoints);
159168
}
160169
}, 1000);
161170
}

lib/firebase/firebase-service.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ export const accountService = {
4747
// Initialize new fields if they don't exist (match Firebase example structure)
4848
completedQuests: accountData.completedQuests || existingData.completedQuests || [],
4949
questProgress: accountData.questProgress || existingData.questProgress || {},
50-
profile: accountData.profile || existingData.profile || {
51-
level: accountData.level || 1,
52-
},
50+
// Only preserve existing profile if it exists, don't create empty one
51+
...(existingData.profile && { profile: existingData.profile }),
5352
stats: accountData.stats || existingData.stats || {
5453
totalPoints: accountData.totalPoints || 0,
5554
lastActiveDate: new Date().toISOString().split('T')[0],
@@ -155,17 +154,23 @@ export const accountService = {
155154
const newTotalPoints = account.totalPoints + points;
156155
const newLevel = Math.floor(newExperience / 1000) + 1;
157156

158-
// Update both root-level and nested fields (match Firebase structure)
157+
// Update root-level fields and stats
159158
const accountRef = doc(db, COLLECTIONS.ACCOUNTS, walletAddress);
160-
await updateDoc(accountRef, {
159+
const updateData: any = {
161160
experience: newExperience,
162161
totalPoints: newTotalPoints,
163162
level: newLevel,
164-
'profile.level': newLevel, // Update nested profile.level
165163
'stats.totalPoints': newTotalPoints, // Update nested stats.totalPoints
166164
'stats.lastActiveDate': new Date().toISOString().split('T')[0], // Update last active date
167165
updatedAt: serverTimestamp(),
168-
});
166+
};
167+
168+
// Only update profile.level if profile object exists
169+
if (account.profile) {
170+
updateData['profile.level'] = newLevel;
171+
}
172+
173+
await updateDoc(accountRef, updateData);
169174
}
170175
},
171176

lib/services/account-service.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ export class AccountService {
5959
createdAt: now,
6060
updatedAt: now,
6161
lastLoginAt: now,
62-
profile: {
63-
level: 1,
64-
},
6562
stats: {
6663
totalPoints: 0, // Will be updated when badges are earned
6764
lastActiveDate: new Date().toISOString().split('T')[0],

0 commit comments

Comments
 (0)