Skip to content

Commit

Permalink
Merge branch 'multi-links' of https://github.com/dubinc/dub into mult…
Browse files Browse the repository at this point in the history
…i-links
  • Loading branch information
steven-tey committed Jan 31, 2025
2 parents 74aa7c7 + 0749784 commit 8f7e8bc
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions apps/web/app/api/partners/[partnerId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DubApiError } from "@/lib/api/errors";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { withWorkspace } from "@/lib/auth";
import { EnrolledPartnerSchema } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
Expand All @@ -19,11 +18,6 @@ export const GET = withWorkspace(
});
}

await getProgramOrThrow({
workspaceId: workspace.id,
programId,
});

const programEnrollment = await prisma.programEnrollment.findUniqueOrThrow({
where: {
partnerId_programId: {
Expand All @@ -38,8 +32,27 @@ export const GET = withWorkspace(
},
});

if (programEnrollment.program.workspaceId !== workspace.id) {
throw new DubApiError({
code: "not_found",
message: "Program not found.",
});
}

const { program, links } = programEnrollment;

const totalClicks = links?.reduce((acc, link) => {
return acc + (link?.clicks ?? 0);
}, 0);

const totalLeads = links?.reduce((acc, link) => {
return acc + (link?.leads ?? 0);
}, 0);

const totalSales = links?.reduce((acc, link) => {
return acc + (link?.sales ?? 0);
}, 0);

const earnings = links?.reduce((acc, link) => {
return (
acc +
Expand All @@ -55,6 +68,9 @@ export const GET = withWorkspace(
...programEnrollment,
id: programEnrollment.partnerId,
earnings,
clicks: totalClicks,
leads: totalLeads,
sales: totalSales,
};

return NextResponse.json(EnrolledPartnerSchema.parse(partner));
Expand Down

0 comments on commit 8f7e8bc

Please sign in to comment.