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.

Disable partner notification emails for relative-start bounties.

Kiran K90b43cad65c6
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index 822b657e4e..daba15e74e 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -20,7 +20,7 @@ import {
updateBountySchema,
} from "@/lib/zod/schemas/bounties";
import { arrayEqual, deepEqual } from "@dub/utils";
-import { PartnerGroup, Prisma } from "@prisma/client";
+import { BountyStartMode, PartnerGroup, Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -93,7 +93,10 @@ export const PATCH = withWorkspace(
endsAtUpdate = { endsAt };
} else if (endsAfterDays != null) {
endsAtUpdate = { endsAt: null };
- } else if (nextStartMode === "relative" && bounty.endsAt != null) {
+ } else if (
+ nextStartMode === BountyStartMode.relative &&
+ bounty.endsAt != null
+ ) {
endsAtUpdate = { endsAt: null };
}
@@ -102,7 +105,7 @@ export const PATCH = withWorkspace(
// Relative bounties never store startsAt; coerce so mode switches don't
// fail validation against a leftover absolute startsAt.
startsAt:
- nextStartMode === "relative"
+ nextStartMode === BountyStartMode.relative
? null
: startsAt !== undefined
? startsAt
@@ -113,7 +116,7 @@ export const PATCH = withWorkspace(
endsAfterDays:
endsAfterDays !== undefined
? endsAfterDays
- : nextStartMode === "absolute"
+ : nextStartMode === BountyStartMode.absolute
? null
: bounty.endsAfterDays,
submissionsOpenAt,
@@ -220,7 +223,7 @@ export const PATCH = withWorkspace(
// For absolute bounties, only update startsAt when explicitly provided.
let startsAtUpdate: { startsAt?: Date | null } = {};
- if (nextStartMode === "relative") {
+ if (nextStartMode === BountyStartMode.relative) {
startsAtUpdate = { startsAt: null };
} else if (startsAt !== undefined) {
startsAtUpdate = { startsAt: startsAt ?? new Date() };
@@ -242,7 +245,8 @@ export const PATCH = withWorkspace(
...(startMode !== undefined && { startMode }),
...(endsAfterDays !== undefined
? { endsAfterDays }
- : nextStartMode === "absolute" && bounty.endsAfterDays != null
+ : nextStartMode === BountyStartMode.absolute &&
+ bounty.endsAfterDays != null
? { endsAfterDays: null }
: {}),
submissionsOpenAt:
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 7d2e98c629..596e18c4c7 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -29,7 +29,7 @@ import {
WORKFLOW_ATTRIBUTE_TRIGGER,
} from "@/lib/zod/schemas/workflows";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
-import { Workflow } from "@prisma/client";
+import { BountyStartMode, Workflow } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -210,7 +210,8 @@ export const POST = withWorkspace(
// startsAt is only stored for absolute bounties (defaulting to now when
// omitted); relative bounties start when a partner joins, so it stays null.
- startsAt = startMode === "absolute" ? startsAt || new Date() : null;
+ startsAt =
+ startMode === BountyStartMode.absolute ? startsAt || new Date() : null;
const bounty = await prisma.$transaction(async (tx) => {
let workflow: Workflow | null = null;
@@ -286,12 +287,12 @@ export const POST = withWorkspace(
const shouldScheduleDraftSubmissions =
bounty.type === "performance" &&
bounty.performanceScope === "lifetime" &&
- bounty.startMode === "absolute";
+ bounty.startMode === BountyStartMode.absolute;
const shouldSchedulePartnerNotifications =
sendNotificationEmails &&
canSendEmailCampaigns &&
- bounty.startMode === "absolute";
+ bounty.startMode === BountyStartMode.absolute;
waitUntil(
Promise.allSettled([
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 91ee6dc52c..549b0fff01 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
@@ -39,7 +39,7 @@ import {
useRouterStuff,
} from "@dub/ui";
import { cn } from "@dub/utils";
-import { BountySubmissionFrequency } from "@prisma/client";
+import { BountyStartMode, BountySubmissionFrequency } from "@prisma/client";
import { Dispatch, SetStateAction, useMemo, useState } from "react";
import { Controller, FormProvider } from "react-hook-form";
import { BountyCriteria } from "./bounty-criteria";
@@ -111,7 +111,7 @@ function BountySheetContent({ setIsOpen, bounty }: BountySheetProps) {
const bountyTimingValue = useMemo(
() => ({
- startMode: startMode ?? "absolute",
+ startMode: startMode ?? BountyStartMode.absolute,
startsAt: startsAt ? new Date(startsAt) : new Date(),
endsAt: endsAt ? new Date(endsAt) : null,
endsAfterDays: endsAfterDays ?? null,
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 f67d26c50f..78e814c401 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
@@ -14,6 +14,7 @@ import {
} from "@/ui/shared/inline-badge-popover";
import { CalendarIcon, DatePicker } from "@dub/ui";
import { formatDate } from "@dub/utils";
+import { BountyStartMode } from "@prisma/client";
import { addDays, addMonths, addWeeks } from "date-fns";
import { useEffect, useState } from "react";
@@ -98,7 +99,7 @@ function parsePresets(value: BountyTimingInput): ParsedPresets {
let startPreset: StartPreset;
let customStartsAt: Date | null;
- if (value.startMode === "relative") {
+ if (value.startMode === BountyStartMode.relative) {
startPreset = "onPartnerJoin";
customStartsAt = null;
} else {
@@ -142,7 +143,7 @@ function parsePresets(value: BountyTimingInput): ParsedPresets {
if (!value.endsAt) {
endPreset = "never";
customEndsAt = null;
- } else if (value.startMode === "absolute") {
+ } else if (value.startMode === BountyStartMode.absolute) {
const matchedEndPreset = (
Object.entries(BOUNTY_DURATION_DAYS) as [DurationPreset, number][]
).find(([, days]) =>
@@ -171,7 +172,7 @@ function parsePresets(value: BountyTimingInput): ParsedPresets {
}
function parsePresetsForEdit(value: BountyTimingInput): ParsedPresets {
- if (value.startMode === "relative") {
+ if (value.startMode === BountyStartMode.relative) {
const startPreset: StartPreset = "onPartnerJoin";
const customStartsAt = null;
@@ -385,7 +386,7 @@ export function BountyDuration({
const endSuffix =
customEndsAfterDays != null ||
(endPreset !== "never" && endPreset !== "custom")
- ? value.startMode === "relative"
+ ? value.startMode === BountyStartMode.relative
? "after joining"
: "from start date"
: null;
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/confirm-create-bounty-modal.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/confirm-create-bounty-modal.tsx
index 189c0b8bec..a140a4e8c2 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/confirm-create-bounty-modal.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/confirm-create-bounty-modal.tsx
@@ -21,6 +21,7 @@ import {
import { Users, Users6 } from "@dub/ui/icons";
import { nFormatter, pluralize } from "@dub/utils";
import { cn } from "@dub/utils/src";
+import { BountyStartMode } from "@prisma/client";
import { Dispatch, SetStateAction, useMemo, useState } from "react";
type ConfirmCreateBountyModalProps = {
@@ -49,43 +50,50 @@ function ConfirmCreateBountyModal({
showConfirmCreateBountyModal: boolean;
setShowConfirmCreateBountyModal: Dispatch<SetStateAction<boolean>>;
} & ConfirmCreateBountyModalProps) {
+ const { groups } = useGroups();
const { plan, slug: workspaceSlug, isOwner } = useWorkspace();
const { canSendEmailCampaigns } = getPlanCapabilities(plan);
const [isLoading, setIsLoading] = useState(false);
+
const [sendNotificationEmails, setSendNotificationEmails] = useState(
canSendEmailCampaigns,
);
+ const isRelative = bounty?.startMode === BountyStartMode.relative;
+
const { totalPartners, loading } = usePartnersCountByGroupIds({
- groupIds: bounty?.groups?.map((group) => group.id) ?? [],
+ groupIds: isRelative
+ ? null
+ : bounty?.groups?.map((group) => group.id) ?? [],
});
- const { groups } = useGroups();
-
const eligibleGroups = useMemo(() => {
if (!groups || !bounty || bounty.groups.length === 0) {
return [];
}
+
return bounty.groups
.map((bountyGroup) => groups.find((g) => g.id === bountyGroup.id))
.filter((g): g is NonNullable<typeof g> => g !== undefined);
}, [groups, bounty?.groups]);
+ if (!bounty) {
+ return null;
+ }
+
const handleConfirm = async () => {
setIsLoading(true);
try {
- await onConfirm({ sendNotificationEmails });
+ await onConfirm({
+ sendNotificationEmails: isRelative ? false : sendNotificationEmails,
+ });
setShowConfirmCreateBountyModal(false);
} finally {
setIsLoading(false);
}
};
- if (!bounty) {
- return null;
- }
-
const { dateRangeLabel, partnerAudienceLabel } = getProgramBountyMeta(bounty);
return (
@@ -177,7 +185,7 @@ function ConfirmCreateBountyModal({
<DynamicTooltipWrapper
tooltipProps={
- !canSendEmailCampaigns
+ !canSendEmailCampaigns || isRelative
? {
content: (
<TooltipContent
@@ -194,8 +202,8 @@ function ConfirmCreateBountyModal({
<label
className={cn(
"mt-4 flex items-center gap-2",
- !canSendEmailCampaigns &&
- "pointer-events-none cursor-not-allowed",
+ !canSendEmailCampaigns ||
+ (isRelative && "pointer-events-none cursor-not-allowed"),
)}
>
<Checkbox
@@ -203,13 +211,13 @@ function ConfirmCreateBountyModal({
onCheckedChange={(checked) =>
setSendNotificationEmails(Boolean(checked))
}
- disabled={!canSendEmailCampaigns}
+ disabled={!canSendEmailCampaigns || isRelative}
className="data-[state=checked]:bg-black"
/>
<span
className={cn(
"text-content-default select-none text-sm font-medium",
- !canSendEmailCampaigns && "opacity-50",
+ !canSendEmailCampaigns || (isRelative && "opacity-50"),
)}
>
Send notification to{" "}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts
index 9d02709f69..5ae2fc3bc6 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts
@@ -17,7 +17,7 @@ import {
bountySocialContentRequirementsSchema,
} from "@/lib/zod/schemas/bounties";
import { formatDate } from "@dub/utils";
-import { BountySubmissionFrequency } from "@prisma/client";
+import { BountyStartMode, BountySubmissionFrequency } from "@prisma/client";
import { addDays } from "date-fns";
import {
Dispatch,
@@ -250,7 +250,8 @@ export function useAddEditBountyForm({
setHasEndDate(
Boolean(nextEndsAt) ||
- (nextStartMode === "relative" && Boolean(nextEndsAfterDays)),
+ (nextStartMode === BountyStartMode.relative &&
+ Boolean(nextEndsAfterDays)),
);
if (!nextEndsAt) {
@@ -603,7 +604,7 @@ export function useAddEditBountyForm({
} = form.getValues();
// Relative bounties start when a partner joins, so startsAt must be null
- if (data.startMode === "relative") {
+ if (data.startMode === BountyStartMode.relative) {
data.startsAt = null;
}
@@ -694,14 +695,17 @@ export function useAddEditBountyForm({
: performanceCondition,
})
: name || "New bounty",
- startsAt: startMode === "relative" ? null : startsAt || new Date(),
+ startsAt:
+ startMode === BountyStartMode.relative
+ ? null
+ : startsAt || new Date(),
endsAt:
- startMode === "relative"
+ startMode === BountyStartMode.relative
? endsAfterDays != null
? null
: endsAt ?? null
: effectiveEndsAt,
- startMode: startMode ?? "absolute",
+ startMode: startMode ?? BountyStartMode.absolute,
endsAfterDays: endsAfterDays ?? null,
rewardAmount: rewardAmount ? rewardAmount * 100 : null,
rewardDescription: rewardDescription || null,
diff --git a/apps/web/lib/bounty/api/bounty-availability.ts b/apps/web/lib/bounty/api/bounty-availability.ts
index af8f22b7fe..de10ac3a5f 100644
--- a/apps/web/lib/bounty/api/bounty-availability.ts
+++ b/apps/web/lib/bounty/api/bounty-availability.ts
@@ -104,7 +104,9 @@ export function getEffectiveBountyPeriod({
// If startMode is absolute, use the startsAt (Assumed to be set).
// If startMode is relative, use the groupJoinedAt or createdAt.
const bountyStartDate =
- startMode === "absolute" ? startsAt! : groupJoinedAt || createdAt;
+ startMode === BountyStartMode.absolute
+ ? startsAt!
+ : groupJoinedAt || createdAt;
return {
startsAt: bountyStartDate,
@@ -135,7 +137,7 @@ export function isPartnerEligibleForBounty({
}
// Relative bounties are for new partners only (joined on/after bounty creation)
- if (bounty.startMode === "relative") {
+ if (bounty.startMode === BountyStartMode.relative) {
const partnerJoinedAt =
programEnrollment.groupJoinedAt || programEnrollment.createdAt;
diff --git a/apps/web/lib/bounty/api/validate-bounty.ts b/apps/web/lib/bounty/api/validate-bounty.ts
index ea9fda8667..1136f88e6e 100644
--- a/apps/web/lib/bounty/api/validate-bounty.ts
+++ b/apps/web/lib/bounty/api/validate-bounty.ts
@@ -1,5 +1,6 @@
import { DubApiError } from "@/lib/api/errors";
import { CreateBountyInput } from "@/lib/types";
+import { BountyStartMode } from "@prisma/client";
export function validateBounty({
type,
@@ -14,11 +15,11 @@ export function validateBounty({
rewardDescription,
performanceScope,
}: Partial<CreateBountyInput>) {
- startMode = startMode ?? "absolute";
+ startMode = startMode ?? BountyStartMode.absolute;
// startsAt is required when startMode is absolute and must be null when
// startMode is relative (relative bounties start when a partner joins).
- if (startMode === "relative") {
+ if (startMode === BountyStartMode.relative) {
if (startsAt != null) {
throw new DubApiError({
message:
@@ -39,7 +40,7 @@ export function validateBounty({
});
}
- if (startMode === "absolute" && endsAfterDays) {
+ if (startMode === BountyStartMode.absolute && endsAfterDays) {
throw new DubApiError({
message:
"endsAfterDays is only supported when the bounty starts when a partner joins.",
diff --git a/apps/web/lib/bounty/bounty-period.ts b/apps/web/lib/bounty/bounty-period.ts
index 3c4b794676..ec1eaa3559 100644
--- a/apps/web/lib/bounty/bounty-period.ts
+++ b/apps/web/lib/bounty/bounty-period.ts
@@ -38,7 +38,7 @@ export function resolveBountyTiming({
}) {
const now = new Date();
- let startMode: BountyStartMode = "absolute";
+ let startMode: BountyStartMode = BountyStartMode.absolute;
let startsAt = now;
switch (startPreset) {
@@ -55,7 +55,7 @@ export function resolveBountyTiming({
startsAt = addMonths(now, 6);
break;
case "onPartnerJoin":
- startMode = "relative";
+ startMode = BountyStartMode.relative;
startsAt = now;
break;
case "custom":
@@ -72,7 +72,7 @@ export function resolveBountyTiming({
case "twoWeeks":
case "oneMonth":
case "sixMonths":
- if (startMode === "absolute") {
+ if (startMode === BountyStartMode.absolute) {
endsAt = addDays(startsAt, BOUNTY_DURATION_DAYS[endPreset]);
} else {
endsAfterDays = BOUNTY_DURATION_DAYS[endPreset];
@@ -110,7 +110,7 @@ export function getProgramBountyMeta({
startMode: BountyStartMode;
endsAfterDays: number | null;
}) {
- const isRelative = startMode === "relative" || !startsAt;
+ const isRelative = startMode === BountyStartMode.relative || !startsAt;
let dateRangeLabel: string;
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index fc2a8e8b73..64858abb6f 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -127,7 +127,7 @@ export const createBountySchema = z.object({
performanceCondition: bountyPerformanceConditionSchema.nullish(),
performanceScope: z.enum(BountyPerformanceScope).nullish(),
sendNotificationEmails: z.boolean().optional(),
- startMode: z.enum(BountyStartMode).default("absolute"),
+ startMode: z.enum(BountyStartMode).default(BountyStartMode.absolute),
endsAfterDays: z.number().int().positive().nullish(),
});
diff --git a/apps/web/tests/bounties/index.test.ts b/apps/web/tests/bounties/index.test.ts
index 5e9ecd8322..a67b51e1a3 100644
--- a/apps/web/tests/bounties/index.test.ts
+++ b/apps/web/tests/bounties/index.test.ts
@@ -1,4 +1,4 @@
-import { Bounty } from "@prisma/client";
+import { Bounty, BountyStartMode } from "@prisma/client";
import { addDays, addMonths, subDays } from "date-fns";
import { E2E_PARTNER_GROUP } from "tests/utils/resource";
import { describe, expect, onTestFinished, test } from "vitest";
@@ -397,7 +397,7 @@ describe.sequential(
path: "/bounties",
body: {
...base,
- startMode: "relative",
+ startMode: BountyStartMode.relative,
startsAt: null,
endsAt: null,
endsAfterDays: 30,
@@ -408,7 +408,7 @@ describe.sequential(
expect(status).toEqual(200);
expect(bounty).toMatchObject({
- startMode: "relative",
+ startMode: BountyStartMode.relative,
startsAt: null,
endsAt: null,
endsAfterDays: 30,