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.

fix tests, update script

Steven Teyc0b9c4608651
diff --git a/apps/web/scripts/migrate-discounts.ts b/apps/web/scripts/migrate-discounts.ts
index 0cd49ce7b9..5f106724ca 100644
--- a/apps/web/scripts/migrate-discounts.ts
+++ b/apps/web/scripts/migrate-discounts.ts
@@ -5,9 +5,32 @@ import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
async function main() {
+ const nonDefaultDiscounts = await prisma.discount.findMany({
+ where: {
+ defaultForProgram: null,
+ },
+ include: {
+ _count: {
+ select: {
+ programEnrollments: true,
+ },
+ },
+ },
+ });
+ console.table(nonDefaultDiscounts, ["id", "programId", "_count"]);
+ console.log(
+ `Found ${nonDefaultDiscounts.reduce(
+ (acc, discount) => acc + discount._count.programEnrollments,
+ 0,
+ )} program enrollments with non-default discounts`,
+ );
+
// Migrate program-wide discounts
const programDiscounts = await prisma.discount.findMany({
where: {
+ defaultForProgram: {
+ isNot: null,
+ },
default: false,
},
select: {
@@ -15,7 +38,6 @@ async function main() {
programId: true,
},
});
- console.log({ programDiscounts });
const finalProgramDiscounts = programDiscounts.map((discount) => {
return {
@@ -27,34 +49,34 @@ async function main() {
console.table(finalProgramDiscounts);
// Update the default column in the Reward table
- const res1 = await prisma.discount.updateMany({
- where: {
- id: {
- in: finalProgramDiscounts.map((discount) => discount.discountId),
- },
- },
- data: {
- default: true,
- },
- });
+ // const res1 = await prisma.discount.updateMany({
+ // where: {
+ // id: {
+ // in: finalProgramDiscounts.map((discount) => discount.discountId),
+ // },
+ // },
+ // data: {
+ // default: true,
+ // },
+ // });
- console.log({ res1 });
+ // console.log({ res1 });
- for (const discount of finalProgramDiscounts) {
- const discountId = discount.discountId;
+ // for (const discount of finalProgramDiscounts) {
+ // const discountId = discount.discountId;
- const res2 = await prisma.programEnrollment.updateMany({
- where: {
- programId: discount.programId,
- discountId: null,
- },
- data: {
- discountId,
- },
- });
+ // const res2 = await prisma.programEnrollment.updateMany({
+ // where: {
+ // programId: discount.programId,
+ // discountId: null, // only update if the discountId is null
+ // },
+ // data: {
+ // discountId,
+ // },
+ // });
- console.log({ res2 });
- }
+ // console.log({ res2 });
+ // }
}
main();
diff --git a/apps/web/tests/utils/resource.ts b/apps/web/tests/utils/resource.ts
index bac7d66694..aa64f255eb 100644
--- a/apps/web/tests/utils/resource.ts
+++ b/apps/web/tests/utils/resource.ts
@@ -50,6 +50,7 @@ export const E2E_DISCOUNT = {
maxDuration: 3,
couponId: "XZuejd0Q",
couponTestId: "2NMXz81x",
+ default: true,
description: null,
};
diff --git a/packages/prisma/schema/discount.prisma b/packages/prisma/schema/discount.prisma
index c0b38b896e..a43e11064d 100644
--- a/packages/prisma/schema/discount.prisma
+++ b/packages/prisma/schema/discount.prisma
@@ -14,5 +14,8 @@ model Discount {
programEnrollments ProgramEnrollment[]
program Program @relation("ProgramDiscounts", fields: [programId], references: [id], onDelete: Cascade, onUpdate: Cascade)
+ // TODO: remove after https://github.com/dubinc/dub/pull/2611 + https://github.com/dubinc/dub/pull/2595 merges
+ defaultForProgram Program? @relation("ProgramDefaultDiscount")
+
@@index(programId)
}
diff --git a/packages/prisma/schema/program.prisma b/packages/prisma/schema/program.prisma
index 9c173e0525..6d77ce9312 100644
--- a/packages/prisma/schema/program.prisma
+++ b/packages/prisma/schema/program.prisma
@@ -38,6 +38,10 @@ model Program {
holdingPeriodDays Int @default(0) // number of days to wait before earnings are added to a payout
minPayoutAmount Int @default(10000) // Default minimum payout amount of $100
+ // TODO: remove after https://github.com/dubinc/dub/pull/2611 + https://github.com/dubinc/dub/pull/2595 merges
+ defaultDiscountId String? @unique // default discount for the program
+ defaultDiscount Discount? @relation("ProgramDefaultDiscount", fields: [defaultDiscountId], references: [id], onDelete: NoAction, onUpdate: NoAction)
+
embedData Json? @db.Json
landerData Json? @db.Json
landerPublishedAt DateTime?