Skip to content

Commit

Permalink
🥤 Update add comment actions args (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse authored May 10, 2024
1 parent ce97cfb commit 749a075
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationWithComments, "id" | "waveId" | "userId">;
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);
Expand All @@ -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") {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export function AddCommentForm({
action: (payload: AddCommentActionPayload) => Promise<void>,
) =>
form.handleSubmit(async ({ content }) => {
await action({ content, application });
await action({
content,
applicationId: application.id,
waveId: application.waveId,
});
setEditorKey((prev) => prev + 1);
form.reset();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ import { addReplyAction, AddReplyActionPayload } from "./addCommentAction";
import { addCommentSchema, type AddCommentSchema } from "./addCommentSchema";

interface CommentReplyFormProps
extends Pick<AddReplyActionPayload, "replyTargetId" | "application"> {
extends Pick<
AddReplyActionPayload,
"replyTargetId" | "applicationId" | "waveId"
> {
onReply: () => void;
}

export function CommentReplyForm({
application,
applicationId,
waveId,
replyTargetId,
onReply,
}: CommentReplyFormProps) {
Expand All @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export const CommentPreview = ({
</div>
{isReply && (
<CommentReplyForm
application={application}
applicationId={application.id}
waveId={application.waveId}
replyTargetId={id}
onReply={onReply}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};

Expand Down Expand Up @@ -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");
});
Expand All @@ -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");
});
Expand Down

0 comments on commit 749a075

Please sign in to comment.