generated from pagevamp/copilot-custom-app-starter-kit
-
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
Open
SandipBajracharya
wants to merge
6
commits into
feature/api-improvements
Choose a base branch
from
OUT-2917
base: feature/api-improvements
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3826e62
feat(OUT-2917): public api to list comments of a task
SandipBajracharya f348f39
refactor(OUT-2917): expose comments list route as sub-resource on tasks
SandipBajracharya fb80e8e
fix(OUT-2917): await path params
SandipBajracharya ef83070
refactor(OUT-2917): implemented proper typing, validation
SandipBajracharya ede8aa7
perf(OUT-2917): index comment table and get multiple signed urls from…
SandipBajracharya a0774f8
fix(OUT-2917): sequentially map the attachments
SandipBajracharya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...grations/20260115090155_add_created_at_task_id_worspace_id_index_on_comment/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- CreateIndex | ||
| CREATE INDEX "IX_Comments_taskId_workspaceId_createdAt" ON "Comments"("taskId", "workspaceId", "createdAt" DESC); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| 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' | ||
| import { getPaginationLimit } from '@/utils/pagination' | ||
|
|
||
| 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 || undefined, | ||
| initiatorId: createdBy || undefined, | ||
| } | ||
|
|
||
| const commentService = new CommentService(user) | ||
| const comments = await commentService.getAllComments({ | ||
| limit: getPaginationLimit(limit), | ||
| 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, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| 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 { createSignedUrls } 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 attachmentPaths = attachments.map((attachment) => attachment.filePath) | ||
| const signedUrls = await PublicCommentSerializer.getFormattedSignedUrls(attachmentPaths) | ||
|
|
||
| return attachments.map((attachment) => { | ||
| const url = signedUrls.find((item) => item.path === attachment.filePath)?.url | ||
| return { | ||
| id: attachment.id, | ||
| fileName: attachment.fileName, | ||
| fileSize: attachment.fileSize, | ||
| mimeType: attachment.fileType, | ||
| downloadUrl: z | ||
| .string() | ||
| .url({ message: `Invalid downloadUrl for attachment with id ${attachment.id}` }) | ||
| .parse(url), | ||
| uploadedBy, | ||
| uploadedByUserType, | ||
| uploadedDate: RFC3339DateSchema.parse(toRFC3339(attachment.createdAt)), | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
|
|
||
| static async getFormattedSignedUrls(attachmentPaths: string[]) { | ||
| if (!attachmentPaths.length) return [] | ||
| const signedUrls = await createSignedUrls(attachmentPaths) | ||
| return signedUrls.map((item) => ({ path: item.path, url: item.signedUrl })) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { defaultLimit } from '@/constants/public-api' | ||
| import z from 'zod' | ||
|
|
||
| type PrismaPaginationArgs = { | ||
| take?: number | ||
| skip?: number | ||
| cursor?: { id: string } | ||
| } | ||
|
|
||
| export function getBasicPaginationAttributes(limit?: number, lastIdCursor?: string): PrismaPaginationArgs { | ||
| return { | ||
| take: limit, | ||
| cursor: lastIdCursor ? { id: lastIdCursor } : undefined, | ||
| skip: lastIdCursor ? 1 : undefined, | ||
| } | ||
| } | ||
|
|
||
| export function getPaginationLimit(limit?: number | string | null) { | ||
| const safeLimit = z.coerce.number().safeParse(limit) | ||
| return !safeLimit.success || !safeLimit.data ? defaultLimit : safeLimit.data | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the comments table properly indexed here, can you please check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
created composite index with
taskId, workspaceId, createdAt DESC.