Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions apps/web/app/api/commissions/count/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { getCommissionsCountQuerySchema } from "@/lib/zod/schemas/commissions";
import { prisma } from "@dub/prisma";
Expand All @@ -10,10 +10,12 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace, searchParams }) => {
const { programId } = searchParams;

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

const {
status,
Expand Down
12 changes: 7 additions & 5 deletions apps/web/app/api/commissions/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import {
CommissionResponseSchema,
Expand All @@ -13,10 +13,12 @@ import { z } from "zod";
export const GET = withWorkspace(async ({ workspace, searchParams }) => {
const { programId } = searchParams;

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

const {
status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export async function sendStripePayouts({
sendEmail({
subject: "You've been paid!",
email: payout.partner.email,
from: "Dub Partners <system@dub.co>",
react: PartnerPayoutSent({
email: payout.partner.email,
program: payout.program,
Expand Down
22 changes: 11 additions & 11 deletions apps/web/app/api/partners/[partnerId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
import { getPartnerForProgram } from "@/lib/api/partners/get-partner-for-program";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { withWorkspace } from "@/lib/auth";
import { EnrolledPartnerSchemaWithExpandedFields } from "@/lib/zod/schemas/partners";
import { NextResponse } from "next/server";
Expand All @@ -19,16 +18,17 @@ export const GET = withWorkspace(
});
}

const [_program, partner] = await Promise.all([
getProgramOrThrow({
workspaceId: workspace.id,
programId,
}),
getPartnerForProgram({
programId,
partnerId,
}),
]);
if (programId !== workspace.defaultProgramId) {
throw new DubApiError({
code: "not_found",
message: "Program not found",
});
}

const partner = await getPartnerForProgram({
programId,
partnerId,
});

if (!partner)
throw new DubApiError({
Expand Down
8 changes: 8 additions & 0 deletions apps/web/app/api/partners/export/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { convertToCSV } from "@/lib/analytics/utils/convert-to-csv";
import { DubApiError } from "@/lib/api/errors";
import { getPartners } from "@/lib/api/partners/get-partners";
import { withWorkspace } from "@/lib/auth";
import {
Expand All @@ -17,6 +18,13 @@ export const GET = withWorkspace(
async ({ searchParams, workspace }) => {
const { programId } = searchParams;

if (programId !== workspace.defaultProgramId) {
throw new DubApiError({
code: "not_found",
message: "Program not found",
});
}

let { columns, ...filters } = partnersExportQuerySchema.parse(searchParams);

const partners = await getPartners({
Expand Down
10 changes: 6 additions & 4 deletions apps/web/app/api/partners/links/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export const GET = withWorkspace(
const { programId, partnerId, tenantId } =
retrievePartnerLinksSchema.parse(searchParams);

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

const programEnrollment = await prisma.programEnrollment.findUnique({
where: partnerId
Expand Down
7 changes: 7 additions & 0 deletions apps/web/app/api/partners/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export const GET = withWorkspace(
});
}

if (programId !== workspace.defaultProgramId) {
throw new DubApiError({
code: "not_found",
message: "Program not found",
});
}

const partners = await getPartners({
...partnersQuerySchema.parse(searchParams),
workspaceId: workspace.id,
Expand Down
17 changes: 9 additions & 8 deletions apps/web/app/api/partners/sales/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 { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth/workspace";
Expand All @@ -17,15 +16,17 @@ export const PATCH = withWorkspace(
let { programId, invoiceId, amount, modifyAmount, currency } =
updatePartnerSaleSchema.parse(await parseRequestBody(req));

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

const commission = await prisma.commission.findUnique({
where: {
programId_invoiceId: {
programId: program.id,
programId,
invoiceId,
},
},
Expand Down Expand Up @@ -72,13 +73,13 @@ export const PATCH = withWorkspace(
const reward = await determinePartnerReward({
event: "sale",
partnerId: partner.id,
programId: program.id,
programId: programId,
});

if (!reward) {
throw new DubApiError({
code: "not_found",
message: `No reward found for partner ${partner.id} in program ${program.id}.`,
message: `No reward found for partner ${partner.id} in program ${programId}.`,
});
}

Expand Down
1 change: 0 additions & 1 deletion apps/web/app/api/paypal/webhook/payout-status-changed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export async function payoutStatusChanged(event: any) {
sendEmail({
subject: "You've been paid!",
email: payout.partner.email,
from: "Dub Partners <system@dub.co>",
react: PartnerPayoutSent({
email: payout.partner.email,
program: payout.program,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";

export const GET = withWorkspace(async ({ workspace, params }) => {
await getProgramOrThrow({
workspaceId: workspace.id,
programId: params.programId,
});
if (params.programId !== workspace.defaultProgramId) {
throw new DubApiError({
code: "not_found",
message: "Program not found",
});
}

const application = await prisma.programApplication.findUnique({
where: { id: params.applicationId },
Expand Down
24 changes: 12 additions & 12 deletions apps/web/app/api/programs/[programId]/discounts/partners/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
import { getDiscountOrThrow } from "@/lib/api/partners/get-discount-or-throw";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { withWorkspace } from "@/lib/auth";
import { discountPartnersQuerySchema } from "@/lib/zod/schemas/discount";
import { prisma } from "@dub/prisma";
Expand All @@ -9,19 +9,19 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(
async ({ workspace, params, searchParams }) => {
const { programId } = params;
const { discountId } = discountPartnersQuerySchema.parse(searchParams);
if (programId !== workspace.defaultProgramId) {
throw new DubApiError({
code: "not_found",
message: "Program not found",
});
}

await Promise.all([
getProgramOrThrow({
workspaceId: workspace.id,
programId,
}),
const { discountId } = discountPartnersQuerySchema.parse(searchParams);

getDiscountOrThrow({
programId,
discountId,
}),
]);
await getDiscountOrThrow({
programId,
discountId,
});

const partners = await prisma.programEnrollment.findMany({
where: {
Expand Down
12 changes: 7 additions & 5 deletions apps/web/app/api/programs/[programId]/discounts/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { DiscountSchema } from "@/lib/zod/schemas/discount";
import { prisma } from "@dub/prisma";
Expand All @@ -9,10 +9,12 @@ import { z } from "zod";
export const GET = withWorkspace(async ({ workspace, params }) => {
const { programId } = params;

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

const discounts = await prisma.discount.findMany({
where: {
Expand Down
17 changes: 10 additions & 7 deletions apps/web/app/api/programs/[programId]/metrics/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import {
getProgramMetricsQuerySchema,
Expand All @@ -11,17 +11,20 @@ import { NextResponse } from "next/server";
// GET /api/programs/[programId]/metrics - get metrics for a program
export const GET = withWorkspace(
async ({ workspace, params, searchParams }) => {
const { programId } = params;
const { interval, start, end } =
getProgramMetricsQuerySchema.parse(searchParams);
const { startDate, endDate } = getStartEndDates({ interval, start, end });

const program = await getProgramOrThrow({
workspaceId: workspace.id,
programId: params.programId,
});
if (programId !== workspace.defaultProgramId) {
throw new DubApiError({
code: "not_found",
message: "Program not found",
});
}

const where = {
programId: program.id,
programId,
createdAt: {
gte: startDate.toISOString(),
lte: endDate.toISOString(),
Expand Down Expand Up @@ -53,7 +56,7 @@ export const GET = withWorkspace(

prisma.programEnrollment.count({
where: {
programId: program.id,
programId,
status: "approved",
},
}),
Expand Down
12 changes: 7 additions & 5 deletions apps/web/app/api/programs/[programId]/resources/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { programResourcesSchema } from "@/lib/zod/schemas/program-resources";
import { prisma } from "@dub/prisma";
Expand All @@ -8,10 +8,12 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace, params }) => {
const { programId } = params;

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

const program = await prisma.program.findUnique({
where: {
Expand Down
24 changes: 12 additions & 12 deletions apps/web/app/api/programs/[programId]/rewards/partners/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
import { getRewardOrThrow } from "@/lib/api/partners/get-reward-or-throw";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { withWorkspace } from "@/lib/auth";
import { rewardPartnersQuerySchema } from "@/lib/zod/schemas/rewards";
import { prisma } from "@dub/prisma";
Expand All @@ -9,19 +9,19 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(
async ({ workspace, params, searchParams }) => {
const { programId } = params;
const { rewardId } = rewardPartnersQuerySchema.parse(searchParams);
if (programId !== workspace.defaultProgramId) {
throw new DubApiError({
code: "not_found",
message: "Program not found",
});
}

await Promise.all([
getProgramOrThrow({
workspaceId: workspace.id,
programId,
}),
const { rewardId } = rewardPartnersQuerySchema.parse(searchParams);

getRewardOrThrow({
rewardId,
programId,
}),
]);
await getRewardOrThrow({
rewardId,
programId,
});

const partners = await prisma.partnerReward.findMany({
where: {
Expand Down
Loading