()
| 32 | const BATCHES_PER_REQUEST = 50; |
| 33 | |
| 34 | export async function POST(): Promise< |
| 35 | NextResponse<NormalizedEmailBackfillResponse | { error: string }> |
| 36 | > { |
| 37 | const { authFailedResponse } = await getUserFromAuth({ adminOnly: true }); |
| 38 | if (authFailedResponse) return authFailedResponse; |
| 39 | |
| 40 | let totalProcessed = 0; |
| 41 | |
| 42 | for (let i = 0; i < BATCHES_PER_REQUEST; i++) { |
| 43 | const rows = await db |
| 44 | .select({ id: kilocode_users.id, google_user_email: kilocode_users.google_user_email }) |
| 45 | .from(kilocode_users) |
| 46 | .where(isNull(kilocode_users.normalized_email)) |
| 47 | .limit(BATCH_SIZE); |
| 48 | |
| 49 | if (rows.length === 0) break; |
| 50 | |
| 51 | const updates = rows.map(row => ({ |
| 52 | id: row.id, |
| 53 | normalized_email: normalizeEmail(row.google_user_email), |
| 54 | })); |
| 55 | |
| 56 | await db.execute(sql` |
| 57 | UPDATE ${kilocode_users} |
| 58 | SET normalized_email = email_updates.normalized_email |
| 59 | FROM (VALUES ${sql.join( |
| 60 | updates.map(u => sql`(${u.id}, ${u.normalized_email})`), |
| 61 | sql`, ` |
| 62 | )}) AS email_updates(id, normalized_email) |
| 63 | WHERE ${kilocode_users.id} = email_updates.id |
| 64 | `); |
| 65 | |
| 66 | totalProcessed += rows.length; |
| 67 | |
| 68 | if (rows.length < BATCH_SIZE) break; |
| 69 | } |
| 70 | |
| 71 | return NextResponse.json({ |
| 72 | processed: totalProcessed, |
| 73 | remaining: totalProcessed === BATCH_SIZE * BATCHES_PER_REQUEST, |
| 74 | }); |
| 75 | } |
nothing calls this directly
no test coverage detected