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.

Fix partner bounty visibility queries and eligibility where helpers.

Kiran Kb770f433e404
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 88bfe2e456..7d2e98c629 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -62,9 +62,7 @@ export const GET = withWorkspace(
prisma.bounty.findMany({
where: {
programId,
- ...(programEnrollment && {
- ...buildBountyEligibilityWhere(partnerGroupId),
- }),
+ ...(programEnrollment && buildBountyEligibilityWhere(partnerGroupId)),
},
include: {
...bountyEligibilityIncludes,
diff --git a/apps/web/lib/bounty/api/bounty-availability.ts b/apps/web/lib/bounty/api/bounty-availability.ts
index aa617d75c3..50c33ed358 100644
--- a/apps/web/lib/bounty/api/bounty-availability.ts
+++ b/apps/web/lib/bounty/api/bounty-availability.ts
@@ -10,6 +10,20 @@ import {
import { addDays } from "date-fns";
import { isBountyExpired, isBountyStarted } from "../bounty-period";
+type PartnerBountyEligibilityInput = {
+ program: Pick<Program, "defaultGroupId">;
+ bounty: Pick<
+ Bounty,
+ "id" | "startsAt" | "endsAt" | "endsAfterDays" | "startMode" | "archivedAt"
+ > & {
+ groups: Pick<BountyGroup, "groupId">[];
+ };
+ programEnrollment: Pick<
+ ProgramEnrollment,
+ "groupJoinedAt" | "createdAt" | "groupId" | "status"
+ >;
+};
+
export function buildBountyEligibilityWhere(
groupId: string | undefined,
): Prisma.BountyWhereInput {
@@ -35,30 +49,32 @@ export function buildBountyEligibilityWhere(
};
}
-export function buildBountyActivePeriodWhere(): Prisma.BountyWhereInput[] {
+export function buildBountyActivePeriodWhere(): Prisma.BountyWhereInput {
const now = new Date();
- return [
- {
- startMode: BountyStartMode.relative,
- },
- {
- startMode: BountyStartMode.absolute,
- startsAt: {
- lte: now,
+ return {
+ OR: [
+ {
+ startMode: BountyStartMode.relative,
},
- OR: [
- {
- endsAt: null,
+ {
+ startMode: BountyStartMode.absolute,
+ startsAt: {
+ lte: now,
},
- {
- endsAt: {
- gte: now,
+ OR: [
+ {
+ endsAt: null,
},
- },
- ],
- },
- ];
+ {
+ endsAt: {
+ gte: now,
+ },
+ },
+ ],
+ },
+ ],
+ };
}
export const bountyEligibilityIncludes = {
@@ -90,21 +106,7 @@ export function getEffectiveBountyPeriod({
};
}
-type PartnerBountyEligibilityInput = {
- program: Pick<Program, "defaultGroupId">;
- bounty: Pick<
- Bounty,
- "id" | "startsAt" | "endsAt" | "endsAfterDays" | "startMode" | "archivedAt"
- > & {
- groups: Pick<BountyGroup, "groupId">[];
- };
- programEnrollment: Pick<
- ProgramEnrollment,
- "groupJoinedAt" | "createdAt" | "groupId" | "status"
- >;
-};
-
-export function isPartnerEligibleForBounty({
+function isPartnerEligibleForBounty({
program,
bounty,
programEnrollment,
@@ -154,6 +156,10 @@ export const canPartnerSeeBounty = ({
submissions: Pick<BountySubmission, "id">[];
};
}): boolean => {
+ if (bounty.archivedAt) {
+ return false;
+ }
+
// Bounties the partner has a submission on stay visible
if (bounty.submissions.length > 0) {
return true;
diff --git a/apps/web/lib/bounty/api/get-bounties-for-partner.ts b/apps/web/lib/bounty/api/get-bounties-for-partner.ts
index 18d9c54764..ecc65517f6 100644
--- a/apps/web/lib/bounty/api/get-bounties-for-partner.ts
+++ b/apps/web/lib/bounty/api/get-bounties-for-partner.ts
@@ -7,53 +7,58 @@ import { PartnerBountySchema } from "@/lib/zod/schemas/partner-profile";
import { Program, ProgramEnrollment } from "@prisma/client";
import * as z from "zod/v4";
import {
+ bountyEligibilityIncludes,
buildBountyActivePeriodWhere,
buildBountyEligibilityWhere,
+ canPartnerSeeBounty,
getEffectiveBountyPeriod,
} from "./bounty-availability";
type GetBountiesForPartnerParams = Pick<
ProgramEnrollment,
- "groupId" | "partnerId" | "totalCommissions" | "groupJoinedAt" | "createdAt"
+ | "groupId"
+ | "partnerId"
+ | "totalCommissions"
+ | "groupJoinedAt"
+ | "createdAt"
+ | "status"
> & {
links: PartnerLink[];
program: Pick<Program, "id" | "defaultGroupId">;
};
export async function getBountiesForPartner({
- partnerId,
- groupId,
- totalCommissions,
- createdAt,
- groupJoinedAt,
program,
links,
+ ...programEnrollment
}: GetBountiesForPartnerParams) {
- const now = new Date();
+ const { groupId, partnerId, totalCommissions, createdAt, groupJoinedAt } =
+ programEnrollment;
+
const partnerGroupId = groupId || program.defaultGroupId;
const bounties = await prisma.bounty.findMany({
where: {
programId: program.id,
+ archivedAt: null,
OR: [
{
- archivedAt: null,
- ...buildBountyEligibilityWhere(partnerGroupId),
- OR: [
- ...buildBountyActivePeriodWhere(),
- // Bounties the partner has a submission on stay visible
- {
- submissions: {
- some: {
- partnerId,
- },
- },
+ submissions: {
+ some: {
+ partnerId,
},
+ },
+ },
+ {
+ AND: [
+ buildBountyEligibilityWhere(partnerGroupId),
+ buildBountyActivePeriodWhere(),
],
},
],
},
include: {
+ ...bountyEligibilityIncludes,
workflow: {
select: {
triggerConditions: true,
@@ -79,8 +84,16 @@ export async function getBountiesForPartner({
const partnerLinkStats = aggregatePartnerLinksStats(links);
+ const visibleBounties = bounties.filter((bounty) =>
+ canPartnerSeeBounty({
+ program,
+ bounty,
+ programEnrollment,
+ }),
+ );
+
return z.array(PartnerBountySchema).parse(
- bounties.map((bounty) => {
+ visibleBounties.map((bounty) => {
const performanceCondition =
bounty.workflow?.triggerConditions?.[0] || null;