The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
More changes to the script
Kiran K3e92e202195b
diff --git a/apps/web/scripts/migrations/backfill-program-application-status.ts b/apps/web/scripts/migrations/backfill-program-application-status.tsindex e8fd723bd8..4c1d95ce0d 100644--- a/apps/web/scripts/migrations/backfill-program-application-status.ts+++ b/apps/web/scripts/migrations/backfill-program-application-status.ts@@ -1,4 +1,3 @@-import { getAuditLogs } from "@/lib/api/audit-logs/get-audit-logs";import { getApplicationStatusFromEnrollment } from "@/lib/partners/get-application-status-from-enrollment";import { prisma } from "@/lib/prisma";import { ProgramApplicationStatus } from "@prisma/client";@@ -10,9 +9,6 @@ const BATCH_SIZE = 500;// Max gap between an application's reviewedAt and the activity log written by the same reviewconst REVIEW_LOG_MATCH_WINDOW_MS = 10 * 60 * 1000;-// Approve/reject wrote Tinybird audit logs until #3746 (2026-04-13) replaced them with ActivityLog rows-const ACTIVITY_LOGS_INTRODUCED_AT = new Date("2026-04-14");-const REVIEW_LOG_ACTIONS = {"partner_application.approved": ProgramApplicationStatus.approved,"partner_application.rejected": ProgramApplicationStatus.rejected,@@ -49,24 +45,6 @@ function findClosestDecision(return closest ? REVIEW_LOG_ACTIONS[closest.action] : null;}-// Tinybird returns DateTime64 as "YYYY-MM-DD HH:mm:ss.SSS" in UTC, without a timezone suffix-function parseTinybirdTimestamp(timestamp: string) {- return new Date(- /[zZ]|[+-]\d{2}:?\d{2}$/.test(timestamp)- ? timestamp- : `${timestamp.replace(" ", "T")}Z`,- );-}--function getAuditLogPartnerId(targets: string) {- try {- const parsed = JSON.parse(targets) as { type?: string; id?: string }[];- return parsed.find(({ type }) => type === "partner")?.id ?? null;- } catch {- return null;- }-}-// Pass 1: applications linked to an enrollment take their status and partnerId from itasync function backfillFromEnrollments() {console.log("Pass 1: applications with an enrollment");@@ -578,20 +556,15 @@ async function backfillFromActivityLogs() {console.table(totals);}-// Pass 5: reviewed applications from before ActivityLog existed take the decision from the-// Tinybird audit log written by the same review (matched by programId, partnerId, and closest to reviewedAt).-// dub_audit_logs has a 1-year TTL, so this has to run before the 2026-03-30+ logs expire.-async function backfillFromAuditLogs() {+// Pass 5: reviewed applications still pending without an enrollment were rejected, or their partner was removed from the program+async function backfillRejectedWithoutEnrollment() {console.log(- "Pass 5: reviewed applications without an enrollment, from the Tinybird audit log",+ "Pass 5: reviewed applications without an enrollment and no matching activity log",);const totals = {scanned: 0,- approved: 0,rejected: 0,- noPartnerId: 0,- noMatchingAuditLog: 0,};let cursor: string | undefined;@@ -608,15 +581,10 @@ async function backfillFromAuditLogs() {status: ProgramApplicationStatus.pending,reviewedAt: {not: null,- lt: ACTIVITY_LOGS_INTRODUCED_AT,},},select: {id: true,- programId: true,- partnerId: true,- email: true,- reviewedAt: true,},orderBy: {id: "asc",@@ -631,156 +599,22 @@ async function backfillFromAuditLogs() {totals.scanned += applications.length;cursor = applications[applications.length - 1].id;- const partnerIdByEmail = await getPartnerIdsByEmail(- applications- .filter(({ partnerId }) => !partnerId)- .map(({ email }) => email),- );-- const candidates = applications.flatMap((application) => {- const partnerId =- application.partnerId ??- partnerIdByEmail.get(application.email.toLowerCase());-- if (!partnerId || !application.reviewedAt) {- totals.noPartnerId++;- return [];- }-- return [- {- ...application,- partnerId,- reviewedAt: application.reviewedAt,- },- ];- });-- const candidatesByProgramId = new Map<string, typeof candidates>();-- for (const candidate of candidates) {- candidatesByProgramId.set(candidate.programId, [- ...(candidatesByProgramId.get(candidate.programId) ?? []),- candidate,- ]);- }-- const programs = await prisma.program.findMany({- where: {- id: {- in: [...candidatesByProgramId.keys()],- },- },- select: {- id: true,- workspaceId: true,- },- });-- const logsByKey = new Map<string, { action: string; createdAt: Date }[]>();-- for (const program of programs) {- const programCandidates = candidatesByProgramId.get(program.id) ?? [];-- const reviewedAtTimes = programCandidates.map(({ reviewedAt }) =>- reviewedAt.getTime(),- );-- // TODO:- // We can add a custom pipe to do this- const auditLogs = await getAuditLogs({- workspaceId: program.workspaceId,- programId: program.id,- start: new Date(- Math.min(...reviewedAtTimes) - REVIEW_LOG_MATCH_WINDOW_MS,- ),- end: new Date(- Math.max(...reviewedAtTimes) + REVIEW_LOG_MATCH_WINDOW_MS,- ),- });-- for (const auditLog of auditLogs) {- if (!isReviewLogAction(auditLog.action)) {- continue;- }-- const partnerId = getAuditLogPartnerId(auditLog.targets);-- if (!partnerId) {- continue;- }-- const key = `${program.id}:${partnerId}`;-- logsByKey.set(key, [- ...(logsByKey.get(key) ?? []),- {- action: auditLog.action,- createdAt: parseTinybirdTimestamp(auditLog.timestamp),- },- ]);- }- }-- const idsByStatus: Record<ProgramApplicationStatus, string[]> = {- pending: [],- approved: [],- rejected: [],- };-- const unmatched: typeof candidates = [];-- for (const candidate of candidates) {- const status = findClosestDecision(- logsByKey.get(`${candidate.programId}:${candidate.partnerId}`) ?? [],- candidate.reviewedAt,- );-- if (!status) {- unmatched.push(candidate);- continue;- }-- idsByStatus[status].push(candidate.id);- }-- totals.noMatchingAuditLog += unmatched.length;-- if (unmatched.length > 0) {- console.log(- `${unmatched.length} reviewed applications have no matching audit log`,- );- console.table(unmatched.slice(0, 20));- }-- for (const status of [- ProgramApplicationStatus.approved,- ProgramApplicationStatus.rejected,- ]) {- const ids = idsByStatus[status];-- if (ids.length === 0) {- continue;- }-- if (DRY_RUN) {- totals[status] += ids.length;- continue;- }-+ if (DRY_RUN) {+ totals.rejected += applications.length;+ } else {const { count } = await prisma.programApplication.updateMany({where: {id: {- in: ids,+ in: applications.map(({ id }) => id),},status: ProgramApplicationStatus.pending,},data: {- status,+ status: ProgramApplicationStatus.rejected,},});- totals[status] += count;+ totals.rejected += count;}console.log(@@ -795,11 +629,11 @@ async function main() {console.log(`DRY_RUN=${DRY_RUN} BATCH_SIZE=${BATCH_SIZE}`);// In order:- await backfillFromEnrollments();- await backfillRejectedFromRejectionFields();- await backfillPartnerIdsByEmail();- await backfillFromActivityLogs();- await backfillFromAuditLogs();+ // await backfillFromEnrollments();+ // await backfillRejectedFromRejectionFields();+ // await backfillPartnerIdsByEmail();+ // await backfillFromActivityLogs();+ // await backfillRejectedWithoutEnrollment();console.log("Finished.");}