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.

add back /rewards and /discounts for now

Steven Teyaa10ec44c7ca
diff --git a/apps/web/app/(ee)/api/programs/[programId]/discounts/route.ts b/apps/web/app/(ee)/api/programs/[programId]/discounts/route.ts
new file mode 100644
index 0000000000..502c80c589
--- /dev/null
+++ b/apps/web/app/(ee)/api/programs/[programId]/discounts/route.ts
@@ -0,0 +1,37 @@
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { withWorkspace } from "@/lib/auth";
+import { DiscountSchema } from "@/lib/zod/schemas/discount";
+import { prisma } from "@dub/prisma";
+import { NextResponse } from "next/server";
+import { z } from "zod";
+
+// TODO: Remove once we migrate fully to partner groups
+// GET /api/programs/[programId]/discounts - get all discounts for a program
+export const GET = withWorkspace(async ({ workspace }) => {
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const discounts = await prisma.discount.findMany({
+ where: {
+ programId,
+ },
+ include: {
+ _count: {
+ select: {
+ programEnrollments: true,
+ },
+ },
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ const discountsWithPartnersCount = discounts.map((discount) => ({
+ ...discount,
+ partnersCount: discount._count.programEnrollments,
+ }));
+
+ return NextResponse.json(
+ z.array(DiscountSchema).parse(discountsWithPartnersCount),
+ );
+});
diff --git a/apps/web/app/(ee)/api/programs/[programId]/rewards/route.ts b/apps/web/app/(ee)/api/programs/[programId]/rewards/route.ts
new file mode 100644
index 0000000000..c7a6e70e82
--- /dev/null
+++ b/apps/web/app/(ee)/api/programs/[programId]/rewards/route.ts
@@ -0,0 +1,28 @@
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { withWorkspace } from "@/lib/auth";
+import { RewardSchema } from "@/lib/zod/schemas/rewards";
+import { prisma } from "@dub/prisma";
+import { NextResponse } from "next/server";
+import { z } from "zod";
+
+// TODO: Remove once we migrate fully to partner groups
+// GET /api/programs/[programId]/rewards - get all rewards for a program
+export const GET = withWorkspace(async ({ workspace }) => {
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const rewards = await prisma.reward.findMany({
+ where: {
+ programId,
+ },
+ orderBy: [
+ {
+ event: "desc",
+ },
+ {
+ createdAt: "desc",
+ },
+ ],
+ });
+
+ return NextResponse.json(z.array(RewardSchema).parse(rewards));
+});
diff --git a/apps/web/scripts/migrations/migrate-partners-with-tenantids.ts b/apps/web/scripts/migrations/migrate-partners-with-tenantids.ts
new file mode 100644
index 0000000000..1c7f577867
--- /dev/null
+++ b/apps/web/scripts/migrations/migrate-partners-with-tenantids.ts
@@ -0,0 +1,31 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+
+async function main() {
+ const group = await prisma.partnerGroup.findUniqueOrThrow({
+ where: {
+ id: "grp_xxx",
+ },
+ });
+
+ const users = await prisma.programEnrollment.updateMany({
+ where: {
+ programId: group.programId,
+ tenantId: {
+ not: null,
+ },
+ applicationId: null,
+ },
+ data: {
+ groupId: group.id,
+ saleRewardId: group.saleRewardId,
+ leadRewardId: group.leadRewardId,
+ clickRewardId: group.clickRewardId,
+ discountId: group.discountId,
+ },
+ });
+
+ console.log(users);
+}
+
+main();