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 migrate-rewards-remainder.ts

Steven Tey724730598245
diff --git a/apps/web/scripts/migrate-rewards-remainder.ts b/apps/web/scripts/migrate-rewards-remainder.ts
index ec77d3c975..c1b9d77e3c 100644
--- a/apps/web/scripts/migrate-rewards-remainder.ts
+++ b/apps/web/scripts/migrate-rewards-remainder.ts
@@ -1,6 +1,11 @@
import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
+/*
+ Follow-up to scripts/migrate-rewards.ts
+ This script migrates the rewards remainder for program enrollments that don't have a reward.
+ It does so by finding the default reward for the program and assigning it to the enrollment.
+*/
async function main() {
const programEnrollmentsWithNoReward =
await prisma.programEnrollment.findMany({
@@ -11,7 +16,73 @@ async function main() {
},
});
- console.table(programEnrollmentsWithNoReward);
+ console.table(programEnrollmentsWithNoReward, [
+ "id",
+ "programId",
+ "partnerId",
+ "clickRewardId",
+ "leadRewardId",
+ "saleRewardId",
+ ]);
+
+ const programDefaultRewards = await prisma.reward.findMany({
+ where: {
+ programId: {
+ in: programEnrollmentsWithNoReward.map(
+ (enrollment) => enrollment.programId,
+ ),
+ },
+ default: true,
+ },
+ });
+
+ console.table(programDefaultRewards, [
+ "id",
+ "programId",
+ "event",
+ "type",
+ "amount",
+ ]);
+
+ const updatedEnrollments = programEnrollmentsWithNoReward.map(
+ (enrollment) => ({
+ ...enrollment,
+ clickRewardId: programDefaultRewards.find(
+ (reward) =>
+ reward.event === "click" && reward.programId === enrollment.programId,
+ )?.id,
+ leadRewardId: programDefaultRewards.find(
+ (reward) =>
+ reward.event === "lead" && reward.programId === enrollment.programId,
+ )?.id,
+ saleRewardId: programDefaultRewards.find(
+ (reward) =>
+ reward.event === "sale" && reward.programId === enrollment.programId,
+ )?.id,
+ }),
+ );
+
+ console.table(updatedEnrollments, [
+ "id",
+ "programId",
+ "partnerId",
+ "clickRewardId",
+ "leadRewardId",
+ "saleRewardId",
+ ]);
+
+ await Promise.all(
+ updatedEnrollments.map((enrollment) =>
+ prisma.programEnrollment.update({
+ where: { id: enrollment.id },
+ data: {
+ clickRewardId: enrollment.clickRewardId,
+ leadRewardId: enrollment.leadRewardId,
+ saleRewardId: enrollment.saleRewardId,
+ },
+ }),
+ ),
+ );
}
main();