From cc426f553731deba5a4f60f49518c8eed8dd64f4 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Tue, 24 Sep 2024 14:37:11 +0300 Subject: [PATCH] chore(server): comments IoC 15 - getPaginatedBranchCommentsFactory --- .../modules/comments/domain/operations.ts | 29 ++++ .../comments/graph/resolvers/comments.ts | 10 +- .../modules/comments/repositories/comments.ts | 125 +++++++++--------- .../modules/comments/services/retrieval.ts | 32 +++-- 4 files changed, 116 insertions(+), 80 deletions(-) diff --git a/packages/server/modules/comments/domain/operations.ts b/packages/server/modules/comments/domain/operations.ts index 4d04ec840..d5aed3bbd 100644 --- a/packages/server/modules/comments/domain/operations.ts +++ b/packages/server/modules/comments/domain/operations.ts @@ -77,6 +77,17 @@ export type GetPaginatedCommitCommentsTotalCount = ( params: Omit ) => Promise +export type GetPaginatedBranchCommentsPage = ( + params: PaginatedBranchCommentsParams +) => Promise<{ + items: CommentRecord[] + cursor: string | null +}> + +export type GetPaginatedBranchCommentsTotalCount = ( + params: Omit +) => Promise + export type CheckStreamResourcesAccess = (params: { streamId: string resources: ResourceIdentifier[] @@ -153,3 +164,21 @@ export type GetPaginatedCommitComments = ( items: CommentRecord[] cursor: string | null }> + +export type PaginatedBranchCommentsParams = { + branchId: string + limit: number + cursor?: MaybeNullOrUndefined + filter?: MaybeNullOrUndefined<{ + threadsOnly: boolean + includeArchived: boolean + }> +} + +export type GetPaginatedBranchCommentsFactory = ( + params: PaginatedBranchCommentsParams +) => Promise<{ + totalCount: number + items: CommentRecord[] + cursor: string | null +}> diff --git a/packages/server/modules/comments/graph/resolvers/comments.ts b/packages/server/modules/comments/graph/resolvers/comments.ts index ac8274a90..9e1484fa4 100644 --- a/packages/server/modules/comments/graph/resolvers/comments.ts +++ b/packages/server/modules/comments/graph/resolvers/comments.ts @@ -15,6 +15,8 @@ import { getCommentFactory, getCommentsLegacyFactory, getCommentsResourcesFactory, + getPaginatedBranchCommentsPageFactory, + getPaginatedBranchCommentsTotalCountFactory, getPaginatedCommitCommentsPageFactory, getPaginatedCommitCommentsTotalCountFactory, getResourceCommentCountFactory, @@ -34,7 +36,7 @@ import { SmartTextEditorValueSchema } from '@/modules/core/services/richTextEditorService' import { - getPaginatedBranchComments, + getPaginatedBranchCommentsFactory, getPaginatedCommitCommentsFactory, getPaginatedProjectComments } from '@/modules/comments/services/retrieval' @@ -213,6 +215,12 @@ const getPaginatedCommitComments = getPaginatedCommitCommentsFactory({ db }) }) +const getPaginatedBranchComments = getPaginatedBranchCommentsFactory({ + getPaginatedBranchCommentsPage: getPaginatedBranchCommentsPageFactory({ db }), + getPaginatedBranchCommentsTotalCount: getPaginatedBranchCommentsTotalCountFactory({ + db + }) +}) const getStreamComment = async ( { streamId, commentId }: { streamId: string; commentId: string }, diff --git a/packages/server/modules/comments/repositories/comments.ts b/packages/server/modules/comments/repositories/comments.ts index 497422bb2..b0b96f0a0 100644 --- a/packages/server/modules/comments/repositories/comments.ts +++ b/packages/server/modules/comments/repositories/comments.ts @@ -37,6 +37,7 @@ import { DeleteComment, GetComment, GetCommentsResources, + GetPaginatedBranchCommentsPage, GetPaginatedCommitCommentsPage, GetPaginatedCommitCommentsTotalCount, InsertCommentLinks, @@ -44,10 +45,12 @@ import { InsertComments, MarkCommentUpdated, MarkCommentViewed, + PaginatedBranchCommentsParams, PaginatedCommitCommentsParams, UpdateComment } from '@/modules/comments/domain/operations' import { + BranchRecord, CommitRecord, ObjectRecord, StreamCommitRecord @@ -60,7 +63,8 @@ const tables = { comments: (db: Knex) => db(Comments.name), commentLinks: (db: Knex) => db(CommentLinks.name), commentViews: (db: Knex) => db(CommentViews.name), - commits: (db: Knex) => db(Commits.name) + commits: (db: Knex) => db(Commits.name), + branches: (db: Knex) => db(Branches.name) } export const generateCommentId = () => crs({ length: 10 }) @@ -408,80 +412,71 @@ export const getPaginatedCommitCommentsTotalCountFactory = return parseInt(row.count || '0') } -export type PaginatedBranchCommentsParams = { - branchId: string - limit: number - cursor?: MaybeNullOrUndefined - filter?: MaybeNullOrUndefined<{ - threadsOnly: boolean - includeArchived: boolean - }> -} +const getPaginatedBranchCommentsBaseQueryFactory = + (deps: { db: Knex }) => + (params: Omit) => { + const { branchId, filter } = params -function getPaginatedBranchCommentsBaseQuery( - params: Omit -) { - const { branchId, filter } = params + const q = tables + .branches(deps.db) + .distinct() + .select(Comments.cols) + .innerJoin(BranchCommits.name, BranchCommits.col.branchId, Branches.col.id) + .innerJoin(CommentLinks.name, function () { + this.on(CommentLinks.col.resourceId, BranchCommits.col.commitId).andOnVal( + CommentLinks.col.resourceType, + 'commit' as CommentLinkResourceType + ) + }) + .innerJoin(Comments.name, Comments.col.id, CommentLinks.col.commentId) + .where(Branches.col.id, branchId) - const q = Branches.knex() - .distinct() - .select(Comments.cols) - .innerJoin(BranchCommits.name, BranchCommits.col.branchId, Branches.col.id) - .innerJoin(CommentLinks.name, function () { - this.on(CommentLinks.col.resourceId, BranchCommits.col.commitId).andOnVal( - CommentLinks.col.resourceType, - 'commit' as CommentLinkResourceType - ) - }) - .innerJoin(Comments.name, Comments.col.id, CommentLinks.col.commentId) - .where(Branches.col.id, branchId) + if (!filter?.includeArchived) { + q.andWhere(Comments.col.archived, false) + } - if (!filter?.includeArchived) { - q.andWhere(Comments.col.archived, false) + if (filter?.threadsOnly) { + q.whereNull(Comments.col.parentComment) + } + + return q } - if (filter?.threadsOnly) { - q.whereNull(Comments.col.parentComment) +export const getPaginatedBranchCommentsPageFactory = + (deps: { db: Knex }): GetPaginatedBranchCommentsPage => + async (params: PaginatedBranchCommentsParams) => { + const { cursor } = params + + const limit = clamp(params.limit, 0, 100) + if (!limit) return { items: [], cursor: null } + + const q = getPaginatedBranchCommentsBaseQueryFactory(deps)(params) + .orderBy(Comments.col.createdAt, 'desc') + .limit(limit) + + if (cursor) { + q.andWhere(Comments.col.createdAt, '<', decodeCursor(cursor)) + } + + const items = await q + return { + items, + cursor: items.length + ? encodeCursor(items[items.length - 1].createdAt.toISOString()) + : null + } } - return q -} +export const getPaginatedBranchCommentsTotalCountFactory = + (deps: { db: Knex }) => + async (params: Omit) => { + const baseQ = getPaginatedBranchCommentsBaseQueryFactory(deps)(params) + const q = knex.count<{ count: string }[]>().from(baseQ.as('sq1')) + const [row] = await q -export async function getPaginatedBranchComments( - params: PaginatedBranchCommentsParams -) { - const { cursor } = params - - const limit = clamp(params.limit, 0, 100) - if (!limit) return { items: [], cursor: null } - - const q = getPaginatedBranchCommentsBaseQuery(params) - .orderBy(Comments.col.createdAt, 'desc') - .limit(limit) - - if (cursor) { - q.andWhere(Comments.col.createdAt, '<', decodeCursor(cursor)) + return parseInt(row.count || '0') } - const items = await q - return { - items, - cursor: items.length - ? encodeCursor(items[items.length - 1].createdAt.toISOString()) - : null - } -} - -export async function getPaginatedBranchCommentsTotalCount( - params: Omit -) { - const baseQ = getPaginatedBranchCommentsBaseQuery(params) - const q = knex.count<{ count: string }[]>().from(baseQ.as('sq1')) - const [row] = await q - - return parseInt(row.count || '0') -} - export type PaginatedProjectCommentsParams = { projectId: string limit?: MaybeNullOrUndefined diff --git a/packages/server/modules/comments/services/retrieval.ts b/packages/server/modules/comments/services/retrieval.ts index 807fbeb29..49e635216 100644 --- a/packages/server/modules/comments/services/retrieval.ts +++ b/packages/server/modules/comments/services/retrieval.ts @@ -1,8 +1,5 @@ import { Optional } from '@speckle/shared' import { - PaginatedBranchCommentsParams, - getPaginatedBranchComments as getPaginatedBranchCommentsDb, - getPaginatedBranchCommentsTotalCount, getPaginatedProjectComments as getPaginatedProjectCommentsDb, getPaginatedProjectCommentsTotalCount, PaginatedProjectCommentsParams, @@ -11,9 +8,13 @@ import { import { getBranchLatestCommits } from '@/modules/core/repositories/branches' import { isUndefined } from 'lodash' import { + GetPaginatedBranchCommentsFactory, + GetPaginatedBranchCommentsPage, + GetPaginatedBranchCommentsTotalCount, GetPaginatedCommitComments, GetPaginatedCommitCommentsPage, GetPaginatedCommitCommentsTotalCount, + PaginatedBranchCommentsParams, PaginatedCommitCommentsParams } from '@/modules/comments/domain/operations' @@ -34,19 +35,22 @@ export const getPaginatedCommitCommentsFactory = } } -export async function getPaginatedBranchComments( - params: PaginatedBranchCommentsParams -) { - const [result, totalCount] = await Promise.all([ - getPaginatedBranchCommentsDb(params), - getPaginatedBranchCommentsTotalCount(params) - ]) +export const getPaginatedBranchCommentsFactory = + (deps: { + getPaginatedBranchCommentsPage: GetPaginatedBranchCommentsPage + getPaginatedBranchCommentsTotalCount: GetPaginatedBranchCommentsTotalCount + }): GetPaginatedBranchCommentsFactory => + async (params: PaginatedBranchCommentsParams) => { + const [result, totalCount] = await Promise.all([ + deps.getPaginatedBranchCommentsPage(params), + deps.getPaginatedBranchCommentsTotalCount(params) + ]) - return { - ...result, - totalCount + return { + ...result, + totalCount + } } -} export async function getPaginatedProjectComments( params: PaginatedProjectCommentsParams