Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥤 Update add comment actions args #471

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading