This commit is contained in:
zramsay 2025-03-11 18:06:43 -04:00
parent b9390f8f8c
commit fc07185ecd

View File

@ -53,6 +53,26 @@ export async function initializeUser(userId: string, email: string) {
console.log('Creating new user in points system:', userIdUUID, email);
// Create new user entry - using RPC function with admin client to bypass RLS
console.log(`Attempting to create user with ID: ${userIdUUID} and email: ${email}`);
// Direct insert is more reliable than RPC in this case
const { data: directInsert, error: insertError } = await supabaseAdmin
.from('user_points')
.insert({
user_id: userIdUUID,
email: email,
total_points: 0
})
.select()
.single();
if (!insertError) {
console.log('Successfully created user via direct insert:', directInsert);
return directInsert;
}
// Try RPC as fallback if direct insert fails
console.log('Trying RPC create_user_points as fallback');
const { data: newUser, error } = await supabaseAdmin
.rpc('create_user_points', {
user_id_param: userIdUUID,
@ -113,6 +133,12 @@ export async function awardPointsForImage(
message: 'Missing user data, cannot award points'
};
}
// If userId is an email, use it for deterministic UUID generation
if (userId.includes('@') && email) {
console.log('userId is an email, using it for consistent UUID generation');
userId = email; // Use email for consistency
}
// Convert userId to UUID format
let userIdUUID: string;
@ -277,6 +303,11 @@ export async function getUserPoints(userId: string) {
return 0;
}
// If this is an email address, note it for debugging
if (userId.includes('@')) {
console.log('Note: Email address used as user ID for points:', userId);
}
// Convert to UUID
let userIdUUID: string;
try {
@ -304,10 +335,18 @@ export async function getUserPoints(userId: string) {
// If we got an empty result or a not found error, try to initialize the user
if (!userExists || (checkError && checkError.code === 'PGRST116')) {
// Get the user's email from the points page - we might not have it yet
// We'll use a dummy email if we can't get the real one
const userIdForEmail = validateUuid(userId) ? userId : userId.substring(0, 8);
const email = userIdForEmail + '@example.com'; // Fallback email
// Check if the userId is an email
let email;
if (userId.includes('@')) {
email = userId; // Use the userId as email if it is one
console.log('Using email from userId for initialization:', email);
} else {
// Use a dummy email if we don't have a real one
const userIdForEmail = validateUuid(userId) ? userId : userId.substring(0, 8);
email = userIdForEmail + '@example.com'; // Fallback email
console.log('Using dummy email for initialization:', email);
}
await initializeUser(userId, email);
}
} catch (initError) {
@ -467,6 +506,11 @@ export async function getUserTransactions(userId: string) {
return [];
}
// If this is an email address, note it for consistency
if (userId.includes('@')) {
console.log('Note: Email address used as user ID for transactions:', userId);
}
// Convert to UUID
let userIdUUID: string;
try {