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.

generateBountyName + make bounty.name non null

Steven Teyb94d76defca0
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 7f298b3e9c..3c86a1ece6 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -1,4 +1,5 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
+import { generateBountyName } from "@/lib/api/bounties/generate-bounty-name";
import { createId } from "@/lib/api/create-id";
import { DubApiError } from "@/lib/api/errors";
import { throwIfInvalidGroupIds } from "@/lib/api/groups/throw-if-invalid-group-ids";
@@ -97,6 +98,13 @@ export const POST = withWorkspace(
groupIds,
});
+ const bountyName =
+ name ??
+ generateBountyName({
+ rewardAmount,
+ condition: performanceCondition,
+ });
+
const bounty = await prisma.$transaction(async (tx) => {
let workflow: Workflow | null = null;
const bountyId = createId({ prefix: "bnty_" });
@@ -128,7 +136,7 @@ export const POST = withWorkspace(
id: bountyId,
programId,
workflowId: workflow?.id,
- name,
+ name: bountyName,
description,
type,
startsAt: startsAt!, // Can remove the ! when we're on a newer TS version (currently 5.4.4)
diff --git a/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts b/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
index 6a2d36afe1..0d31710117 100644
--- a/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
@@ -71,6 +71,11 @@ export async function POST(req: Request) {
status: {
in: ["approved", "invited"],
},
+ partner: {
+ email: {
+ not: null,
+ },
+ },
},
include: {
partner: true,
@@ -84,7 +89,7 @@ export async function POST(req: Request) {
if (programEnrollments.length === 0) {
return logAndRespond(
- `Finished sending emails to ~${page * MAX_PAGE_SIZE} partners for bounty ${bountyId}.`,
+ `No program enrollments found for bounty ${bountyId}.`,
);
}
@@ -95,29 +100,27 @@ export async function POST(req: Request) {
`Sending emails to ${programEnrollmentChunk.length} partners: ${programEnrollmentChunk.map(({ partner }) => partner.email).join(", ")}`,
);
await resend?.batch.send(
- programEnrollmentChunk
- .filter(({ partner }) => partner.email)
- .map(({ partner }) => ({
- from: VARIANT_TO_FROM_MAP.notifications,
- to: partner.email!,
- subject: `New bounty available for ${bounty.program.name}`,
- react: NewBountyAvailable({
- email: partner.email!,
- bounty: {
- name: bounty.name!,
- type: bounty.type,
- endsAt: bounty.endsAt,
- description: bounty.description,
- },
- program: {
- name: bounty.program.name,
- slug: bounty.program.slug,
- },
- }),
- headers: {
- "Idempotency-Key": `${bountyId}-${partner.id}`,
+ programEnrollmentChunk.map(({ partner }) => ({
+ from: VARIANT_TO_FROM_MAP.notifications,
+ to: partner.email!, // coerce the type here because we've already filtered out partners with no email in the prisma query
+ subject: `New bounty available for ${bounty.program.name}`,
+ react: NewBountyAvailable({
+ email: partner.email!,
+ bounty: {
+ name: bounty.name,
+ type: bounty.type,
+ endsAt: bounty.endsAt,
+ description: bounty.description,
},
- })),
+ program: {
+ name: bounty.program.name,
+ slug: bounty.program.slug,
+ },
+ }),
+ headers: {
+ "Idempotency-Key": `${bountyId}-${partner.id}`,
+ },
+ })),
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet.tsx
index 8777fdbe0b..8d1ce69a58 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet.tsx
@@ -10,10 +10,7 @@ import {
import { createBountySchema } from "@/lib/zod/schemas/bounties";
import { workflowConditionSchema } from "@/lib/zod/schemas/workflows";
import { useConfirmModal } from "@/ui/modals/confirm-modal";
-import {
- BountyLogic,
- generateBountyName,
-} from "@/ui/partners/bounties/bounty-logic";
+import { BountyLogic } from "@/ui/partners/bounties/bounty-logic";
import { GroupsMultiSelect } from "@/ui/partners/groups/groups-multi-select";
import {
ProgramSheetAccordion,
@@ -244,11 +241,6 @@ function BountySheetContent({ setIsOpen, bounty }: BountySheetProps) {
};
data.performanceCondition = condition;
-
- data.name = generateBountyName({
- rewardAmount: data.rewardAmount,
- condition,
- });
} else if (type === "submission") {
data.performanceCondition = null;
}
diff --git a/apps/web/lib/actions/partners/approve-bounty-submission.ts b/apps/web/lib/actions/partners/approve-bounty-submission.ts
index f27da87120..10f910c587 100644
--- a/apps/web/lib/actions/partners/approve-bounty-submission.ts
+++ b/apps/web/lib/actions/partners/approve-bounty-submission.ts
@@ -89,7 +89,7 @@ export const approveBountySubmissionAction = authActionClient
supportEmail: program.supportEmail || "[email protected]",
},
bounty: {
- name: bounty.name!,
+ name: bounty.name,
type: bounty.type,
},
}),
diff --git a/apps/web/lib/actions/partners/create-bounty-submission.ts b/apps/web/lib/actions/partners/create-bounty-submission.ts
index 41f3f3c45b..ce040de74c 100644
--- a/apps/web/lib/actions/partners/create-bounty-submission.ts
+++ b/apps/web/lib/actions/partners/create-bounty-submission.ts
@@ -153,7 +153,7 @@ export const createBountySubmissionAction = authPartnerActionClient
},
bounty: {
id: bounty.id,
- name: bounty.name!,
+ name: bounty.name,
},
partner: {
name: partner.name,
@@ -176,7 +176,7 @@ export const createBountySubmissionAction = authPartnerActionClient
react: BountySubmitted({
email: partner.email,
bounty: {
- name: bounty.name!,
+ name: bounty.name,
},
program: {
name: program.name,
diff --git a/apps/web/lib/actions/partners/reject-bounty-submission.ts b/apps/web/lib/actions/partners/reject-bounty-submission.ts
index ec3d66fe6b..793de2fac0 100644
--- a/apps/web/lib/actions/partners/reject-bounty-submission.ts
+++ b/apps/web/lib/actions/partners/reject-bounty-submission.ts
@@ -82,7 +82,7 @@ export const rejectBountySubmissionAction = authActionClient
supportEmail: program.supportEmail || "[email protected]",
},
bounty: {
- name: bounty.name!,
+ name: bounty.name,
},
submission: {
rejectionReason:
diff --git a/apps/web/lib/api/bounties/generate-bounty-name.ts b/apps/web/lib/api/bounties/generate-bounty-name.ts
new file mode 100644
index 0000000000..e22b75ee81
--- /dev/null
+++ b/apps/web/lib/api/bounties/generate-bounty-name.ts
@@ -0,0 +1,25 @@
+import { WorkflowCondition } from "@/lib/types";
+import { WORKFLOW_ATTRIBUTE_LABELS } from "@/lib/zod/schemas/workflows";
+import { currencyFormatter, nFormatter } from "@dub/utils";
+import { isCurrencyAttribute } from "../workflows/utils";
+
+export const generateBountyName = ({
+ rewardAmount,
+ condition,
+}: {
+ rewardAmount: number;
+ condition?: WorkflowCondition | null;
+}) => {
+ if (!condition) {
+ return `Earn ${currencyFormatter(rewardAmount / 100)}`;
+ }
+
+ const isCurrency = isCurrencyAttribute(condition.attribute);
+ const rewardAmountFormatted = currencyFormatter(rewardAmount / 100);
+ const attributeLabel = WORKFLOW_ATTRIBUTE_LABELS[condition.attribute];
+ const valueFormatted = isCurrency
+ ? `${currencyFormatter(condition.value / 100)} in`
+ : `${nFormatter(condition.value, { full: true })}`;
+
+ return `Earn ${rewardAmountFormatted} after generating ${valueFormatted} ${attributeLabel}`;
+};
diff --git a/apps/web/lib/api/workflows/execute-award-bounty-action.ts b/apps/web/lib/api/workflows/execute-award-bounty-action.ts
index af6edf82a5..76bae34d44 100644
--- a/apps/web/lib/api/workflows/execute-award-bounty-action.ts
+++ b/apps/web/lib/api/workflows/execute-award-bounty-action.ts
@@ -121,7 +121,7 @@ export const executeAwardBountyAction = async ({
react: BountyCompleted({
email: partner.email,
bounty: {
- name: bounty.name!,
+ name: bounty.name,
type: bounty.type,
},
program: {
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index 7d1c237d90..ac940f8689 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -28,7 +28,7 @@ export const createBountySchema = z.object({
.string()
.trim()
.max(100, "Name must be less than 100 characters")
- .nullish(),
+ .optional(),
description: z
.string()
.trim()
diff --git a/apps/web/ui/partners/bounties/bounty-logic.tsx b/apps/web/ui/partners/bounties/bounty-logic.tsx
index 3e81f6cc50..f58d0b0270 100644
--- a/apps/web/ui/partners/bounties/bounty-logic.tsx
+++ b/apps/web/ui/partners/bounties/bounty-logic.tsx
@@ -1,6 +1,6 @@
import { isCurrencyAttribute } from "@/lib/api/workflows/utils";
import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
-import { WorkflowCondition, WorkflowConditionAttribute } from "@/lib/types";
+import { WorkflowConditionAttribute } from "@/lib/types";
import { WORKFLOW_ATTRIBUTES } from "@/lib/zod/schemas/workflows";
import {
InlineBadgePopover,
@@ -8,7 +8,7 @@ import {
InlineBadgePopoverMenu,
} from "@/ui/shared/inline-badge-popover";
import { Trophy } from "@dub/ui/icons";
-import { cn, currencyFormatter, nFormatter } from "@dub/utils";
+import { cn, currencyFormatter } from "@dub/utils";
import { useAddEditBountyForm } from "app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet";
import { useContext } from "react";
import { Controller } from "react-hook-form";
@@ -20,23 +20,6 @@ const WORKFLOW_ATTRIBUTE_LABELS: Record<WorkflowConditionAttribute, string> = {
totalCommissions: "total commissions",
} as const;
-export const generateBountyName = ({
- rewardAmount,
- condition,
-}: {
- rewardAmount: number;
- condition: WorkflowCondition;
-}) => {
- const isCurrency = isCurrencyAttribute(condition.attribute);
- const rewardAmountFormatted = currencyFormatter(rewardAmount / 100);
- const attributeLabel = WORKFLOW_ATTRIBUTE_LABELS[condition.attribute];
- const valueFormatted = isCurrency
- ? `${currencyFormatter(condition.value / 100)} in`
- : `${nFormatter(condition.value, { full: true })}`;
-
- return `Earn ${rewardAmountFormatted} after generating ${valueFormatted} ${attributeLabel}`;
-};
-
export function BountyLogic({ className }: { className?: string }) {
const { control, watch } = useAddEditBountyForm();
diff --git a/packages/prisma/schema/bounty.prisma b/packages/prisma/schema/bounty.prisma
index 0f6c3896bf..73be73619b 100644
--- a/packages/prisma/schema/bounty.prisma
+++ b/packages/prisma/schema/bounty.prisma
@@ -21,7 +21,7 @@ model Bounty {
id String @id @default(cuid())
programId String
workflowId String? @unique
- name String?
+ name String
description String? @db.Text
type BountyType
startsAt DateTime