fixed weird signin bug

This commit is contained in:
George Powell
2026-02-12 20:24:38 -05:00
parent 290fb06fe9
commit f6652e59a7
5 changed files with 335 additions and 119 deletions

View File

@@ -42,54 +42,42 @@ export const actions: Actions = {
// Migrate anonymous stats if different anonymous ID
if (anonymousId && anonymousId !== user.id) {
try {
// Update all daily completions from the local anonymous ID to the user's ID
await db
.update(dailyCompletions)
.set({ anonymousId: user.id })
// Get completions for both the anonymous ID and the user ID
const anonCompletions = await db
.select()
.from(dailyCompletions)
.where(eq(dailyCompletions.anonymousId, anonymousId));
console.log(`Migrated stats from ${anonymousId} to ${user.id}`);
// Deduplicate any entries for the same date after migration
const allUserCompletions = await db
const userCompletions = await db
.select()
.from(dailyCompletions)
.where(eq(dailyCompletions.anonymousId, user.id));
// Group by date to find duplicates
const dateGroups = new Map<string, typeof allUserCompletions>();
for (const completion of allUserCompletions) {
const date = completion.date;
if (!dateGroups.has(date)) {
dateGroups.set(date, []);
}
dateGroups.get(date)!.push(completion);
}
// Create a set of dates the user already has completions for
const userDates = new Set(userCompletions.map(c => c.date));
// Process dates with duplicates
const duplicateIds: string[] = [];
for (const [date, completions] of dateGroups) {
if (completions.length > 1) {
// Sort by completedAt timestamp (earliest first)
completions.sort((a, b) => a.completedAt.getTime() - b.completedAt.getTime());
// Keep the first (earliest), mark the rest for deletion
const toDelete = completions.slice(1);
duplicateIds.push(...toDelete.map(c => c.id));
console.log(`Found ${completions.length} duplicates for date ${date}, keeping earliest, deleting ${toDelete.length}`);
let migrated = 0;
let skipped = 0;
// Migrate only non-conflicting completions
for (const completion of anonCompletions) {
if (!userDates.has(completion.date)) {
// No conflict - safe to migrate
await db
.update(dailyCompletions)
.set({ anonymousId: user.id })
.where(eq(dailyCompletions.id, completion.id));
migrated++;
} else {
// Conflict exists - delete the anonymous completion (keep user's existing one)
await db
.delete(dailyCompletions)
.where(eq(dailyCompletions.id, completion.id));
skipped++;
}
}
// Delete duplicate entries
if (duplicateIds.length > 0) {
await db
.delete(dailyCompletions)
.where(inArray(dailyCompletions.id, duplicateIds));
console.log(`Deleted ${duplicateIds.length} duplicate completion entries`);
}
console.log(`Migration complete: ${migrated} moved, ${skipped} duplicates removed`);
} catch (error) {
console.error('Error migrating anonymous stats:', error);
// Don't fail the signin if stats migration fails