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.

Restrict relative bounties to new partners and fix duration form sync.

Kiran K2c35dd6d9d2b
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/add-edit-bounty-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/add-edit-bounty-sheet.tsx
index 5d2600e9b1..cd807e7b15 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/add-edit-bounty-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/add-edit-bounty-sheet.tsx
@@ -40,7 +40,7 @@ import {
} from "@dub/ui";
import { cn } from "@dub/utils";
import { BountySubmissionFrequency } from "@prisma/client";
-import { Dispatch, SetStateAction, useState } from "react";
+import { Dispatch, SetStateAction, useMemo, useState } from "react";
import { Controller, FormProvider } from "react-hook-form";
import { BountyCriteria } from "./bounty-criteria";
import { BountyDuration } from "./bounty-duration";
@@ -110,6 +110,16 @@ function BountySheetContent({ setIsOpen, bounty }: BountySheetProps) {
const showBountySocialMetricsUpsell =
bountyTypeUI === "socialMetrics" && !canUseBountySocialMetrics;
+ const bountyTimingValue = useMemo(
+ () => ({
+ startMode: startMode ?? "absolute",
+ startsAt: startsAt ? new Date(startsAt) : new Date(),
+ endsAt: endsAt ? new Date(endsAt) : null,
+ endsAfterDays: endsAfterDays ?? null,
+ }),
+ [startMode, startsAt, endsAt, endsAfterDays],
+ );
+
return (
<form onSubmit={onSubmit} className="flex h-full flex-col">
<div className="sticky top-0 z-10 border-b border-neutral-200 bg-white">
@@ -248,12 +258,7 @@ function BountySheetContent({ setIsOpen, bounty }: BountySheetProps) {
<BountyDuration
isEditing={!!bounty}
- value={{
- startMode: startMode ?? "absolute",
- startsAt: startsAt ? new Date(startsAt) : new Date(),
- endsAt: endsAt ? new Date(endsAt) : null,
- endsAfterDays: endsAfterDays ?? null,
- }}
+ value={bountyTimingValue}
onChange={handleTimingChange}
/>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx
index 1108e29ce7..f67d26c50f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx
@@ -339,8 +339,8 @@ export function BountyDuration({
}, [
isEditing,
value.startMode,
- value.startsAt,
- value.endsAt,
+ value.startsAt?.getTime(),
+ value.endsAt?.getTime(),
value.endsAfterDays,
]);
@@ -408,7 +408,12 @@ export function BountyDuration({
setStartPreset(preset);
if (preset === "custom") {
- setCustomStartsAt(customStartsAt ?? value.startsAt);
+ const nextCustomStartsAt = customStartsAt ?? value.startsAt;
+ setCustomStartsAt(nextCustomStartsAt);
+ applyTiming({
+ nextStartPreset: "custom",
+ nextCustomStartsAt,
+ });
return;
}
@@ -450,11 +455,15 @@ export function BountyDuration({
setCustomEndsAfterDays(null);
if (preset === "custom") {
- setCustomEndsAt(
+ const nextCustomEndsAt =
customEndsAt ??
- value.endsAt ??
- addWeeks(value.startsAt, 2),
- );
+ value.endsAt ??
+ addWeeks(value.startsAt, 2);
+ setCustomEndsAt(nextCustomEndsAt);
+ applyTiming({
+ nextEndPreset: "custom",
+ nextCustomEndsAt,
+ });
return;
}
diff --git a/apps/web/lib/bounty/api/bounty-availability.ts b/apps/web/lib/bounty/api/bounty-availability.ts
index 50c33ed358..f1516d2d44 100644
--- a/apps/web/lib/bounty/api/bounty-availability.ts
+++ b/apps/web/lib/bounty/api/bounty-availability.ts
@@ -14,7 +14,13 @@ type PartnerBountyEligibilityInput = {
program: Pick<Program, "defaultGroupId">;
bounty: Pick<
Bounty,
- "id" | "startsAt" | "endsAt" | "endsAfterDays" | "startMode" | "archivedAt"
+ | "id"
+ | "startsAt"
+ | "endsAt"
+ | "endsAfterDays"
+ | "startMode"
+ | "archivedAt"
+ | "createdAt"
> & {
groups: Pick<BountyGroup, "groupId">[];
};
@@ -127,6 +133,19 @@ function isPartnerEligibleForBounty({
return false;
}
+ // Relative bounties are for new partners only (joined on/after bounty creation)
+ if (bounty.startMode === "relative") {
+ const partnerJoinedAt =
+ programEnrollment.groupJoinedAt || programEnrollment.createdAt;
+
+ if (partnerJoinedAt < bounty.createdAt) {
+ console.log(
+ `Partner joined before relative bounty ${bounty.id} was created.`,
+ );
+ return false;
+ }
+ }
+
// Check if the bounty is in the active period
const { startsAt, endsAt } = getEffectiveBountyPeriod({
programEnrollment,
diff --git a/apps/web/lib/bounty/api/validate-bounty.ts b/apps/web/lib/bounty/api/validate-bounty.ts
index a4067bde80..ea9fda8667 100644
--- a/apps/web/lib/bounty/api/validate-bounty.ts
+++ b/apps/web/lib/bounty/api/validate-bounty.ts
@@ -114,7 +114,7 @@ export function validateBounty({
});
}
- if (submissionFrequency && !endsAt) {
+ if (submissionFrequency && !endsAt && !endsAfterDays) {
throw new DubApiError({
code: "bad_request",
message: "An end date is required when submissionFrequency is set.",
diff --git a/apps/web/tests/bounties/index.test.ts b/apps/web/tests/bounties/index.test.ts
index c17a1b339d..5e9ecd8322 100644
--- a/apps/web/tests/bounties/index.test.ts
+++ b/apps/web/tests/bounties/index.test.ts
@@ -392,6 +392,35 @@ describe.sequential(
});
});
+ test("POST /bounties - submissionFrequency with relative endsAfterDays is accepted", async () => {
+ const { status, data: bounty } = await http.post<Bounty>({
+ path: "/bounties",
+ body: {
+ ...base,
+ startMode: "relative",
+ startsAt: null,
+ endsAt: null,
+ endsAfterDays: 30,
+ maxSubmissions: 4,
+ submissionFrequency: "week",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(bounty).toMatchObject({
+ startMode: "relative",
+ startsAt: null,
+ endsAt: null,
+ endsAfterDays: 30,
+ maxSubmissions: 4,
+ submissionFrequency: "week",
+ });
+
+ onTestFinished(async () => {
+ await h.deleteBounty(bounty.id);
+ });
+ });
+
test("POST /bounties - submissionsOpenAt without endsAt is rejected", async () => {
const submissionsOpenAt = addDays(bountyStartsAt, 5).toISOString();