Skip to content

Commit 1a7aa46

Browse files
author
Deepak Pandey
committed
fix: Use profileService for consistent database operations
- Replace direct database queries with profileService.getProfile() and profileService.updateProfile() - Use same approach as protected area for consistent error handling - Automatically create profile if it doesn't exist (same as protected area) - Fix TypeScript errors by using proper ProfileUpdateData interface - Separate completion status update from basic profile update - Improve error handling and user feedback This ensures the complete-profile page works exactly like the protected area
1 parent 77cd35a commit 1a7aa46

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

app/complete-profile/page.tsx

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { toast } from 'sonner';
88
import CodeuniaLogo from '@/components/codeunia-logo';
99
import { InputValidator } from '@/lib/security/input-validation';
1010
import { CheckCircle, XCircle, AlertCircle, Loader2, Sparkles } from 'lucide-react';
11+
import { profileService } from '@/lib/services/profile';
1112

1213
interface User {
1314
id: string;
@@ -47,29 +48,30 @@ export default function CompleteProfile() {
4748
}
4849
setUser(user);
4950

50-
// Check if profile already exists and is complete
51-
const { data: profile } = await getSupabaseClient()
52-
.from('profiles')
53-
.select('first_name, last_name, username, profile_complete')
54-
.eq('id', user.id)
55-
.single();
56-
57-
if (profile) {
58-
const isProfileComplete = profile.first_name &&
59-
profile.last_name &&
60-
profile.username &&
61-
profile.profile_complete;
51+
// Check if profile already exists and is complete using profileService
52+
try {
53+
const profile = await profileService.getProfile(user.id);
6254

63-
if (isProfileComplete) {
64-
// Profile is already complete, redirect to dashboard
65-
router.push('/protected/dashboard');
66-
return;
55+
if (profile) {
56+
const isProfileComplete = profile.first_name &&
57+
profile.last_name &&
58+
profile.username &&
59+
profile.profile_complete;
60+
61+
if (isProfileComplete) {
62+
// Profile is already complete, redirect to dashboard
63+
router.push('/protected/dashboard');
64+
return;
65+
}
66+
67+
// Pre-fill existing data
68+
if (profile.first_name) setFirstName(profile.first_name);
69+
if (profile.last_name) setLastName(profile.last_name);
70+
if (profile.username) setUsername(profile.username);
6771
}
68-
69-
// Pre-fill existing data
70-
if (profile.first_name) setFirstName(profile.first_name);
71-
if (profile.last_name) setLastName(profile.last_name);
72-
if (profile.username) setUsername(profile.username);
72+
} catch (profileError) {
73+
console.error('Error checking profile:', profileError);
74+
// Continue with the form - profileService will handle creation if needed
7375
}
7476

7577
// Pre-fill from OAuth provider data if available
@@ -186,30 +188,32 @@ export default function CompleteProfile() {
186188

187189
setIsLoading(true);
188190
try {
189-
// Update profile with the provided information using upsert to handle missing profiles
190-
const { data: upserted, error } = await getSupabaseClient()
191+
// First update the basic profile information using profileService
192+
const updatedProfile = await profileService.updateProfile(user.id, {
193+
first_name: firstName.trim(),
194+
last_name: lastName.trim(),
195+
username: username.trim()
196+
});
197+
198+
if (!updatedProfile) {
199+
console.error('Profile update failed: No data returned');
200+
toast.error('Failed to update profile. Please try again.');
201+
return;
202+
}
203+
204+
// Then update the completion status fields directly
205+
const { error: completionError } = await getSupabaseClient()
191206
.from('profiles')
192-
.upsert([{
193-
id: user.id,
194-
first_name: firstName.trim(),
195-
last_name: lastName.trim(),
196-
username: username.trim(),
207+
.update({
197208
profile_complete: true,
198209
username_set: true,
199210
username_editable: false
200-
}], { onConflict: 'id' })
201-
.select('id')
202-
.single();
203-
204-
if (error) {
205-
console.error('Profile update error:', error);
206-
toast.error(error.message || 'Failed to update profile. Please try again.');
207-
return;
208-
}
211+
})
212+
.eq('id', user.id);
209213

210-
if (!upserted) {
211-
console.error('Profile update failed: No data returned');
212-
toast.error('Failed to update profile. Please try again.');
214+
if (completionError) {
215+
console.error('Error updating completion status:', completionError);
216+
toast.error('Profile updated but completion status failed. Please try again.');
213217
return;
214218
}
215219

0 commit comments

Comments
 (0)