diff --git a/server/models/Comment.ts b/server/models/Comment.ts index f1b993af41..0d4851fd3f 100644 --- a/server/models/Comment.ts +++ b/server/models/Comment.ts @@ -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> ) { - 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>) { - 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 * diff --git a/server/presenters/comment.ts b/server/presenters/comment.ts index 282b59803b..9267fe7a66 100644 --- a/server/presenters/comment.ts +++ b/server/presenters/comment.ts @@ -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, diff --git a/server/routes/api/comments/comments.test.ts b/server/routes/api/comments/comments.test.ts index 06bfec5c8b..0e8ceef12d 100644 --- a/server/routes/api/comments/comments.test.ts +++ b/server/routes/api/comments/comments.test.ts @@ -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); diff --git a/server/test/factories.ts b/server/test/factories.ts index 8674b8fa04..69e3c705d6 100644 --- a/server/test/factories.ts +++ b/server/test/factories.ts @@ -431,6 +431,15 @@ export async function buildComment(overrides: { return comment; } +export async function buildResolvedComment( + user: User, + overrides: Parameters[0] +) { + const comment = await buildComment(overrides); + await comment.resolve(user); + return comment; +} + export async function buildFileOperation( overrides: Partial = {} ) {