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.

return early if invalid groupIds

Steven Tey130d30d0174a
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index 861bb530ea..b9425626b8 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -1,5 +1,6 @@
import { getBountyWithDetails } from "@/lib/api/bounties/get-bounty-with-details";
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";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -9,16 +10,11 @@ import {
BountySchemaExtended,
updateBountySchema,
} from "@/lib/zod/schemas/bounties";
-import { booleanQuerySchema } from "@/lib/zod/schemas/misc";
import { prisma } from "@dub/prisma";
-import { Prisma } from "@prisma/client";
+import { arrayEqual } from "@dub/utils";
+import { PartnerGroup, Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
-import { z } from "zod";
-
-const schema = z.object({
- includeExpandedFields: booleanQuerySchema.optional().default("false"),
-});
// GET /api/bounties/[bountyId] - get a bounty
export const GET = withWorkspace(
@@ -74,21 +70,28 @@ export const PATCH = withWorkspace(
id: bountyId,
programId,
},
+ include: {
+ groups: true,
+ },
});
// TODO:
// When we do archive, make sure it disables the workflow
- const groups = groupIds?.length
- ? await prisma.partnerGroup.findMany({
- where: {
- programId,
- id: {
- in: groupIds,
- },
- },
- })
- : [];
+ // if groupIds is provided and is different from the current groupIds, update the groups
+ let updatedPartnerGroups: PartnerGroup[] | undefined = undefined;
+ if (
+ groupIds &&
+ !arrayEqual(
+ bounty.groups.map((group) => group.groupId),
+ groupIds,
+ )
+ ) {
+ updatedPartnerGroups = await throwIfInvalidGroupIds({
+ programId,
+ groupIds,
+ });
+ }
const updatedBounty = await prisma.$transaction(async (tx) => {
const updatedBounty = await tx.bounty.update({
@@ -105,12 +108,14 @@ export const PATCH = withWorkspace(
submissionRequirements !== undefined && {
submissionRequirements: submissionRequirements ?? Prisma.JsonNull,
}),
- groups: {
- deleteMany: {},
- create: groups.map((group) => ({
- groupId: group.id,
- })),
- },
+ ...(updatedPartnerGroups && {
+ groups: {
+ deleteMany: {},
+ create: updatedPartnerGroups.map((group) => ({
+ groupId: group.id,
+ })),
+ },
+ }),
},
});
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 52c6420f2d..e5021a2161 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -1,5 +1,6 @@
import { createId } from "@/lib/api/create-id";
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";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -90,16 +91,10 @@ export const POST = withWorkspace(
});
}
- const groups = groupIds?.length
- ? await prisma.partnerGroup.findMany({
- where: {
- programId,
- id: {
- in: groupIds,
- },
- },
- })
- : null;
+ const partnerGroups = await throwIfInvalidGroupIds({
+ programId,
+ groupIds,
+ });
const bounty = await prisma.$transaction(async (tx) => {
let workflow: Workflow | null = null;
@@ -142,10 +137,10 @@ export const POST = withWorkspace(
type === "submission" && {
submissionRequirements,
}),
- ...(groups?.length && {
+ ...(partnerGroups.length && {
groups: {
createMany: {
- data: groups.map(({ id }) => ({
+ data: partnerGroups.map(({ id }) => ({
groupId: id,
})),
},
diff --git a/apps/web/app/(ee)/api/cron/bounties/published/route.ts b/apps/web/app/(ee)/api/cron/bounties/published/route.ts
index 5dd2af14d8..37ce804918 100644
--- a/apps/web/app/(ee)/api/cron/bounties/published/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/published/route.ts
@@ -1,9 +1,6 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { qstash } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
-import { resend } from "@dub/email/resend";
-import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";
-import NewBountyAvailable from "@dub/email/templates/new-bounty-available";
import { prisma } from "@dub/prisma";
import { APP_DOMAIN_WITH_NGROK, log } from "@dub/utils";
import { z } from "zod";
@@ -79,31 +76,34 @@ export async function POST(req: Request) {
);
}
- await resend?.batch.send(
- programEnrollments
- .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}`,
- },
- })),
+ console.log(
+ `Sending emails to ${programEnrollments.length} partners: ${programEnrollments.map(({ partner }) => partner.email).join(", ")}`,
);
+ // await resend?.batch.send(
+ // programEnrollments
+ // .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}`,
+ // },
+ // })),
+ // );
// Trigger next page
const nextPage = page + 1;
diff --git a/apps/web/lib/api/groups/throw-if-invalid-group-ids.ts b/apps/web/lib/api/groups/throw-if-invalid-group-ids.ts
new file mode 100644
index 0000000000..8b1b40dbc1
--- /dev/null
+++ b/apps/web/lib/api/groups/throw-if-invalid-group-ids.ts
@@ -0,0 +1,37 @@
+import { prisma } from "@dub/prisma";
+import { PartnerGroup } from "@dub/prisma/client";
+import { DubApiError } from "../errors";
+
+export async function throwIfInvalidGroupIds({
+ programId,
+ groupIds,
+}: {
+ programId: string;
+ groupIds: string[] | null;
+}) {
+ let partnerGroups: PartnerGroup[] = [];
+
+ if (groupIds && groupIds.length) {
+ partnerGroups = await prisma.partnerGroup.findMany({
+ where: {
+ programId,
+ id: {
+ in: groupIds,
+ },
+ },
+ });
+
+ const invalidGroupIds = groupIds?.filter(
+ (groupId) => !partnerGroups?.some((group) => group.id === groupId),
+ );
+
+ if (invalidGroupIds?.length) {
+ throw new DubApiError({
+ message: `Invalid group IDs detected: ${invalidGroupIds.join(", ")}`,
+ code: "bad_request",
+ });
+ }
+ }
+
+ return partnerGroups;
+}
diff --git a/apps/web/ui/partners/partner-application-sheet.tsx b/apps/web/ui/partners/partner-application-sheet.tsx
index 3b2135785f..77ab04f3bc 100644
--- a/apps/web/ui/partners/partner-application-sheet.tsx
+++ b/apps/web/ui/partners/partner-application-sheet.tsx
@@ -418,7 +418,7 @@ function PartnerRejectButton({
onSuccess: async () => {
await mutatePrefix("/api/partners");
toast.success(
- "Application rejected. No email sent, and can they reapply in 30 days.",
+ "Application rejected. No email sent, and they can reapply in 30 days.",
);
setIsOpen(false);
},
diff --git a/packages/utils/src/functions/array-equal.ts b/packages/utils/src/functions/array-equal.ts
new file mode 100644
index 0000000000..c403e61154
--- /dev/null
+++ b/packages/utils/src/functions/array-equal.ts
@@ -0,0 +1,19 @@
+export const arrayEqual = (
+ a: string[],
+ b: string[],
+ {
+ sameOrder = false,
+ }: {
+ sameOrder?: boolean;
+ } = {},
+) => {
+ if (a.length !== b.length) {
+ return false;
+ }
+
+ if (sameOrder) {
+ return a.every((item, index) => item === b[index]);
+ }
+
+ return a.every((item) => b.includes(item));
+};
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index 68d189fcbb..9b7e877b4f 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -1,3 +1,4 @@
+export * from "./array-equal";
export * from "./camel-case";
export * from "./capitalize";
export * from "./chunk";