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.

add validateBounty

Kiran K83864c6fe5e6
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index 5df49613de..508c9e5c32 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -1,6 +1,7 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { generatePerformanceBountyName } from "@/lib/api/bounties/generate-performance-bounty-name";
import { getBountyWithDetails } from "@/lib/api/bounties/get-bounty-with-details";
+import { validateBounty } from "@/lib/api/bounties/validate-bounty";
import { DubApiError } from "@/lib/api/errors";
import { throwIfInvalidGroupIds } from "@/lib/api/groups/throw-if-invalid-group-ids";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
@@ -66,32 +67,6 @@ export const PATCH = withWorkspace(
groupIds,
} = updateBountySchema.parse(await parseRequestBody(req));
- if (startsAt && endsAt && endsAt < startsAt) {
- throw new DubApiError({
- message:
- "Bounty end date (endsAt) must be on or after start date (startsAt).",
- code: "bad_request",
- });
- }
-
- if (submissionsOpenAt) {
- if (startsAt && submissionsOpenAt < startsAt) {
- throw new DubApiError({
- message:
- "Bounty submissions open date (submissionsOpenAt) must be on or after start date (startsAt).",
- code: "bad_request",
- });
- }
-
- if (endsAt && submissionsOpenAt > endsAt) {
- throw new DubApiError({
- message:
- "Bounty submissions open date (submissionsOpenAt) must be on or before end date (endsAt).",
- code: "bad_request",
- });
- }
- }
-
const bounty = await prisma.bounty.findUniqueOrThrow({
where: {
id: bountyId,
@@ -108,20 +83,15 @@ export const PATCH = withWorkspace(
},
});
- if (rewardAmount === null || rewardAmount === 0) {
- if (bounty.type === "performance") {
- throw new DubApiError({
- code: "bad_request",
- message: "Reward amount is required for performance bounties",
- });
- } else if (!rewardDescription) {
- throw new DubApiError({
- code: "bad_request",
- message:
- "For submission bounties, either reward amount or reward description is required",
- });
- }
- }
+ validateBounty({
+ type: bounty.type,
+ startsAt,
+ endsAt,
+ submissionsOpenAt,
+ rewardAmount,
+ rewardDescription,
+ performanceScope: bounty.performanceScope,
+ });
// TODO:
// When we do archive, make sure it disables the workflow
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 96448276b6..41734d2b6b 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -1,5 +1,6 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { generatePerformanceBountyName } from "@/lib/api/bounties/generate-performance-bounty-name";
+import { validateBounty } from "@/lib/api/bounties/validate-bounty";
import { createId } from "@/lib/api/create-id";
import { DubApiError } from "@/lib/api/errors";
import { throwIfInvalidGroupIds } from "@/lib/api/groups/throw-if-invalid-group-ids";
@@ -123,53 +124,15 @@ export const POST = withWorkspace(
// Use current date as default if startsAt is not provided
startsAt = startsAt || new Date();
- if (endsAt && endsAt < startsAt) {
- throw new DubApiError({
- message:
- "Bounty end date (endsAt) must be on or after start date (startsAt).",
- code: "bad_request",
- });
- }
-
- if (submissionsOpenAt) {
- if (submissionsOpenAt < startsAt) {
- throw new DubApiError({
- message:
- "Bounty submissions open date (submissionsOpenAt) must be on or after start date (startsAt).",
- code: "bad_request",
- });
- }
-
- if (endsAt && submissionsOpenAt > endsAt) {
- throw new DubApiError({
- message:
- "Bounty submissions open date (submissionsOpenAt) must be on or before end date (endsAt).",
- code: "bad_request",
- });
- }
- }
-
- if (!rewardAmount) {
- if (type === "performance") {
- throw new DubApiError({
- code: "bad_request",
- message: "Reward amount is required for performance bounties.",
- });
- } else if (!rewardDescription) {
- throw new DubApiError({
- code: "bad_request",
- message:
- "For submission bounties, either reward amount or reward description is required.",
- });
- }
- }
-
- if (!performanceScope && type === "performance") {
- throw new DubApiError({
- code: "bad_request",
- message: "performanceScope must be set for performance bounties.",
- });
- }
+ validateBounty({
+ type,
+ startsAt,
+ endsAt,
+ submissionsOpenAt,
+ rewardAmount,
+ rewardDescription,
+ performanceScope,
+ });
const partnerGroups = await throwIfInvalidGroupIds({
programId,
diff --git a/apps/web/lib/api/bounties/validate-bounty.ts b/apps/web/lib/api/bounties/validate-bounty.ts
new file mode 100644
index 0000000000..2502b1296a
--- /dev/null
+++ b/apps/web/lib/api/bounties/validate-bounty.ts
@@ -0,0 +1,65 @@
+import { createBountySchema } from "@/lib/zod/schemas/bounties";
+import { z } from "zod";
+import { DubApiError } from "../errors";
+
+type CreateBountyInput = Partial<z.infer<typeof createBountySchema>>;
+
+export async function validateBounty({
+ type,
+ startsAt,
+ endsAt,
+ submissionsOpenAt,
+ rewardAmount,
+ rewardDescription,
+ performanceScope,
+}: CreateBountyInput) {
+ startsAt = startsAt || new Date();
+
+ if (endsAt && endsAt < startsAt) {
+ throw new DubApiError({
+ message:
+ "Bounty end date (endsAt) must be on or after start date (startsAt).",
+ code: "bad_request",
+ });
+ }
+
+ if (submissionsOpenAt) {
+ if (submissionsOpenAt < startsAt) {
+ throw new DubApiError({
+ message:
+ "Bounty submissions open date (submissionsOpenAt) must be on or after start date (startsAt).",
+ code: "bad_request",
+ });
+ }
+
+ if (endsAt && submissionsOpenAt > endsAt) {
+ throw new DubApiError({
+ message:
+ "Bounty submissions open date (submissionsOpenAt) must be on or before end date (endsAt).",
+ code: "bad_request",
+ });
+ }
+ }
+
+ if (rewardAmount === null || rewardAmount === 0) {
+ if (type === "performance") {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Reward amount is required for performance bounties.",
+ });
+ } else if (!rewardDescription) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "For submission bounties, either reward amount or reward description is required.",
+ });
+ }
+ }
+
+ if (!performanceScope && type === "performance") {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "performanceScope must be set for performance bounties.",
+ });
+ }
+}