Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update backfill-program-application-status.ts

Steven Tey1c81fbf1ed52
diff --git a/apps/web/scripts/migrations/backfill-program-application-status.ts b/apps/web/scripts/migrations/backfill-program-application-status.ts
index 53b6ecb467..d15164e025 100644
--- a/apps/web/scripts/migrations/backfill-program-application-status.ts
+++ b/apps/web/scripts/migrations/backfill-program-application-status.ts
@@ -641,6 +641,80 @@ async function backfillRejectedWithoutEnrollment() {
console.table(totals);
}
+async function unsetPartnerIdForUnclaimedApplications() {
+ console.log("Unsetting partnerId for unclaimed applications");
+
+ const totals = {
+ scanned: 0,
+ unclaimed: 0,
+ };
+
+ let cursor: string | undefined;
+
+ while (true) {
+ const applications = await prisma.programApplication.findMany({
+ where: {
+ status: ProgramApplicationStatus.pending,
+ enrollment: null,
+ partnerId: {
+ not: null,
+ },
+ ...(cursor && {
+ id: {
+ gt: cursor,
+ },
+ }),
+ },
+ select: {
+ id: true,
+ partnerId: true,
+ },
+ orderBy: {
+ id: "asc",
+ },
+ take: BATCH_SIZE,
+ });
+
+ if (applications.length === 0) {
+ break;
+ }
+
+ totals.scanned += applications.length;
+ cursor = applications[applications.length - 1].id;
+
+ const unclaimedApplications = applications.filter(
+ ({ partnerId }) => !partnerId,
+ );
+
+ if (unclaimedApplications.length > 0) {
+ console.table(unclaimedApplications.slice(0, 20));
+ }
+
+ if (DRY_RUN) {
+ totals.unclaimed += unclaimedApplications.length;
+ } else {
+ const { count } = await prisma.programApplication.updateMany({
+ where: {
+ id: {
+ in: unclaimedApplications.map(({ id }) => id),
+ },
+ },
+ data: {
+ partnerId: null,
+ },
+ });
+
+ totals.unclaimed += count;
+ }
+
+ console.log(
+ `${DRY_RUN ? "Would process" : "Processed"} ${applications.length} applications up to ${cursor}`,
+ );
+ }
+
+ console.table(totals);
+}
+
async function main() {
console.log(`DRY_RUN=${DRY_RUN} BATCH_SIZE=${BATCH_SIZE}`);
@@ -650,7 +724,7 @@ async function main() {
// await backfillPartnerIdsByEmail();
// await backfillFromActivityLogs();
// await backfillRejectedWithoutEnrollment();
-
+ // await unsetPartnerIdForUnclaimedApplications();
console.log("Finished.");
}