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-commission-attributes.ts

Kiran K8fabd29e5401
diff --git a/apps/web/scripts/migrate-commission-attributes.ts b/apps/web/scripts/migrate-commission-attributes.ts
index 558143ce02..189ae45224 100644
--- a/apps/web/scripts/migrate-commission-attributes.ts
+++ b/apps/web/scripts/migrate-commission-attributes.ts
@@ -5,10 +5,17 @@ import { prisma } from "@dub/prisma";
import { EventType } from "@prisma/client";
import "dotenv-flow/config";
+// Migrate the commission attributes from Program table to Reward table.
+
async function main() {
const programs = await prisma.program.findMany();
for (const program of programs) {
+ const maxDuration =
+ program.commissionInterval === "year"
+ ? program.commissionDuration * 12
+ : program.commissionDuration;
+
const reward = await prisma.reward.create({
data: {
id: createId({ prefix: "rew_" }),
@@ -16,21 +23,22 @@ async function main() {
event: EventType.sale,
type: program.commissionType,
amount: program.commissionAmount,
- duration: program.commissionDuration,
- interval: program.commissionInterval,
+ maxDuration,
createdAt: program.createdAt,
updatedAt: program.updatedAt,
},
});
- await prisma.program.update({
- where: {
- id: reward.programId,
- },
- data: {
- defaultRewardId: reward.id,
- },
- });
+ if (!program.defaultRewardId) {
+ await prisma.program.update({
+ where: {
+ id: reward.programId,
+ },
+ data: {
+ defaultRewardId: reward.id,
+ },
+ });
+ }
console.log(`Migrated commission attributes for program ${program.id}`);
}