From aeb546706cfe37e27b4dc7854b28c2c817e384ec Mon Sep 17 00:00:00 2001 From: nezouse Date: Fri, 10 May 2024 14:27:13 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=A4=20Update=20add=20coment=20actions?= =?UTF-8?q?=20args?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../addCommentForm/addCommentAction.ts | 31 +++++++------------ .../addCommentForm/addCommentForm.tsx | 6 +++- .../addCommentForm/commentReplyForm.tsx | 10 ++++-- .../comments/comment/commentPreview.tsx | 3 +- .../addCommentForm/addCommentAction.test.ts | 20 +++++++----- .../addCommentForm/addReplyAction.test.ts | 7 ++--- .../addCommentForm/addReviewAction.test.ts | 11 +++---- 7 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.ts b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.ts index ce95e73a..3fa064da 100644 --- a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.ts +++ b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.ts @@ -7,26 +7,23 @@ import { insertCommentAsReview, } from "@/drizzle/queries/comments"; -import { type ApplicationWithComments } from "@/types/Application"; +import { type ApplicationId } from "@/types/Application"; import { canAddComment, canAddReview } from "@/config/actionPermissions"; import { type AddCommentSchema } from "./addCommentSchema"; export interface AddCommentActionPayload { - application: Pick; + applicationId: ApplicationId; + waveId: number; content: AddCommentSchema["content"]; } export async function addCommentAction({ content, - application, + applicationId, + waveId, }: AddCommentActionPayload) { - const applicationId = application.id; - const waveId = application.waveId; - - const { userId, validationErrorMessage } = await canAddComment( - application.id, - ); + const { userId, validationErrorMessage } = await canAddComment(applicationId); if (typeof validationErrorMessage !== "undefined") { throw new Error(validationErrorMessage); @@ -43,11 +40,9 @@ export async function addCommentAction({ export async function addReviewAction({ content, - application, + applicationId, + waveId, }: AddCommentActionPayload) { - const applicationId = application.id; - const waveId = application.waveId; - const { userId, validationErrorMessage } = await canAddReview(applicationId); if (typeof validationErrorMessage !== "undefined") { @@ -69,15 +64,11 @@ export interface AddReplyActionPayload extends AddCommentActionPayload { export async function addReplyAction({ content, - application, + applicationId, + waveId, replyTargetId, }: AddReplyActionPayload) { - const applicationId = application.id; - const waveId = application.waveId; - - const { userId, validationErrorMessage } = await canAddComment( - application.id, - ); + const { userId, validationErrorMessage } = await canAddComment(applicationId); if (typeof validationErrorMessage !== "undefined") { throw new Error(validationErrorMessage); diff --git a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentForm.tsx b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentForm.tsx index 550e78e4..7835a6d3 100644 --- a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentForm.tsx +++ b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentForm.tsx @@ -53,7 +53,11 @@ export function AddCommentForm({ action: (payload: AddCommentActionPayload) => Promise, ) => form.handleSubmit(async ({ content }) => { - await action({ content, application }); + await action({ + content, + applicationId: application.id, + waveId: application.waveId, + }); setEditorKey((prev) => prev + 1); form.reset(); }); diff --git a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentReplyForm.tsx b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentReplyForm.tsx index 1bb43760..1a27207a 100644 --- a/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentReplyForm.tsx +++ b/src/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/commentReplyForm.tsx @@ -19,12 +19,16 @@ import { addReplyAction, AddReplyActionPayload } from "./addCommentAction"; import { addCommentSchema, type AddCommentSchema } from "./addCommentSchema"; interface CommentReplyFormProps - extends Pick { + extends Pick< + AddReplyActionPayload, + "replyTargetId" | "applicationId" | "waveId" + > { onReply: () => void; } export function CommentReplyForm({ - application, + applicationId, + waveId, replyTargetId, onReply, }: CommentReplyFormProps) { @@ -36,7 +40,7 @@ export function CommentReplyForm({ }); const handleSubmit = form.handleSubmit(async ({ content }) => { - await addReplyAction({ content, application, replyTargetId }); + await addReplyAction({ content, applicationId, waveId, replyTargetId }); form.reset(); onReply(); }); diff --git a/src/app/waves/[waveId]/applications/[applicationId]/comments/comment/commentPreview.tsx b/src/app/waves/[waveId]/applications/[applicationId]/comments/comment/commentPreview.tsx index 87f85264..ff9caf15 100644 --- a/src/app/waves/[waveId]/applications/[applicationId]/comments/comment/commentPreview.tsx +++ b/src/app/waves/[waveId]/applications/[applicationId]/comments/comment/commentPreview.tsx @@ -94,7 +94,8 @@ export const CommentPreview = ({ {isReply && ( diff --git a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.test.ts b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.test.ts index 12dbb098..cea16aba 100644 --- a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.test.ts +++ b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addCommentAction.test.ts @@ -22,12 +22,8 @@ const applicationId = "f8e46fab-f2c4-4c46-85ca-9e5cbf716d39"; const categoryId = "7979fbc1-1a84-4b75-8f7e-bea6f9bf0a99"; const defaultActionArgs = { - application: { - id: applicationId, - userId: anotherUserId, - waveId, - comments: [], - }, + applicationId, + waveId, content: "comment", }; @@ -82,11 +78,19 @@ describe("app/waves/[waveId]/applications/[applicationId]/comments/addCommentFor it("device verified - own application", async () => { await createUser(userId); - mockUserSession({ userId, credentialType: "orb" }); + mockUserSession({ userId, credentialType: "device" }); + const userApplicationId = "d14f44cd-7a64-4b1c-9731-47cee811e149"; + await createApplication({ + applicationId: userApplicationId, + categoryId, + userId, + waveId, + isDraft: false, + }); await addCommentAction({ ...defaultActionArgs, - application: { ...defaultActionArgs.application, userId }, + applicationId: userApplicationId, }); const comments = await db.query.Comment.findMany(); diff --git a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReplyAction.test.ts b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReplyAction.test.ts index bd4053be..bffef642 100644 --- a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReplyAction.test.ts +++ b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReplyAction.test.ts @@ -22,11 +22,8 @@ const applicationId = "f8e46fab-f2c4-4c46-85ca-9e5cbf716d39"; const categoryId = "7979fbc1-1a84-4b75-8f7e-bea6f9bf0a99"; const defaultActionArgs = { - application: { - id: applicationId, - userId: anotherUserId, - waveId, - }, + applicationId, + waveId, content: "comment", replyTargetId: "cc7565e4-2a8d-4716-a91a-4c86854c762b", }; diff --git a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReviewAction.test.ts b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReviewAction.test.ts index 191fb4e8..5849c942 100644 --- a/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReviewAction.test.ts +++ b/tests/unit/app/waves/[waveId]/applications/[applicationId]/comments/addCommentForm/addReviewAction.test.ts @@ -22,11 +22,8 @@ const applicationId = "f8e46fab-f2c4-4c46-85ca-9e5cbf716d39"; const categoryId = "7979fbc1-1a84-4b75-8f7e-bea6f9bf0a99"; const defaultActionArgs = { - application: { - id: applicationId, - userId: anotherUserId, - waveId, - }, + applicationId, + waveId, content: "comment", }; @@ -210,7 +207,7 @@ describe("app/waves/[waveId]/applications/[applicationId]/comments/addCommentFor expect(() => addReviewAction({ ...defaultActionArgs, - application: { id: newApplicationId, userId, waveId }, + applicationId: newApplicationId, }), ).rejects.toThrowError("You cannot review your own submission"); }); @@ -223,7 +220,7 @@ describe("app/waves/[waveId]/applications/[applicationId]/comments/addCommentFor expect(() => addReviewAction({ ...defaultActionArgs, - application: { id: newApplicationId, userId, waveId }, + applicationId: newApplicationId, }), ).rejects.toThrowError("Application not found"); });