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 deletion for bounties with submissions

Steven Teyd55d940b85f1
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index a07fe87a39..8b0a2ac815 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -36,134 +36,177 @@ export const GET = withWorkspace(
return NextResponse.json(BountySchemaExtended.parse(bounty));
},
+ {
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
);
// PATCH /api/bounties/[bountyId] - update a bounty
-export const PATCH = withWorkspace(async ({ workspace, params, req }) => {
- const { bountyId } = params;
- const programId = getDefaultProgramIdOrThrow(workspace);
-
- const {
- name,
- description,
- startsAt,
- endsAt,
- rewardAmount,
- submissionRequirements,
- performanceCondition,
- groupIds,
- } = updateBountySchema.parse(await parseRequestBody(req));
-
- if (startsAt && endsAt && endsAt < startsAt) {
- throw new DubApiError({
- message: "endsAt must be on or after startsAt.",
- code: "bad_request",
+export const PATCH = withWorkspace(
+ async ({ workspace, params, req }) => {
+ const { bountyId } = params;
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const {
+ name,
+ description,
+ startsAt,
+ endsAt,
+ rewardAmount,
+ submissionRequirements,
+ performanceCondition,
+ groupIds,
+ } = updateBountySchema.parse(await parseRequestBody(req));
+
+ if (startsAt && endsAt && endsAt < startsAt) {
+ throw new DubApiError({
+ message: "endsAt must be on or after startsAt.",
+ code: "bad_request",
+ });
+ }
+ const bounty = await getBountyOrThrow({
+ bountyId,
+ programId,
});
- }
- const bounty = await getBountyOrThrow({
- bountyId,
- programId,
- });
- // TODO:
- // When we do archive, make sure it disables the workflow
+ // TODO:
+ // When we do archive, make sure it disables the workflow
- const groups = groupIds?.length
- ? await prisma.partnerGroup.findMany({
- where: {
- programId,
- id: {
- in: groupIds,
+ const groups = groupIds?.length
+ ? await prisma.partnerGroup.findMany({
+ where: {
+ programId,
+ id: {
+ in: groupIds,
+ },
},
- },
- })
- : [];
-
- const updatedBounty = await prisma.$transaction(async (tx) => {
- const updatedBounty = await tx.bounty.update({
- where: {
- id: bounty.id,
- },
- data: {
- name,
- description,
- startsAt: startsAt!, // Can remove the ! when we're on a newer TS version (currently 5.4.4)
- endsAt,
- rewardAmount,
- ...(bounty.type === "submission" && {
- submissionRequirements: submissionRequirements ?? Prisma.JsonNull,
- }),
- groups: {
- deleteMany: {},
- create: groups.map((group) => ({
- groupId: group.id,
- })),
- },
- },
- });
+ })
+ : [];
- if (updatedBounty.workflowId && performanceCondition) {
- await tx.workflow.update({
+ const updatedBounty = await prisma.$transaction(async (tx) => {
+ const updatedBounty = await tx.bounty.update({
where: {
- id: updatedBounty.workflowId,
+ id: bounty.id,
},
data: {
- triggerConditions: [performanceCondition],
+ name,
+ description,
+ startsAt: startsAt!, // Can remove the ! when we're on a newer TS version (currently 5.4.4)
+ endsAt,
+ rewardAmount,
+ ...(bounty.type === "submission" && {
+ submissionRequirements: submissionRequirements ?? Prisma.JsonNull,
+ }),
+ groups: {
+ deleteMany: {},
+ create: groups.map((group) => ({
+ groupId: group.id,
+ })),
+ },
},
});
- }
- return {
- ...updatedBounty,
- performanceCondition,
- };
- });
-
- waitUntil(
- sendWorkspaceWebhook({
- workspace,
- trigger: "bounty.updated",
- data: BountySchema.parse(updatedBounty),
- }),
- );
-
- return NextResponse.json(BountySchema.parse(updatedBounty));
-});
+ if (updatedBounty.workflowId && performanceCondition) {
+ await tx.workflow.update({
+ where: {
+ id: updatedBounty.workflowId,
+ },
+ data: {
+ triggerConditions: [performanceCondition],
+ },
+ });
+ }
-// DELETE /api/bounties/[bountyId] - delete a bounty
-export const DELETE = withWorkspace(async ({ workspace, params }) => {
- const { bountyId } = params;
- const programId = getDefaultProgramIdOrThrow(workspace);
-
- await getBountyOrThrow({
- bountyId,
- programId,
- });
-
- await prisma.$transaction(async (tx) => {
- const bounty = await tx.bounty.delete({
- where: {
- id: bountyId,
- },
+ return {
+ ...updatedBounty,
+ performanceCondition,
+ };
});
- await tx.bountySubmission.deleteMany({
- where: {
- bountyId,
- },
+ waitUntil(
+ sendWorkspaceWebhook({
+ workspace,
+ trigger: "bounty.updated",
+ data: BountySchema.parse(updatedBounty),
+ }),
+ );
+
+ return NextResponse.json(BountySchema.parse(updatedBounty));
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
+
+// DELETE /api/bounties/[bountyId] - delete a bounty
+export const DELETE = withWorkspace(
+ async ({ workspace, params }) => {
+ const { bountyId } = params;
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const bounty = await getBountyOrThrow({
+ bountyId,
+ programId,
+ includeExpandedFields: true,
});
- if (bounty.workflowId) {
- await tx.workflow.delete({
+ if (bounty.submissions.length > 0) {
+ throw new DubApiError({
+ message:
+ "Bounties with submissions cannot be deleted. You can archive them instead.",
+ code: "bad_request",
+ });
+ }
+
+ await prisma.$transaction(async (tx) => {
+ const bounty = await tx.bounty.delete({
where: {
- id: bounty.workflowId,
+ id: bountyId,
},
});
- }
- });
- // TODO:
- // We should also delete the files submitted for the bounty from R2.
+ await tx.bountySubmission.deleteMany({
+ where: {
+ bountyId,
+ },
+ });
- return NextResponse.json({ id: bountyId });
-});
+ if (bounty.workflowId) {
+ await tx.workflow.delete({
+ where: {
+ id: bounty.workflowId,
+ },
+ });
+ }
+ });
+
+ // TODO:
+ // We should also delete the files submitted for the bounty from R2.
+
+ return NextResponse.json({ id: bountyId });
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
index 245b118e14..1c3a2a23fa 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
@@ -54,6 +54,16 @@ export const GET = withWorkspace(
z.array(BountySubmissionExtendedSchema).parse(results),
);
},
+ {
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
);
// Get the submissions for a bounty of the type `submission`
diff --git a/apps/web/app/(ee)/api/bounties/count/route.ts b/apps/web/app/(ee)/api/bounties/count/route.ts
index 2991d690c9..c7ba39124d 100644
--- a/apps/web/app/(ee)/api/bounties/count/route.ts
+++ b/apps/web/app/(ee)/api/bounties/count/route.ts
@@ -12,31 +12,43 @@ const bountiesCountQuerySchema = z.object({
const statuses = Object.values(BountySubmissionStatus);
// GET /api/bounties/count
-export const GET = withWorkspace(async ({ workspace, searchParams }) => {
- const programId = getDefaultProgramIdOrThrow(workspace);
- const { groupBy } = bountiesCountQuerySchema.parse(searchParams);
+export const GET = withWorkspace(
+ async ({ workspace, searchParams }) => {
+ const programId = getDefaultProgramIdOrThrow(workspace);
+ const { groupBy } = bountiesCountQuerySchema.parse(searchParams);
- const count = await prisma.bountySubmission.groupBy({
- by: ["status"],
- where: {
- programId,
- },
- _count: true,
- });
+ const count = await prisma.bountySubmission.groupBy({
+ by: ["status"],
+ where: {
+ programId,
+ },
+ _count: true,
+ });
- const counts = count.map((c) => ({
- status: c.status,
- count: c._count,
- }));
+ const counts = count.map((c) => ({
+ status: c.status,
+ count: c._count,
+ }));
- statuses.forEach((status) => {
- if (!counts.some((c) => c.status === status)) {
- counts.push({
- status,
- count: 0,
- });
- }
- });
+ statuses.forEach((status) => {
+ if (!counts.some((c) => c.status === status)) {
+ counts.push({
+ status,
+ count: 0,
+ });
+ }
+ });
- return NextResponse.json(counts);
-});
+ return NextResponse.json(counts);
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 8ce126541c..31d1737874 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -23,17 +23,29 @@ import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// GET /api/bounties - get all bounties for a program
-export const GET = withWorkspace(async ({ workspace, searchParams }) => {
- const programId = getDefaultProgramIdOrThrow(workspace);
- const parsedInput = getBountiesQuerySchema.parse(searchParams);
+export const GET = withWorkspace(
+ async ({ workspace, searchParams }) => {
+ const programId = getDefaultProgramIdOrThrow(workspace);
+ const parsedInput = getBountiesQuerySchema.parse(searchParams);
- const bounties = await getBounties({
- ...parsedInput,
- programId,
- });
+ const bounties = await getBounties({
+ ...parsedInput,
+ programId,
+ });
- return NextResponse.json(bounties);
-});
+ return NextResponse.json(bounties);
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business plus",
+ "business extra",
+ "business max",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
// POST /api/bounties - create a bounty
export const POST = withWorkspace(
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-action-button.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-action-button.tsx
index d03948ac00..fd077de0c8 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-action-button.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-action-button.tsx
@@ -8,7 +8,7 @@ import { ThreeDots } from "@/ui/shared/icons";
import { Button, MenuItem, Popover } from "@dub/ui";
import { PenWriting, Trash } from "@dub/ui/icons";
import { Command } from "cmdk";
-import { useState } from "react";
+import { useMemo, useState } from "react";
import { toast } from "sonner";
import { useBountySheet } from "./add-edit-bounty-sheet";
@@ -26,6 +26,15 @@ export function BountyActionButton({
const [isOpen, setIsOpen] = useState(false);
const { id: workspaceId } = useWorkspace();
const { setShowCreateBountySheet, BountySheet } = useBountySheet({ bounty });
+ console.log("bounty.submissions", bounty.submissions);
+
+ const hasSubmissions = useMemo(
+ () =>
+ (bounty.submissions?.pending ?? 0) > 0 ||
+ (bounty.submissions?.approved ?? 0) > 0 ||
+ (bounty.submissions?.rejected ?? 0) > 0,
+ [bounty.submissions],
+ );
const { confirmModal: deleteModal, setShowConfirmModal: setShowDeleteModal } =
useConfirmModal({
@@ -88,6 +97,11 @@ export function BountyActionButton({
setIsOpen(false);
setShowDeleteModal(true);
}}
+ disabledTooltip={
+ hasSubmissions
+ ? "Bounties with submissions cannot be deleted."
+ : undefined
+ }
>
Delete bounty
</MenuItem>
diff --git a/packages/ui/src/menu-item.tsx b/packages/ui/src/menu-item.tsx
index 4c3270cf44..e69a9d49f3 100644
--- a/packages/ui/src/menu-item.tsx
+++ b/packages/ui/src/menu-item.tsx
@@ -8,6 +8,7 @@ import {
ReactNode,
} from "react";
import { Icon } from "./icons";
+import { DynamicTooltipWrapper } from "./tooltip";
export const menuItemVariants = cva(
[
@@ -21,6 +22,9 @@ export const menuItemVariants = cva(
default: "text-content-default hover:bg-bg-subtle",
danger: "text-content-error hover:bg-bg-error",
},
+ disabled: {
+ true: "opacity-50 cursor-default text-content-disabled hover:bg-bg-default",
+ },
},
},
);
@@ -33,6 +37,7 @@ type MenuItemProps<T extends ElementType> = PropsWithChildren<
variant?: "default" | "danger";
icon?: Icon | ReactNode;
shortcut?: string;
+ disabledTooltip?: string | ReactNode;
};
export function MenuItem<T extends ElementType>({
@@ -42,30 +47,40 @@ export function MenuItem<T extends ElementType>({
icon: Icon,
shortcut,
className,
+ disabledTooltip,
...rest
}: MenuItemProps<T>) {
const Component = as || "button";
return (
- <Component
- {...(as === "button" ? { type: "button" } : {})}
- className={cn(menuItemVariants({ variant }), className)}
- {...rest}
+ <DynamicTooltipWrapper
+ tooltipProps={{
+ content: disabledTooltip,
+ }}
>
- <div className="flex grow items-center gap-2">
- {Icon && (isReactNode(Icon) ? Icon : <Icon className="size-4" />)}
- {children}
- </div>
- {shortcut && (
- <kbd
- className={cn(
- "bg-bg-inverted/5 group-hover:bg-bg-inverted/10 hidden rounded px-2 py-0.5 text-xs font-light transition-all duration-75 md:block",
- )}
- >
- {shortcut}
- </kbd>
- )}
- </Component>
+ <Component
+ {...(as === "button" ? { type: "button" } : {})}
+ className={cn(
+ menuItemVariants({ variant, disabled: !!disabledTooltip }),
+ className,
+ )}
+ {...rest}
+ >
+ <div className="flex grow items-center gap-2">
+ {Icon && (isReactNode(Icon) ? Icon : <Icon className="size-4" />)}
+ {children}
+ </div>
+ {shortcut && (
+ <kbd
+ className={cn(
+ "bg-bg-inverted/5 group-hover:bg-bg-inverted/10 hidden rounded px-2 py-0.5 text-xs font-light transition-all duration-75 md:block",
+ )}
+ >
+ {shortcut}
+ </kbd>
+ )}
+ </Component>
+ </DynamicTooltipWrapper>
);
}