MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / POST

Function POST

apps/web/src/app/admin/api/backfills/normalized-email/route.ts:34–75  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

32const BATCHES_PER_REQUEST = 50;
33
34export 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}

Callers

nothing calls this directly

Calls 7

getUserFromAuthFunction · 0.90
normalizeEmailFunction · 0.90
executeMethod · 0.80
joinMethod · 0.80
limitMethod · 0.65
jsonMethod · 0.65
sqlFunction · 0.50

Tested by

no test coverage detected