Set and return resolvedAt

This commit is contained in:
Tom Moor committed 2024-06-22 14:14:48 -04:00
1 parent e977163431
commit 3c3d34f879
4 files changed
+32 -9

No files matched your search

+14 -2
View File
@@ -64,6 +64,9 @@ class Comment extends ParanoidModel<
@Column(DataType.UUID)
createdById: string;
@Column(DataType.DATE)
resolvedAt: Date | null;
@BelongsTo(() => User, "resolvedById")
resolvedBy: User | null;
@@ -97,7 +100,7 @@ class Comment extends ParanoidModel<
resolvedBy: User,
options?: SaveOptions<InferAttributes<Comment>>
) {
if (this.resolvedById) {
if (this.isResolved) {
throw ValidationError("Comment is already resolved");
}
if (this.parentCommentId) {
@@ -106,6 +109,7 @@ class Comment extends ParanoidModel<
this.resolvedById = resolvedBy.id;
this.resolvedBy = resolvedBy;
this.resolvedAt = new Date();
return this.save(options);
}
@@ -115,15 +119,23 @@ class Comment extends ParanoidModel<
* @param options The save options
*/
public unresolve(options?: SaveOptions<InferAttributes<Comment>>) {
if (!this.resolvedById) {
if (!this.isResolved) {
throw ValidationError("Comment is not resolved");
}
this.resolvedById = null;
this.resolvedBy = null;
this.resolvedAt = null;
return this.save(options);
}
/**
* Whether the comment is resolved
*/
public get isResolved() {
return !!this.resolvedAt;
}
/**
* Convert the comment data to plain text
*
+1
View File
@@ -9,6 +9,7 @@ export default function present(comment: Comment) {
parentCommentId: comment.parentCommentId,
createdBy: presentUser(comment.createdBy),
createdById: comment.createdById,
resolvedAt: comment.resolvedAt,
resolvedBy: comment.resolvedBy ? presentUser(comment.resolvedBy) : null,
resolvedById: comment.resolvedById,
createdAt: comment.createdAt,
+8 -7
View File
@@ -4,6 +4,7 @@ import {
buildCollection,
buildComment,
buildDocument,
buildResolvedComment,
buildTeam,
buildUser,
} from "@server/test/factories";
@@ -97,10 +98,9 @@ describe("#comments.list", () => {
userId: user.id,
documentId: document.id,
});
await buildComment({
await buildResolvedComment(user, {
userId: user.id,
documentId: document.id,
resolvedById: user.id,
});
const res = await server.post("/api/comments.list", {
body: {
@@ -190,10 +190,9 @@ describe("#comments.list", () => {
userId: user.id,
documentId: document.id,
});
const resolved = await buildComment({
const resolved = await buildResolvedComment(user, {
userId: user.id,
documentId: document.id,
resolvedById: user.id,
});
const res = await server.post("/api/comments.list", {
body: {
@@ -213,7 +212,7 @@ describe("#comments.list", () => {
expect(body.policies[0].abilities.resolve).toEqual(false);
});
it("should return all comments", async () => {
it("should return all unresolved comments", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection1 = await buildCollection({
@@ -408,6 +407,7 @@ describe("#comments.resolve", () => {
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.resolvedAt).toBeTruthy();
expect(body.data.resolvedById).toEqual(user.id);
expect(body.data.resolvedBy.id).toEqual(user.id);
expect(body.policies.length).toEqual(1);
@@ -463,10 +463,9 @@ describe("#comments.unresolve", () => {
teamId: user.teamId,
});
const comment = await buildComment({
const comment = await buildResolvedComment(user, {
userId: user.id,
documentId: document.id,
resolvedById: user.id,
});
const res = await server.post("/api/comments.unresolve", {
@@ -478,6 +477,8 @@ describe("#comments.unresolve", () => {
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.resolvedAt).toEqual(null);
expect(body.data.resolvedBy).toEqual(null);
expect(body.data.resolvedById).toEqual(null);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toEqual(true);
+9
View File
@@ -431,6 +431,15 @@ export async function buildComment(overrides: {
return comment;
}
export async function buildResolvedComment(
user: User,
overrides: Parameters<typeof buildComment>[0]
) {
const comment = await buildComment(overrides);
await comment.resolve(user);
return comment;
}
export async function buildFileOperation(
overrides: Partial<FileOperation> = {}
) {