Skip to content

Commit 9154ef2

Browse files
authored
Merge pull request dubinc#2409 from dubinc/default-program-id
Add workspace.defaultProgramId
2 parents 668cc4a + 836b620 commit 9154ef2

32 files changed

Lines changed: 204 additions & 195 deletions

File tree

apps/web/app/api/commissions/count/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
2-
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
2+
import { DubApiError } from "@/lib/api/errors";
33
import { withWorkspace } from "@/lib/auth";
44
import { getCommissionsCountQuerySchema } from "@/lib/zod/schemas/commissions";
55
import { prisma } from "@dub/prisma";
@@ -10,10 +10,12 @@ import { NextResponse } from "next/server";
1010
export const GET = withWorkspace(async ({ workspace, searchParams }) => {
1111
const { programId } = searchParams;
1212

13-
await getProgramOrThrow({
14-
workspaceId: workspace.id,
15-
programId,
16-
});
13+
if (programId !== workspace.defaultProgramId) {
14+
throw new DubApiError({
15+
code: "not_found",
16+
message: "Program not found",
17+
});
18+
}
1719

1820
const {
1921
status,

apps/web/app/api/commissions/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
2-
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
2+
import { DubApiError } from "@/lib/api/errors";
33
import { withWorkspace } from "@/lib/auth";
44
import {
55
CommissionResponseSchema,
@@ -13,10 +13,12 @@ import { z } from "zod";
1313
export const GET = withWorkspace(async ({ workspace, searchParams }) => {
1414
const { programId } = searchParams;
1515

16-
await getProgramOrThrow({
17-
workspaceId: workspace.id,
18-
programId,
19-
});
16+
if (programId !== workspace.defaultProgramId) {
17+
throw new DubApiError({
18+
code: "not_found",
19+
message: "Program not found",
20+
});
21+
}
2022

2123
const {
2224
status,

apps/web/app/api/partners/[partnerId]/route.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { DubApiError } from "@/lib/api/errors";
22
import { getPartnerForProgram } from "@/lib/api/partners/get-partner-for-program";
3-
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
43
import { withWorkspace } from "@/lib/auth";
54
import { EnrolledPartnerSchemaWithExpandedFields } from "@/lib/zod/schemas/partners";
65
import { NextResponse } from "next/server";
@@ -19,16 +18,17 @@ export const GET = withWorkspace(
1918
});
2019
}
2120

22-
const [_program, partner] = await Promise.all([
23-
getProgramOrThrow({
24-
workspaceId: workspace.id,
25-
programId,
26-
}),
27-
getPartnerForProgram({
28-
programId,
29-
partnerId,
30-
}),
31-
]);
21+
if (programId !== workspace.defaultProgramId) {
22+
throw new DubApiError({
23+
code: "not_found",
24+
message: "Program not found",
25+
});
26+
}
27+
28+
const partner = await getPartnerForProgram({
29+
programId,
30+
partnerId,
31+
});
3232

3333
if (!partner)
3434
throw new DubApiError({

apps/web/app/api/partners/export/route.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { convertToCSV } from "@/lib/analytics/utils/convert-to-csv";
2+
import { DubApiError } from "@/lib/api/errors";
23
import { getPartners } from "@/lib/api/partners/get-partners";
34
import { withWorkspace } from "@/lib/auth";
45
import {
@@ -17,6 +18,13 @@ export const GET = withWorkspace(
1718
async ({ searchParams, workspace }) => {
1819
const { programId } = searchParams;
1920

21+
if (programId !== workspace.defaultProgramId) {
22+
throw new DubApiError({
23+
code: "not_found",
24+
message: "Program not found",
25+
});
26+
}
27+
2028
let { columns, ...filters } = partnersExportQuerySchema.parse(searchParams);
2129

2230
const partners = await getPartners({

apps/web/app/api/partners/links/route.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ export const GET = withWorkspace(
2222
const { programId, partnerId, tenantId } =
2323
retrievePartnerLinksSchema.parse(searchParams);
2424

25-
await getProgramOrThrow({
26-
programId,
27-
workspaceId: workspace.id,
28-
});
25+
if (programId !== workspace.defaultProgramId) {
26+
throw new DubApiError({
27+
code: "not_found",
28+
message: "Program not found",
29+
});
30+
}
2931

3032
const programEnrollment = await prisma.programEnrollment.findUnique({
3133
where: partnerId

apps/web/app/api/partners/route.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export const GET = withWorkspace(
2727
});
2828
}
2929

30+
if (programId !== workspace.defaultProgramId) {
31+
throw new DubApiError({
32+
code: "not_found",
33+
message: "Program not found",
34+
});
35+
}
36+
3037
const partners = await getPartners({
3138
...partnersQuerySchema.parse(searchParams),
3239
workspaceId: workspace.id,

apps/web/app/api/partners/sales/route.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { DubApiError } from "@/lib/api/errors";
2-
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
32
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
43
import { parseRequestBody } from "@/lib/api/utils";
54
import { withWorkspace } from "@/lib/auth/workspace";
@@ -17,15 +16,17 @@ export const PATCH = withWorkspace(
1716
let { programId, invoiceId, amount, modifyAmount, currency } =
1817
updatePartnerSaleSchema.parse(await parseRequestBody(req));
1918

20-
const program = await getProgramOrThrow({
21-
workspaceId: workspace.id,
22-
programId,
23-
});
19+
if (programId !== workspace.defaultProgramId) {
20+
throw new DubApiError({
21+
code: "not_found",
22+
message: "Program not found",
23+
});
24+
}
2425

2526
const commission = await prisma.commission.findUnique({
2627
where: {
2728
programId_invoiceId: {
28-
programId: program.id,
29+
programId,
2930
invoiceId,
3031
},
3132
},
@@ -72,13 +73,13 @@ export const PATCH = withWorkspace(
7273
const reward = await determinePartnerReward({
7374
event: "sale",
7475
partnerId: partner.id,
75-
programId: program.id,
76+
programId: programId,
7677
});
7778

7879
if (!reward) {
7980
throw new DubApiError({
8081
code: "not_found",
81-
message: `No reward found for partner ${partner.id} in program ${program.id}.`,
82+
message: `No reward found for partner ${partner.id} in program ${programId}.`,
8283
});
8384
}
8485

apps/web/app/api/programs/[programId]/applications/[applicationId]/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
1+
import { DubApiError } from "@/lib/api/errors";
22
import { withWorkspace } from "@/lib/auth";
33
import { prisma } from "@dub/prisma";
44
import { NextResponse } from "next/server";
55

66
export const GET = withWorkspace(async ({ workspace, params }) => {
7-
await getProgramOrThrow({
8-
workspaceId: workspace.id,
9-
programId: params.programId,
10-
});
7+
if (params.programId !== workspace.defaultProgramId) {
8+
throw new DubApiError({
9+
code: "not_found",
10+
message: "Program not found",
11+
});
12+
}
1113

1214
const application = await prisma.programApplication.findUnique({
1315
where: { id: params.applicationId },

apps/web/app/api/programs/[programId]/discounts/partners/route.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { DubApiError } from "@/lib/api/errors";
12
import { getDiscountOrThrow } from "@/lib/api/partners/get-discount-or-throw";
2-
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
33
import { withWorkspace } from "@/lib/auth";
44
import { discountPartnersQuerySchema } from "@/lib/zod/schemas/discount";
55
import { prisma } from "@dub/prisma";
@@ -9,19 +9,19 @@ import { NextResponse } from "next/server";
99
export const GET = withWorkspace(
1010
async ({ workspace, params, searchParams }) => {
1111
const { programId } = params;
12-
const { discountId } = discountPartnersQuerySchema.parse(searchParams);
12+
if (programId !== workspace.defaultProgramId) {
13+
throw new DubApiError({
14+
code: "not_found",
15+
message: "Program not found",
16+
});
17+
}
1318

14-
await Promise.all([
15-
getProgramOrThrow({
16-
workspaceId: workspace.id,
17-
programId,
18-
}),
19+
const { discountId } = discountPartnersQuerySchema.parse(searchParams);
1920

20-
getDiscountOrThrow({
21-
programId,
22-
discountId,
23-
}),
24-
]);
21+
await getDiscountOrThrow({
22+
programId,
23+
discountId,
24+
});
2525

2626
const partners = await prisma.programEnrollment.findMany({
2727
where: {

apps/web/app/api/programs/[programId]/discounts/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
1+
import { DubApiError } from "@/lib/api/errors";
22
import { withWorkspace } from "@/lib/auth";
33
import { DiscountSchema } from "@/lib/zod/schemas/discount";
44
import { prisma } from "@dub/prisma";
@@ -9,10 +9,12 @@ import { z } from "zod";
99
export const GET = withWorkspace(async ({ workspace, params }) => {
1010
const { programId } = params;
1111

12-
await getProgramOrThrow({
13-
workspaceId: workspace.id,
14-
programId,
15-
});
12+
if (programId !== workspace.defaultProgramId) {
13+
throw new DubApiError({
14+
code: "not_found",
15+
message: "Program not found",
16+
});
17+
}
1618

1719
const discounts = await prisma.discount.findMany({
1820
where: {

0 commit comments

Comments
 (0)