The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Keep submitted bounties visible and centralize partner eligibility checks.
Kiran K2bd1c3da6208
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.tsindex bebc88a9dd..59f4d90137 100644--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.ts+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.ts@@ -1,16 +1,15 @@import { DubApiError } from "@/lib/api/errors";import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";import { withPartnerProfile } from "@/lib/auth/partner";-import { getEffectiveBountyPeriod } from "@/lib/bounty/api/bounty-availability";+import {+ canPartnerSeeBounty,+ getEffectiveBountyPeriod,+} from "@/lib/bounty/api/bounty-availability";import { getBountyOrThrow } from "@/lib/bounty/api/get-bounty-or-throw";-import { isBountyExpired, isBountyStarted } from "@/lib/bounty/bounty-period";import { aggregatePartnerLinksStats } from "@/lib/partners/aggregate-partner-links-stats";import { PartnerBountySchema } from "@/lib/zod/schemas/partner-profile";import { NextResponse } from "next/server";-// TODO:-// Allow partners to see a bounty if they have a submission-// GET /api/partner-profile/programs/[programId]/bounties/[bountyId] – get a single bounty for an enrolled programexport const GET = withPartnerProfile(async ({ partner, params }) => {const { programId, bountyId } = params;@@ -20,8 +19,21 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {partnerId: partner.id,programId,include: {- program: true,- links: true,+ program: {+ select: {+ id: true,+ defaultGroupId: true,+ },+ },+ links: {+ select: {+ clicks: true,+ leads: true,+ conversions: true,+ sales: true,+ saleAmount: true,+ },+ },},});@@ -53,13 +65,13 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {},});- const partnerGroupId = programEnrollment.groupId || program.defaultGroupId;- const bountyGroupIds = bounty.groups.map((g) => g.groupId);- const partnerCanSeeBounty =- bountyGroupIds.length === 0 ||- (partnerGroupId && bountyGroupIds.includes(partnerGroupId));+ const canSeeBounty = canPartnerSeeBounty({+ program,+ bounty,+ programEnrollment,+ });- if (!partnerCanSeeBounty) {+ if (!canSeeBounty) {throw new DubApiError({code: "not_found",message: "Bounty not found.",@@ -71,13 +83,6 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {bounty,});- if (!isBountyStarted(startsAt) || isBountyExpired(endsAt)) {- throw new DubApiError({- code: "not_found",- message: "Bounty not found.",- });- }-const { groups, ...bountyWithoutGroups } = bounty;return NextResponse.json(diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/social-content-stats/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/social-content-stats/route.tsindex 124e659a1f..013741d7bc 100644--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/social-content-stats/route.ts+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/social-content-stats/route.ts@@ -2,6 +2,7 @@ import { DubApiError } from "@/lib/api/errors";import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";import { getSocialContent } from "@/lib/api/scrape-creators/get-social-content";import { withPartnerProfile } from "@/lib/auth/partner";+import { canPartnerSubmitBounty } from "@/lib/bounty/api/bounty-availability";import { getBountyOrThrow } from "@/lib/bounty/api/get-bounty-or-throw";import { resolveBountyDetails } from "@/lib/bounty/utils";import { ratelimit } from "@/lib/upstash";@@ -30,16 +31,44 @@ export const GET = withPartnerProfile(});}- const programEnrollment = await getProgramEnrollmentOrThrow({- partnerId: partner.id,- programId,- include: {},- });+ const [programEnrollment, bounty] = await Promise.all([+ getProgramEnrollmentOrThrow({+ partnerId: partner.id,+ programId,+ include: {+ program: {+ select: {+ id: true,+ defaultGroupId: true,+ },+ },+ },+ }),- const bounty = await getBountyOrThrow({- bountyId,- programId: programEnrollment.programId,- });+ getBountyOrThrow({+ bountyId,+ programId,+ include: {+ groups: {+ select: {+ groupId: true,+ },+ },+ submissions: {+ where: {+ partnerId: partner.id,+ },+ },+ },+ }),+ ]);++ if (bounty.submissions.length > 0) {+ throw new DubApiError({+ code: "bad_request",+ message: "You have already submitted a social content for this bounty.",+ });+ }const bountyInfo = resolveBountyDetails(bounty);@@ -50,6 +79,19 @@ export const GET = withPartnerProfile(});}+ const canSubmitBounty = canPartnerSubmitBounty({+ program: programEnrollment.program,+ bounty,+ programEnrollment,+ });++ if (!canSubmitBounty) {+ throw new DubApiError({+ code: "not_found",+ message: "Bounty not found.",+ });+ }+const content = await getSocialContent({platform: bountyInfo.socialMetrics.platform,url,diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.tsindex c9622f3ed0..a8106c0209 100644--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.ts+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.ts@@ -9,8 +9,21 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {partnerId: partner.id,programId: params.programId,include: {- program: true,- links: true,+ program: {+ select: {+ id: true,+ defaultGroupId: true,+ },+ },+ links: {+ select: {+ clicks: true,+ leads: true,+ conversions: true,+ sales: true,+ saleAmount: true,+ },+ },},});diff --git a/apps/web/lib/bounty/api/bounty-availability.ts b/apps/web/lib/bounty/api/bounty-availability.tsindex 6b8b0ac6a7..d358b0bc0c 100644--- a/apps/web/lib/bounty/api/bounty-availability.ts+++ b/apps/web/lib/bounty/api/bounty-availability.ts@@ -1,5 +1,13 @@-import { Bounty, Prisma, ProgramEnrollment } from "@prisma/client";+import {+ Bounty,+ BountyGroup,+ BountySubmission,+ Prisma,+ Program,+ ProgramEnrollment,+} from "@prisma/client";import { addDays } from "date-fns";+import { isBountyExpired, isBountyStarted } from "../bounty-period";export function buildBountyEligibilityWhere(groupId: string | undefined,@@ -55,22 +63,86 @@ export function getEffectiveBountyPeriod({};}-export const canPartnerSeeBounty = ({+type PartnerBountyEligibilityInput = {+ program: Pick<Program, "defaultGroupId">;+ bounty: Pick<+ Bounty,+ "startsAt" | "endsAt" | "endsAfterDays" | "startMode" | "archivedAt"+ > & {+ groups: Pick<BountyGroup, "groupId">[];+ };+ programEnrollment: Pick<+ ProgramEnrollment,+ "groupJoinedAt" | "createdAt" | "groupId" | "status"+ >;+};++export function isPartnerEligibleForBounty({+ program,+ bounty,programEnrollment,+}: PartnerBountyEligibilityInput): boolean {+ // Archived bounties are not visible+ if (bounty.archivedAt) {+ return false;+ }++ // If the bounty has groups, check if the partner is in one of them+ const bountyGroupIds = bounty.groups.map((g) => g.groupId);+ const partnerGroupId = programEnrollment.groupId || program.defaultGroupId;++ if (bountyGroupIds.length > 0 && !bountyGroupIds.includes(partnerGroupId)) {+ return false;+ }++ // Check if the bounty is in the active period+ const { startsAt, endsAt } = getEffectiveBountyPeriod({+ programEnrollment,+ bounty,+ });++ // If the bounty is not in the active period, it is not visible+ if (!isBountyStarted(startsAt) || isBountyExpired(endsAt)) {+ return false;+ }++ return true;+}++export const canPartnerSeeBounty = ({+ program,bounty,-}: {- programEnrollment: Pick<ProgramEnrollment, "groupJoinedAt" | "createdAt">;- bounty: Pick<Bounty, "startsAt" | "endsAt" | "endsAfterDays" | "startMode">;-}) => {- //+ programEnrollment,+}: PartnerBountyEligibilityInput & {+ bounty: PartnerBountyEligibilityInput["bounty"] & {+ submissions: Pick<BountySubmission, "id">[];+ };+}): boolean => {+ // Bounties the partner has a submission on stay visible+ if (bounty.submissions.length > 0) {+ return true;+ }++ return isPartnerEligibleForBounty({+ program,+ bounty,+ programEnrollment,+ });};-export const canPartnerSubmitToBounty = ({- programEnrollment,+export const canPartnerSubmitBounty = ({+ program,bounty,-}: {- programEnrollment: Pick<ProgramEnrollment, "groupJoinedAt" | "createdAt">;- bounty: Pick<Bounty, "startsAt" | "endsAt" | "endsAfterDays" | "startMode">;-}) => {- //+ programEnrollment,+}: PartnerBountyEligibilityInput): boolean => {+ // Only approved partners can submit bounties+ if (programEnrollment.status !== "approved") {+ return false;+ }++ return isPartnerEligibleForBounty({+ program,+ bounty,+ programEnrollment,+ });};diff --git a/apps/web/lib/bounty/api/get-bounty-submission-upload-url.ts b/apps/web/lib/bounty/api/get-bounty-submission-upload-url.tsindex a6432aee6d..be4749878b 100644--- a/apps/web/lib/bounty/api/get-bounty-submission-upload-url.ts+++ b/apps/web/lib/bounty/api/get-bounty-submission-upload-url.ts@@ -1,10 +1,11 @@import { DubApiError } from "@/lib/api/errors";-import { prisma } from "@/lib/prisma";import { storage } from "@/lib/storage";import { ratelimit } from "@/lib/upstash";import { submissionRequirementsSchema } from "@/lib/zod/schemas/bounties";import { nanoid, R2_URL } from "@dub/utils";import { ProgramEnrollment } from "@prisma/client";+import { canPartnerSubmitBounty } from "./bounty-availability";+import { getBountyOrThrow } from "./get-bounty-or-throw";const MAX_ATTEMPTS = 25;const CACHE_KEY_PREFIX = "bounty:submission:file:upload";@@ -16,7 +17,12 @@ type GetBountySubmissionUploadUrlParams = {contentLength: number;programEnrollment: Pick<ProgramEnrollment,- "programId" | "partnerId" | "groupId"+ | "programId"+ | "partnerId"+ | "groupId"+ | "status"+ | "createdAt"+ | "groupJoinedAt">;};@@ -74,73 +80,41 @@ export async function getBountySubmissionUploadUrl({});}- const bounty = await prisma.bounty.findUniqueOrThrow({- where: {- id: bountyId,- },- select: {- programId: true,- type: true,- startsAt: true,- endsAt: true,- archivedAt: true,- submissionRequirements: true,+ const bounty = await getBountyOrThrow({+ bountyId,+ programId,+ include: {groups: {select: {groupId: true,},},+ program: {+ select: {+ id: true,+ defaultGroupId: true,+ },+ },},});- if (bounty.programId !== programId) {- throw new DubApiError({- code: "forbidden",- message: "This bounty is not for this program.",- });- }-- if (bounty.groups.length > 0) {- const isInGroup = bounty.groups.find(- ({ groupId }) => groupId === programEnrollment.groupId,- );-- if (!isInGroup) {- throw new DubApiError({- code: "forbidden",- message: "You are not allowed to submit this bounty.",- });- }- }-- // Validate the bounty dates- const now = new Date();-- if (bounty.startsAt && bounty.startsAt > now) {- throw new DubApiError({- code: "forbidden",- message: "This bounty is not yet available.",- });- }-- if (bounty.endsAt && bounty.endsAt < now) {+ if (bounty.type === "performance") {throw new DubApiError({code: "forbidden",- message: "This bounty is no longer available.",+ message: "You are not allowed to submit a performance bounty.",});}- if (bounty.archivedAt) {- throw new DubApiError({- code: "forbidden",- message: "This bounty is archived.",- });- }+ const canSubmitBounty = canPartnerSubmitBounty({+ program: bounty.program,+ bounty,+ programEnrollment,+ });- if (bounty.type === "performance") {+ if (!canSubmitBounty) {throw new DubApiError({- code: "forbidden",- message: "You are not allowed to submit a performance bounty.",+ code: "not_found",+ message: "Bounty not found.",});}