|
| 1 | +// apps/dashboard/server/lib/comments-service.ts |
| 2 | +import { eq } from "drizzle-orm" |
| 3 | +import { createError } from "h3" |
| 4 | +import type { DB } from "../db" |
| 5 | +import { db } from "../db" |
| 6 | +import { reportComments } from "../db/schema/report-comments" |
| 7 | +import { reports } from "../db/schema/reports" |
| 8 | +import { githubIntegrations } from "../db/schema/github-integrations" |
| 9 | +import { reportEvents } from "../db/schema/report-events" |
| 10 | +import { enqueueCommentUpsert } from "./enqueue-sync" |
| 11 | +import { publishReportStream } from "./report-events-bus" |
| 12 | + |
| 13 | +// Mirror the DbTransaction type pattern from triage.ts |
| 14 | +type DbTransaction = Parameters<DB["transaction"]>[0] extends (tx: infer T) => unknown ? T : never |
| 15 | + |
| 16 | +export interface AddReportCommentArgs { |
| 17 | + projectId: string |
| 18 | + reportId: string |
| 19 | + actorId: string |
| 20 | + actorClientId: string | null |
| 21 | + body: string |
| 22 | +} |
| 23 | + |
| 24 | +export interface AddReportCommentResult { |
| 25 | + comment: typeof reportComments.$inferSelect |
| 26 | + /** Forwarded to addReportCommentSideEffects to avoid a second DB lookup. */ |
| 27 | + githubIssueNumber: number | null |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Insert a dashboard-source comment on a report. Used by: |
| 32 | + * - The dashboard POST endpoint (actorClientId: null) |
| 33 | + * - MCP repro_add_comment (actorClientId: OAuth client_id) |
| 34 | + * |
| 35 | + * In-tx: validates the report belongs to the project, inserts the |
| 36 | + * report_comments row (source: "dashboard", actorClientId), inserts a |
| 37 | + * report_events row of kind "comment_added" (with actorClientId). |
| 38 | + * |
| 39 | + * Throws 404 if the report doesn't exist or doesn't match projectId. |
| 40 | + */ |
| 41 | +export async function addReportComment( |
| 42 | + tx: DbTransaction, |
| 43 | + args: AddReportCommentArgs, |
| 44 | +): Promise<AddReportCommentResult> { |
| 45 | + const [report] = await tx.select().from(reports).where(eq(reports.id, args.reportId)).limit(1) |
| 46 | + if (!report || report.projectId !== args.projectId) { |
| 47 | + throw createError({ statusCode: 404, statusMessage: "Report not found" }) |
| 48 | + } |
| 49 | + |
| 50 | + const [inserted] = await tx |
| 51 | + .insert(reportComments) |
| 52 | + .values({ |
| 53 | + reportId: args.reportId, |
| 54 | + userId: args.actorId, |
| 55 | + actorClientId: args.actorClientId, |
| 56 | + body: args.body, |
| 57 | + source: "dashboard", |
| 58 | + }) |
| 59 | + .returning() |
| 60 | + |
| 61 | + await tx.insert(reportEvents).values({ |
| 62 | + reportId: args.reportId, |
| 63 | + projectId: args.projectId, |
| 64 | + actorId: args.actorId, |
| 65 | + actorClientId: args.actorClientId, |
| 66 | + kind: "comment_added", |
| 67 | + payload: { commentId: inserted.id }, |
| 68 | + }) |
| 69 | + |
| 70 | + return { |
| 71 | + comment: inserted, |
| 72 | + githubIssueNumber: report.githubIssueNumber, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export interface AddReportCommentSideEffectArgs { |
| 77 | + projectId: string |
| 78 | + reportId: string |
| 79 | + commentId: string |
| 80 | + githubIssueNumber: number | null |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Run the side effects that must happen AFTER the transaction commits: |
| 85 | + * - enqueueCommentUpsert (push the comment to GitHub if integration is connected) |
| 86 | + * - publishReportStream (SSE event for dashboard live-updates) |
| 87 | + */ |
| 88 | +export async function addReportCommentSideEffects( |
| 89 | + args: AddReportCommentSideEffectArgs, |
| 90 | +): Promise<void> { |
| 91 | + if (args.githubIssueNumber !== null) { |
| 92 | + const [integration] = await db |
| 93 | + .select({ status: githubIntegrations.status }) |
| 94 | + .from(githubIntegrations) |
| 95 | + .where(eq(githubIntegrations.projectId, args.projectId)) |
| 96 | + .limit(1) |
| 97 | + |
| 98 | + if (integration?.status === "connected") { |
| 99 | + await enqueueCommentUpsert(args.reportId, args.commentId) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + publishReportStream(args.reportId, { |
| 104 | + kind: "comment_added", |
| 105 | + payload: { commentId: args.commentId }, |
| 106 | + }) |
| 107 | +} |
0 commit comments