-
Notifications
You must be signed in to change notification settings - Fork 1
OUT-2917: public api to list comments of a task #1088
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
base: feature/api-improvements
Are you sure you want to change the base?
Changes from 3 commits
3826e62
f348f39
fb80e8e
ef83070
ede8aa7
a0774f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { sendCommentCreateNotifications } from '@/jobs/notifications' | ||
| import { sendReplyCreateNotifications } from '@/jobs/notifications/send-reply-create-notifications' | ||
| import { InitiatedEntity } from '@/types/common' | ||
| import { CreateComment, UpdateComment } from '@/types/dto/comment.dto' | ||
| import { CommentsPublicFilterType, CommentWithAttachments, CreateComment, UpdateComment } from '@/types/dto/comment.dto' | ||
| import { getArrayDifference, getArrayIntersection } from '@/utils/array' | ||
| import { CommentAddedSchema } from '@api/activity-logs/schemas/CommentAddedSchema' | ||
| import { ActivityLogger } from '@api/activity-logs/services/activity-logger.service' | ||
|
|
@@ -12,7 +12,7 @@ import { PoliciesService } from '@api/core/services/policies.service' | |
| import { Resource } from '@api/core/types/api' | ||
| import { UserAction } from '@api/core/types/user' | ||
| import { TasksService } from '@api/tasks/tasks.service' | ||
| import { ActivityType, Comment, CommentInitiator } from '@prisma/client' | ||
| import { ActivityType, Comment, CommentInitiator, Prisma } from '@prisma/client' | ||
| import httpStatus from 'http-status' | ||
| import { z } from 'zod' | ||
|
|
||
|
|
@@ -266,4 +266,40 @@ export class CommentService extends BaseService { | |
| return { ...comment, initiator } | ||
| }) | ||
| } | ||
|
|
||
| async getAllComments(queryFilters: CommentsPublicFilterType): Promise<CommentWithAttachments[]> { | ||
| const { parentId, taskId, limit, lastIdCursor, initiatorId } = queryFilters | ||
| const where = { | ||
| parentId, | ||
| taskId, | ||
| initiatorId, | ||
| workspaceId: this.user.workspaceId, | ||
| } | ||
|
|
||
| const pagination: Prisma.CommentFindManyArgs = { | ||
| take: limit, | ||
| cursor: lastIdCursor ? { id: lastIdCursor } : undefined, | ||
| skip: lastIdCursor ? 1 : undefined, | ||
| } | ||
SandipBajracharya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return await this.db.comment.findMany({ | ||
| where, | ||
| ...pagination, | ||
| include: { attachments: true }, | ||
| orderBy: { createdAt: 'desc' }, | ||
| }) | ||
| } | ||
|
Comment on lines
+282
to
+288
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the comments table properly indexed here, can you please check
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. created composite index with |
||
|
|
||
| async hasMoreCommentsAfterCursor( | ||
| id: string, | ||
| publicFilters: Partial<Parameters<CommentService['getAllComments']>[0]>, | ||
SandipBajracharya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): Promise<boolean> { | ||
| const newComment = await this.db.comment.findFirst({ | ||
| where: { ...publicFilters, workspaceId: this.user.workspaceId }, | ||
| cursor: { id }, | ||
| skip: 1, | ||
| orderBy: { createdAt: 'desc' }, | ||
| }) | ||
| return !!newComment | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { CommentService } from '@/app/api/comment/comment.service' | ||
| import authenticate from '@/app/api/core/utils/authenticate' | ||
| import { defaultLimit } from '@/constants/public-api' | ||
| import { getSearchParams } from '@/utils/request' | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
| import { decode, encode } from 'js-base64' | ||
| import { PublicCommentSerializer } from '@/app/api/comment/public/comment-public.serializer' | ||
| import { CommentsPublicFilterType } from '@/types/dto/comment.dto' | ||
| import { IdParams } from '@/app/api/core/types/api' | ||
|
|
||
| export const getAllCommentsPublicForTask = async (req: NextRequest, { params }: IdParams) => { | ||
| const { id } = await params | ||
| const user = await authenticate(req) | ||
|
|
||
| const { parentCommentId, createdBy, limit, nextToken } = getSearchParams(req.nextUrl.searchParams, [ | ||
| 'parentCommentId', | ||
| 'createdBy', | ||
| 'limit', | ||
| 'nextToken', | ||
| ]) | ||
|
|
||
| const publicFilters: CommentsPublicFilterType = { | ||
| taskId: id, | ||
| parentId: (parentCommentId === 'null' ? null : parentCommentId) || undefined, | ||
SandipBajracharya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| initiatorId: createdBy || undefined, | ||
| } | ||
|
|
||
| const commentService = new CommentService(user) | ||
| const comments = await commentService.getAllComments({ | ||
| limit: limit ? +limit : defaultLimit, | ||
SandipBajracharya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| lastIdCursor: nextToken ? decode(nextToken) : undefined, | ||
| ...publicFilters, | ||
| }) | ||
|
|
||
| const lastCommentId = comments[comments.length - 1]?.id | ||
| const hasMoreComments = lastCommentId | ||
| ? await commentService.hasMoreCommentsAfterCursor(lastCommentId, publicFilters) | ||
| : false | ||
| const base64NextToken = hasMoreComments ? encode(lastCommentId) : undefined | ||
|
|
||
| return NextResponse.json({ | ||
| data: await PublicCommentSerializer.serializeMany(comments), | ||
| nextToken: base64NextToken, | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { RFC3339DateSchema } from '@/types/common' | ||
| import { AssigneeType } from '@prisma/client' | ||
| import z from 'zod' | ||
|
|
||
| export const PublicAttachmentDtoSchema = z.object({ | ||
| id: z.string().uuid(), | ||
| fileName: z.string(), | ||
| fileSize: z.number(), | ||
| mimeType: z.string(), | ||
| downloadUrl: z.string().url(), | ||
| uploadedBy: z.string().uuid(), | ||
| uploadedByUserType: z.nativeEnum(AssigneeType).nullable(), | ||
| uploadedDate: RFC3339DateSchema, | ||
SandipBajracharya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| export type PublicAttachmentDto = z.infer<typeof PublicAttachmentDtoSchema> | ||
|
|
||
| export const PublicCommentDtoSchema = z.object({ | ||
| id: z.string().uuid(), | ||
| object: z.literal('taskComment'), | ||
| taskId: z.string().uuid(), | ||
| parentCommentId: z.string().uuid().nullable(), | ||
| content: z.string(), | ||
| createdBy: z.string().uuid(), | ||
| createdByUserType: z.nativeEnum(AssigneeType).nullable(), | ||
| createdDate: RFC3339DateSchema, | ||
| updatedDate: RFC3339DateSchema, | ||
| attachments: z.array(PublicAttachmentDtoSchema).nullable(), | ||
| }) | ||
| export type PublicCommentDto = z.infer<typeof PublicCommentDtoSchema> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { PublicAttachmentDto, PublicCommentDto, PublicCommentDtoSchema } from '@/app/api/comment/public/comment-public.dto' | ||
| import { RFC3339DateSchema } from '@/types/common' | ||
| import { CommentWithAttachments } from '@/types/dto/comment.dto' | ||
| import { toRFC3339 } from '@/utils/dateHelper' | ||
| import { getSignedUrl } from '@/utils/signUrl' | ||
| import { Attachment, CommentInitiator } from '@prisma/client' | ||
| import { z } from 'zod' | ||
|
|
||
| export class PublicCommentSerializer { | ||
| static async serializeUnsafe(comment: CommentWithAttachments): Promise<PublicCommentDto> { | ||
| return { | ||
| id: comment.id, | ||
| object: 'taskComment', | ||
| parentCommentId: comment.parentId, | ||
| taskId: comment.taskId, | ||
| content: comment.content, | ||
| createdBy: comment.initiatorId, | ||
| createdByUserType: comment.initiatorType, | ||
| createdDate: RFC3339DateSchema.parse(toRFC3339(comment.createdAt)), | ||
| updatedDate: RFC3339DateSchema.parse(toRFC3339(comment.updatedAt)), | ||
| attachments: await PublicCommentSerializer.serializeAttachments({ | ||
| attachments: comment.attachments, | ||
| uploadedByUserType: comment.initiatorType, | ||
| uploadedBy: comment.initiatorId, | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @param attachments array of Attachment | ||
| * @param uploadedBy id of the one who commented | ||
| * @param uploadedByUserType usertype of the one who commented | ||
| * @returns Array of PublicAttachmentDto | ||
| */ | ||
| static async serializeAttachments({ | ||
| attachments, | ||
| uploadedByUserType, | ||
| uploadedBy, | ||
| }: { | ||
| attachments: Attachment[] | ||
| uploadedByUserType: CommentInitiator | null | ||
| uploadedBy: string | ||
| }): Promise<PublicAttachmentDto[]> { | ||
| const promises = attachments.map(async (attachment) => ({ | ||
| id: attachment.id, | ||
| fileName: attachment.fileName, | ||
| fileSize: attachment.fileSize, | ||
| mimeType: attachment.fileType, | ||
| downloadUrl: z | ||
| .string({ | ||
| message: `Invalid downloadUrl for attachment with id ${attachment.id}`, | ||
| }) | ||
| .parse(await getSignedUrl(attachment.filePath)), | ||
SandipBajracharya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uploadedBy, | ||
| uploadedByUserType, | ||
| uploadedDate: RFC3339DateSchema.parse(toRFC3339(attachment.createdAt)), | ||
| })) | ||
| return await Promise.all(promises) | ||
|
||
| } | ||
|
|
||
| static async serialize(comment: CommentWithAttachments): Promise<PublicCommentDto> { | ||
| return PublicCommentDtoSchema.parse(await PublicCommentSerializer.serializeUnsafe(comment)) | ||
| } | ||
|
|
||
| static async serializeMany(comments: CommentWithAttachments[]): Promise<PublicCommentDto[]> { | ||
| const serializedComments = await Promise.all(comments.map(async (comment) => PublicCommentSerializer.serialize(comment))) | ||
| return z.array(PublicCommentDtoSchema).parse(serializedComments) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { getAllCommentsPublicForTask } from '@/app/api/comment/public/comment-public.controller' | ||
| import { withErrorHandler } from '@/app/api/core/utils/withErrorHandler' | ||
|
|
||
| export const GET = withErrorHandler(getAllCommentsPublicForTask) |
Uh oh!
There was an error while loading. Please reload this page.