Skip to content

Commit

Permalink
Move append to db layer
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse committed May 10, 2024
1 parent 8278077 commit 6183c5f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down
21 changes: 18 additions & 3 deletions src/drizzle/queries/comments.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions tests/helpers/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { db } from "@/drizzle/db";
import {
Application,
Category,
Comment,
Moderator,
Reviewer,
User,
Expand Down Expand Up @@ -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,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createApplication,
createBlocked,
createCategory,
createComment,
createModerator,
createReviewer,
createUser,
Expand All @@ -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";
Expand All @@ -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);
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 6183c5f

Please sign in to comment.