diff --git a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentActions.ts b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentActions.ts index e0de2b94..bc926c9d 100644 --- a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentActions.ts +++ b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentActions.ts @@ -93,25 +93,22 @@ export async function addReplyAction({ export interface UpdateCommentActionPayload { applicationId: ApplicationId; commentId: Comment["id"]; - content: Comment["content"]; + newContent: Comment["content"]; } export async function updateCommentAction({ applicationId, commentId, - content, + newContent, }: UpdateCommentActionPayload) { - const { userId, validationErrorMessage } = - await canEditComment(applicationId); + const { validationErrorMessage } = await canEditComment(applicationId); if (typeof validationErrorMessage !== "undefined") { throw new Error(validationErrorMessage); } await updateComment({ - content, - applicationId, - userId, - id: commentId, + newContent, + commentId, }); } diff --git a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentEditForm.tsx b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentEditForm.tsx index 87a09c1c..af9ab15c 100644 --- a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentEditForm.tsx +++ b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentEditForm.tsx @@ -5,7 +5,6 @@ import { useForm } from "react-hook-form"; import { ApplicationWithComments } from "@/types/Application"; import { type Comment } from "@/types/Comment"; -import { formatTime } from "@/lib/dates"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Editor } from "@/components/ui/editor/editor"; @@ -44,13 +43,10 @@ export function CommentEditForm({ }); const handleSubmit = form.handleSubmit(async ({ content }) => { - const appendContent = `\n\nEdited ${formatTime(new Date())}:\n\n${content}`; - const submitContent = comment.content + appendContent; - await updateCommentAction({ applicationId: application.id, commentId: comment.id, - content: submitContent, + newContent: content, }); form.reset(); onEdit(); diff --git a/src/drizzle/queries/comments.ts b/src/drizzle/queries/comments.ts index a8c1828e..25421435 100644 --- a/src/drizzle/queries/comments.ts +++ b/src/drizzle/queries/comments.ts @@ -1,13 +1,28 @@ import { db } from "@/drizzle/db"; import { Comment, Review } from "@/drizzle/schema"; -import { eq } from "drizzle-orm"; +import { eq, sql } from "drizzle-orm"; + +import { formatTime } from "@/lib/dates"; export async function insertComment(data: typeof Comment.$inferInsert) { return db.insert(Comment).values(data); } -export async function updateComment(data: typeof Comment.$inferInsert) { - return db.update(Comment).set(data).where(eq(Comment.id, data.id!)); +interface UpdateCommentArgs { + commentId: string; + newContent: string; +} + +export async function updateComment({ + commentId, + newContent, +}: UpdateCommentArgs) { + const formattedContent = `\n\nEdited ${formatTime(new Date())}:\n\n${newContent}`; + + return db + .update(Comment) + .set({ content: sql`${Comment.content} || ${formattedContent}` }) + .where(eq(Comment.id, commentId)); } export async function insertCommentAsReview(data: typeof Comment.$inferInsert) { diff --git a/tests/helpers/queries.ts b/tests/helpers/queries.ts index 6ffd0084..8b940c1e 100644 --- a/tests/helpers/queries.ts +++ b/tests/helpers/queries.ts @@ -2,6 +2,7 @@ import { db } from "@/drizzle/db"; import { Application, Category, + Comment, Moderator, Reviewer, User, @@ -125,3 +126,24 @@ export async function createApplication({ teamSummary: "teamSummary", }); } + +interface CreateCommentArgs { + commentId: string; + applicationId: string; + userId: string; + content: string; +} + +export async function createComment({ + commentId, + applicationId, + userId, + content, +}: CreateCommentArgs) { + await db.insert(Comment).values({ + id: commentId, + applicationId, + content, + userId, + }); +} diff --git a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/updateCommentAction.test.ts b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/updateCommentAction.test.ts index 711af42c..72db5ed9 100644 --- a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/updateCommentAction.test.ts +++ b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/updateCommentAction.test.ts @@ -5,6 +5,7 @@ import { createApplication, createBlocked, createCategory, + createComment, createModerator, createReviewer, createUser, @@ -13,10 +14,8 @@ import { } from "tests/helpers/queries"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { - addCommentAction, - updateCommentAction, -} from "@/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentActions"; +import { formatTime } from "@/lib/dates"; +import { updateCommentAction } from "@/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentActions"; const userId = "user"; const anotherUserId = "anotherUser"; @@ -25,18 +24,29 @@ const applicationId = "f8e46fab-f2c4-4c46-85ca-9e5cbf716d39"; const categoryId = "7979fbc1-1a84-4b75-8f7e-bea6f9bf0a99"; const commentId = "5c8c268c-3a34-43ec-b8b7-58a0cbe9adde"; -const addCommentActionArgs = { - applicationId, - waveId, - content: "Original comment", -}; +const initialCommentContent = "Original comment"; const updateCommentActionArgs = { applicationId, commentId, - content: "Updated comment", + newContent: "Updated comment", }; +const expectedContent = `${initialCommentContent} + +Edited ${formatTime(new Date())}: + +${updateCommentActionArgs.newContent}`; + +async function createUserComment() { + createComment({ + content: initialCommentContent, + applicationId, + commentId, + userId, + }); +} + describe("app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/updateCommentAction", () => { beforeEach(async () => { await createUser(anotherUserId); @@ -90,91 +100,46 @@ describe("app/waves/[waveId]/applications/[applicationId]/comments/addCommentFor it("device verified - own application", async () => { await createUser(userId); - mockUserSession({ userId, credentialType: "device" }); - const userApplicationId = "d14f44cd-7a64-4b1c-9731-47cee811e149"; - await createApplication({ - applicationId: userApplicationId, - categoryId, - userId, - waveId, - isDraft: false, - }); - - await addCommentAction({ - ...addCommentActionArgs, - applicationId: userApplicationId, - }); - - const comments = await db.query.Comment.findMany(); - expect(comments).toHaveLength(1); - - const comment = comments[0]; - - await updateCommentAction({ - ...updateCommentActionArgs, - applicationId: userApplicationId, - commentId: comment.id, - }); + mockUserSession({ userId, credentialType: "orb" }); + await createUserComment(); + + await updateCommentAction(updateCommentActionArgs); const updatedComments = await db.query.Comment.findMany(); - expect(updatedComments[0].content).toBe(updateCommentActionArgs.content); + expect(updatedComments[0].content).toBe(expectedContent); }); it("orb verified", async () => { await createUser(userId); mockUserSession({ userId, credentialType: "orb" }); + await createUserComment(); - await addCommentAction(addCommentActionArgs); - - const comments = await db.query.Comment.findMany(); - expect(comments).toHaveLength(1); - - const comment = comments[0]; - await updateCommentAction({ - ...updateCommentActionArgs, - commentId: comment.id, - }); + await updateCommentAction(updateCommentActionArgs); const updatedComments = await db.query.Comment.findMany(); - expect(updatedComments[0].content).toBe(updateCommentActionArgs.content); + expect(updatedComments[0].content).toBe(expectedContent); }); it("reviewer", async () => { await createReviewer(userId); mockUserSession({ userId, credentialType: "orb" }); + await createUserComment(); - await addCommentAction(addCommentActionArgs); - - const comments = await db.query.Comment.findMany(); - expect(comments).toHaveLength(1); - - const comment = comments[0]; - await updateCommentAction({ - ...updateCommentActionArgs, - commentId: comment.id, - }); + await updateCommentAction(updateCommentActionArgs); const updatedComments = await db.query.Comment.findMany(); - expect(updatedComments[0].content).toBe(updateCommentActionArgs.content); + expect(updatedComments[0].content).toBe(expectedContent); }); it("moderator", async () => { await createModerator(userId); mockUserSession({ userId, credentialType: "orb" }); + await createUserComment(); - await addCommentAction(addCommentActionArgs); - - const comments = await db.query.Comment.findMany(); - expect(comments).toHaveLength(1); - - const comment = comments[0]; - await updateCommentAction({ - ...updateCommentActionArgs, - commentId: comment.id, - }); + await updateCommentAction(updateCommentActionArgs); const updatedComments = await db.query.Comment.findMany(); - expect(updatedComments[0].content).toBe(updateCommentActionArgs.content); + expect(updatedComments[0].content).toBe(expectedContent); }); }); });