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.

send BountyPendingReview email

Kiran Kebbaae723dd5
diff --git a/apps/web/lib/actions/partners/create-bounty-submission.ts b/apps/web/lib/actions/partners/create-bounty-submission.ts
index 2c07b21023..60e7f8457a 100644
--- a/apps/web/lib/actions/partners/create-bounty-submission.ts
+++ b/apps/web/lib/actions/partners/create-bounty-submission.ts
@@ -1,9 +1,15 @@
"use server";
import { getBountyOrThrow } from "@/lib/api/bounties/get-bounty-or-throw";
+import { getWorkspaceUsers } from "@/lib/api/get-workspace-users";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { BountySubmissionFileSchema } from "@/lib/zod/schemas/bounties";
+import { resend } from "@dub/email/resend";
+import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";
+import BountyPendingReview from "@dub/email/templates/bounty-pending-review";
import { prisma } from "@dub/prisma";
+import { Role } from "@prisma/client";
+import { waitUntil } from "@vercel/functions";
import { z } from "zod";
import { authPartnerActionClient } from "../safe-action";
@@ -21,7 +27,7 @@ export const createBountySubmissionAction = authPartnerActionClient
const { partner } = ctx;
const { programId, bountyId, files, urls, description } = parsedInput;
- await Promise.all([
+ const [programEnrollment, bounty] = await Promise.all([
getProgramEnrollmentOrThrow({
partnerId: partner.id,
programId,
@@ -42,7 +48,7 @@ export const createBountySubmissionAction = authPartnerActionClient
// Validate submission requirements if specified (max number of files/URLs)
// Validate file types and sizes if specified
- await prisma.bountySubmission.create({
+ const submission = await prisma.bountySubmission.create({
data: {
programId,
bountyId,
@@ -53,6 +59,45 @@ export const createBountySubmissionAction = authPartnerActionClient
},
});
+ waitUntil(
+ (async () => {
+ const { users, ...workspace } = await getWorkspaceUsers({
+ programId,
+ role: Role.owner,
+ });
+
+ if (users.length === 0) {
+ return;
+ }
+
+ await resend?.batch.send(
+ users.map((user) => ({
+ from: VARIANT_TO_FROM_MAP.notifications,
+ to: user.email,
+ subject: "Pending bounty review",
+ react: BountyPendingReview({
+ email: user.email,
+ workspace: {
+ slug: workspace.slug,
+ },
+ bounty: {
+ id: bounty.id,
+ name: bounty.name,
+ },
+ partner: {
+ name: partner.name,
+ image: partner.image,
+ email: partner.email!,
+ },
+ submission: {
+ id: submission.id,
+ },
+ }),
+ })),
+ );
+ })(),
+ );
+
return {
success: true,
};
diff --git a/apps/web/lib/api/get-workspace-users.ts b/apps/web/lib/api/get-workspace-users.ts
new file mode 100644
index 0000000000..35b56bf6b0
--- /dev/null
+++ b/apps/web/lib/api/get-workspace-users.ts
@@ -0,0 +1,75 @@
+import { prisma } from "@dub/prisma";
+import { NotificationPreference, Role } from "@prisma/client";
+
+type GetWorkspaceUsersParams =
+ | {
+ workspaceId: string;
+ programId?: never;
+ role: Role;
+ notificationPreference?: NotificationPreference;
+ }
+ | {
+ programId: string;
+ workspaceId?: never;
+ role: Role;
+ notificationPreference?: NotificationPreference;
+ };
+
+export async function getWorkspaceUsers({
+ workspaceId,
+ programId,
+ role,
+ notificationPreference,
+}: GetWorkspaceUsersParams) {
+ if (!workspaceId && !programId) {
+ throw new Error("Either workspaceId or programId must be provided.");
+ }
+
+ const workspace = await prisma.project.findUnique({
+ where: {
+ ...(workspaceId
+ ? {
+ id: workspaceId,
+ }
+ : {
+ defaultProgramId: programId,
+ }),
+ },
+ select: {
+ id: true,
+ slug: true,
+ name: true,
+ users: {
+ where: {
+ role,
+ ...(notificationPreference && { notificationPreference }),
+ },
+ select: {
+ user: {
+ select: {
+ name: true,
+ email: true,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ if (!workspace) {
+ throw new Error(`Workspace ${workspaceId} not found.`);
+ }
+
+ return {
+ id: workspace.id,
+ slug: workspace.slug,
+ name: workspace.name,
+ users: workspace.users
+ .map(({ user }) => user)
+ .filter((user) => user.email)
+ .map((user) => ({
+ name: user.name,
+ email: user.email!,
+ })),
+ };
+}