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 workspace API for submitted lead comments

Rafi8c5b5264a743
diff --git a/apps/web/app/(ee)/api/programs/[programId]/submitted-leads/[leadId]/comments/route.ts b/apps/web/app/(ee)/api/programs/[programId]/submitted-leads/[leadId]/comments/route.ts
new file mode 100644
index 0000000000..2cca8e0afa
--- /dev/null
+++ b/apps/web/app/(ee)/api/programs/[programId]/submitted-leads/[leadId]/comments/route.ts
@@ -0,0 +1,38 @@
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { getSubmittedLeadOrThrow } from "@/lib/submitted-leads/get-submitted-lead-or-throw";
+import { SubmittedLeadCommentSchema } from "@/lib/zod/schemas/submitted-leads";
+import { NextResponse } from "next/server";
+import * as z from "zod/v4";
+
+// GET /api/programs/[programId]/submitted-leads/[leadId]/comments
+export const GET = withWorkspace(
+ async ({ workspace, params }) => {
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const lead = await getSubmittedLeadOrThrow({
+ leadId: params.leadId,
+ programId,
+ });
+
+ const comments = await prisma.submittedLeadComment.findMany({
+ where: {
+ leadId: lead.id,
+ },
+ include: {
+ user: true,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ return NextResponse.json(
+ z.array(SubmittedLeadCommentSchema).parse(comments),
+ );
+ },
+ {
+ requiredPlan: ["business", "advanced", "enterprise"],
+ },
+);
diff --git a/apps/web/lib/submitted-leads/create-submitted-lead-comment-action.ts b/apps/web/lib/submitted-leads/create-submitted-lead-comment-action.ts
new file mode 100644
index 0000000000..bad42188d8
--- /dev/null
+++ b/apps/web/lib/submitted-leads/create-submitted-lead-comment-action.ts
@@ -0,0 +1,47 @@
+"use server";
+
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { prisma } from "@/lib/prisma";
+import { getSubmittedLeadOrThrow } from "@/lib/submitted-leads/get-submitted-lead-or-throw";
+import {
+ SubmittedLeadCommentSchema,
+ createSubmittedLeadCommentSchema,
+} from "@/lib/zod/schemas/submitted-leads";
+import { authActionClient } from "../actions/safe-action";
+import { throwIfNoPermission } from "../actions/throw-if-no-permission";
+
+// Create a comment on a submitted lead
+export const createSubmittedLeadCommentAction = authActionClient
+ .inputSchema(createSubmittedLeadCommentSchema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace, user } = ctx;
+ const { leadId, text, partnerVisible } = parsedInput;
+
+ throwIfNoPermission({
+ role: workspace.role,
+ requiredPermissions: ["messages.write"],
+ });
+
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const lead = await getSubmittedLeadOrThrow({
+ leadId,
+ programId,
+ });
+
+ const comment = await prisma.submittedLeadComment.create({
+ data: {
+ leadId: lead.id,
+ userId: user.id,
+ text,
+ partnerVisible,
+ },
+ include: {
+ user: true,
+ },
+ });
+
+ return {
+ comment: SubmittedLeadCommentSchema.parse(comment),
+ };
+ });
diff --git a/apps/web/lib/submitted-leads/delete-submitted-lead-comment-action.ts b/apps/web/lib/submitted-leads/delete-submitted-lead-comment-action.ts
new file mode 100644
index 0000000000..2452ca2af7
--- /dev/null
+++ b/apps/web/lib/submitted-leads/delete-submitted-lead-comment-action.ts
@@ -0,0 +1,33 @@
+"use server";
+
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { prisma } from "@/lib/prisma";
+import { deleteSubmittedLeadCommentSchema } from "@/lib/zod/schemas/submitted-leads";
+import { authActionClient } from "../actions/safe-action";
+import { throwIfNoPermission } from "../actions/throw-if-no-permission";
+
+// Delete a comment on a submitted lead
+export const deleteSubmittedLeadCommentAction = authActionClient
+ .inputSchema(deleteSubmittedLeadCommentSchema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace, user } = ctx;
+ const { commentId } = parsedInput;
+
+ throwIfNoPermission({
+ role: workspace.role,
+ requiredPermissions: ["messages.write"],
+ });
+
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ await prisma.submittedLeadComment.delete({
+ where: {
+ id: commentId,
+ userId: user.id,
+ partnerId: null,
+ lead: {
+ programId,
+ },
+ },
+ });
+ });
diff --git a/apps/web/lib/submitted-leads/update-submitted-lead-comment-action.ts b/apps/web/lib/submitted-leads/update-submitted-lead-comment-action.ts
new file mode 100644
index 0000000000..ef8fbd1447
--- /dev/null
+++ b/apps/web/lib/submitted-leads/update-submitted-lead-comment-action.ts
@@ -0,0 +1,46 @@
+"use server";
+
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { prisma } from "@/lib/prisma";
+import {
+ SubmittedLeadCommentSchema,
+ updateSubmittedLeadCommentSchema,
+} from "@/lib/zod/schemas/submitted-leads";
+import { authActionClient } from "../actions/safe-action";
+import { throwIfNoPermission } from "../actions/throw-if-no-permission";
+
+// Update a comment on a submitted lead
+export const updateSubmittedLeadCommentAction = authActionClient
+ .inputSchema(updateSubmittedLeadCommentSchema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace, user } = ctx;
+ const { commentId, text } = parsedInput;
+
+ throwIfNoPermission({
+ role: workspace.role,
+ requiredPermissions: ["messages.write"],
+ });
+
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const comment = await prisma.submittedLeadComment.update({
+ where: {
+ id: commentId,
+ userId: user.id,
+ partnerId: null,
+ lead: {
+ programId,
+ },
+ },
+ data: {
+ text,
+ },
+ include: {
+ user: true,
+ },
+ });
+
+ return {
+ comment: SubmittedLeadCommentSchema.parse(comment),
+ };
+ });