| 257 | message: string; |
| 258 | }; |
| 259 | export const constructCommentPrismaQuery = ( |
| 260 | searchParams: QueryParams, |
| 261 | paginationInfo: PaginationInfo, |
| 262 | contentId: number, |
| 263 | userId: string, |
| 264 | ): Prisma.CommentFindManyArgs => { |
| 265 | const { pageSize, skip } = paginationInfo; |
| 266 | const { tabtype, type } = searchParams; |
| 267 | |
| 268 | let orderBy: Prisma.Enumerable<Prisma.CommentOrderByWithRelationInput> = {}; |
| 269 | switch (tabtype) { |
| 270 | case TabType.mu: |
| 271 | orderBy = { upvotes: 'desc' }; |
| 272 | break; |
| 273 | case TabType.md: |
| 274 | orderBy = { downvotes: 'desc' }; |
| 275 | break; |
| 276 | case TabType.mr: |
| 277 | orderBy = { createdAt: 'desc' }; |
| 278 | break; |
| 279 | default: |
| 280 | orderBy = { upvotes: 'desc' }; |
| 281 | } |
| 282 | |
| 283 | const where: Prisma.CommentWhereInput = {}; |
| 284 | if (contentId) { |
| 285 | where.contentId = contentId; |
| 286 | } |
| 287 | if (searchParams.parentId) { |
| 288 | where.parentId = parseInt(searchParams.parentId.toString(), 10); |
| 289 | } else { |
| 290 | where.parent = null; |
| 291 | } |
| 292 | if (type === CommentType.INTRO) { |
| 293 | where.commentType = type; |
| 294 | } |
| 295 | const query: Prisma.CommentFindManyArgs = { |
| 296 | where, |
| 297 | orderBy, |
| 298 | skip, |
| 299 | take: pageSize, |
| 300 | include: { |
| 301 | user: true, |
| 302 | votes: { |
| 303 | where: { |
| 304 | userId, |
| 305 | }, |
| 306 | select: { |
| 307 | voteType: true, // Only fetch the voteType to determine if it's an upvote or downvote |
| 308 | }, |
| 309 | }, |
| 310 | }, |
| 311 | }; |
| 312 | |
| 313 | return query; |
| 314 | }; |
| 315 | |
| 316 | export const generateHandle = (title: string): string => { |