diff --git a/packages/server/modules/core/domain/commits/operations.ts b/packages/server/modules/core/domain/commits/operations.ts index f47e8dfad..05251081a 100644 --- a/packages/server/modules/core/domain/commits/operations.ts +++ b/packages/server/modules/core/domain/commits/operations.ts @@ -219,6 +219,21 @@ export type GetPaginatedBranchCommits = ( cursor: string | null }> +export type GetBranchCommitsTotalCountByName = (params: { + streamId: string + branchName: string +}) => Promise + +export type GetPaginatedBranchCommitsItemsByName = (params: { + streamId: string + branchName: string + limit: number + cursor?: Nullable +}) => Promise<{ + commits: Commit[] + cursor: string | null +}> + export type MoveCommitsToBranch = ( commitIds: string[], branchId: string diff --git a/packages/server/modules/core/services/commit/retrieval.ts b/packages/server/modules/core/services/commit/retrieval.ts index 3da9623e2..c5b23e1a0 100644 --- a/packages/server/modules/core/services/commit/retrieval.ts +++ b/packages/server/modules/core/services/commit/retrieval.ts @@ -6,14 +6,17 @@ import { import { BadRequestError } from '@/modules/shared/errors' import { GetBranchCommitsTotalCount, + GetBranchCommitsTotalCountByName, GetPaginatedBranchCommits, GetPaginatedBranchCommitsItems, + GetPaginatedBranchCommitsItemsByName, GetSpecificBranchCommits, GetStreamCommitCount, LegacyGetPaginatedStreamCommits, LegacyGetPaginatedStreamCommitsPage, PaginatedBranchCommitsParams } from '@/modules/core/domain/commits/operations' +import { GetStreamBranchByName } from '@/modules/core/domain/branches/operations' export const legacyGetPaginatedStreamCommits = (deps: { @@ -97,3 +100,30 @@ export const getPaginatedBranchCommitsFactory = items: newItems } } + +export const getBranchCommitsTotalCountByNameFactory = + (deps: { + getStreamBranchByName: GetStreamBranchByName + getBranchCommitsTotalCount: GetBranchCommitsTotalCount + }): GetBranchCommitsTotalCountByName => + async ({ streamId, branchName }) => { + branchName = branchName.toLowerCase() + const myBranch = await deps.getStreamBranchByName(streamId, branchName) + + if (!myBranch) throw new Error(`Failed to find branch with name ${branchName}.`) + return deps.getBranchCommitsTotalCount({ branchId: myBranch.id }) + } + +export const getPaginatedBranchCommitsItemsByNameFactory = + (deps: { + getStreamBranchByName: GetStreamBranchByName + getPaginatedBranchCommitsItems: GetPaginatedBranchCommitsItems + }): GetPaginatedBranchCommitsItemsByName => + async ({ streamId, branchName, limit, cursor }) => { + branchName = branchName.toLowerCase() + const myBranch = await deps.getStreamBranchByName(streamId, branchName) + + if (!myBranch) throw new Error(`Failed to find branch with name ${branchName}.`) + + return deps.getPaginatedBranchCommitsItems({ branchId: myBranch.id, limit, cursor }) + } diff --git a/packages/server/modules/core/services/commits.js b/packages/server/modules/core/services/commits.js deleted file mode 100644 index 66c53fe4d..000000000 --- a/packages/server/modules/core/services/commits.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' -const knex = require('@/db/knex') -const { getStreamBranchByNameFactory } = require('@/modules/core/repositories/branches') -const { - getBranchCommitsTotalCountFactory, - getPaginatedBranchCommitsItemsFactory -} = require('@/modules/core/repositories/commits') - -module.exports = { - async getCommitsTotalCountByBranchName({ streamId, branchName }) { - branchName = branchName.toLowerCase() - const getStreamBranchByName = getStreamBranchByNameFactory({ db: knex }) - const myBranch = await getStreamBranchByName(streamId, branchName) - - if (!myBranch) throw new Error(`Failed to find branch with name ${branchName}.`) - - const getBranchCommitsTotalCount = getBranchCommitsTotalCountFactory({ db: knex }) - - return getBranchCommitsTotalCount({ branchId: myBranch.id }) - }, - - async getCommitsByBranchName({ streamId, branchName, limit, cursor }) { - branchName = branchName.toLowerCase() - const getStreamBranchByName = getStreamBranchByNameFactory({ db: knex }) - const myBranch = await getStreamBranchByName(streamId, branchName) - - if (!myBranch) throw new Error(`Failed to find branch with name ${branchName}.`) - - const getPaginatedBranchCommits = getPaginatedBranchCommitsItemsFactory({ - db: knex - }) - return getPaginatedBranchCommits({ branchId: myBranch.id, limit, cursor }) - } -} diff --git a/packages/server/modules/core/tests/commits.spec.js b/packages/server/modules/core/tests/commits.spec.js index 66075b701..5e5d55b67 100644 --- a/packages/server/modules/core/tests/commits.spec.js +++ b/packages/server/modules/core/tests/commits.spec.js @@ -5,10 +5,6 @@ const { beforeEachContext } = require('@/test/hooks') const { createObject } = require('../services/objects') -const { - getCommitsTotalCountByBranchName, - getCommitsByBranchName -} = require('../services/commits') const { createBranchAndNotifyFactory } = require('@/modules/core/services/branch/management') @@ -34,7 +30,9 @@ const { updateCommitFactory, getStreamCommitCountFactory, legacyGetPaginatedUserCommitsPage, - legacyGetPaginatedStreamCommitsPageFactory + legacyGetPaginatedStreamCommitsPageFactory, + getBranchCommitsTotalCountFactory, + getPaginatedBranchCommitsItemsFactory } = require('@/modules/core/repositories/commits') const { deleteCommitAndNotifyFactory, @@ -112,6 +110,10 @@ const { } = require('@/modules/serverinvites/services/processing') const { UsersEmitter } = require('@/modules/core/events/usersEmitter') const { getServerInfoFactory } = require('@/modules/core/repositories/server') +const { + getBranchCommitsTotalCountByNameFactory, + getPaginatedBranchCommitsItemsByNameFactory +} = require('@/modules/core/services/commit/retrieval') const getServerInfo = getServerInfoFactory({ db }) const getUser = getUserFactory({ db }) @@ -238,6 +240,14 @@ const createUser = createUserFactory({ }) const getCommitsByUserId = legacyGetPaginatedUserCommitsPage({ db }) const getCommitsByStreamId = legacyGetPaginatedStreamCommitsPageFactory({ db }) +const getCommitsTotalCountByBranchName = getBranchCommitsTotalCountByNameFactory({ + getStreamBranchByName: getStreamBranchByNameFactory({ db }), + getBranchCommitsTotalCount: getBranchCommitsTotalCountFactory({ db }) +}) +const getCommitsByBranchName = getPaginatedBranchCommitsItemsByNameFactory({ + getStreamBranchByName: getStreamBranchByNameFactory({ db }), + getPaginatedBranchCommitsItems: getPaginatedBranchCommitsItemsFactory({ db }) +}) describe('Commits @core-commits', () => { const user = { diff --git a/packages/server/modules/core/tests/users.spec.js b/packages/server/modules/core/tests/users.spec.js index 81df1589f..b92ae670f 100644 --- a/packages/server/modules/core/tests/users.spec.js +++ b/packages/server/modules/core/tests/users.spec.js @@ -9,8 +9,6 @@ const { const { getBranchesByStreamId } = require('../services/branches') -const { getCommitsByBranchName } = require('../services/commits') - const { createObject } = require('../services/objects') const { beforeEachContext } = require('@/test/hooks') const { Scopes, Roles } = require('@speckle/shared') @@ -27,7 +25,8 @@ const { createCommitFactory, insertStreamCommitsFactory, insertBranchCommitsFactory, - legacyGetPaginatedStreamCommitsPageFactory + legacyGetPaginatedStreamCommitsPageFactory, + getPaginatedBranchCommitsItemsFactory } = require('@/modules/core/repositories/commits') const { createCommitByBranchIdFactory, @@ -140,6 +139,9 @@ const { } = require('@/modules/core/repositories/tokens') const { getTokenAppInfoFactory } = require('@/modules/auth/repositories/apps') const { getServerInfoFactory } = require('@/modules/core/repositories/server') +const { + getPaginatedBranchCommitsItemsByNameFactory +} = require('@/modules/core/services/commit/retrieval') const getServerInfo = getServerInfoFactory({ db }) const getUser = legacyGetUserFactory({ db }) @@ -287,6 +289,10 @@ const validateToken = validateTokenFactory({ updateApiToken: updateApiTokenFactory({ db }) }) const getCommitsByStreamId = legacyGetPaginatedStreamCommitsPageFactory({ db }) +const getCommitsByBranchName = getPaginatedBranchCommitsItemsByNameFactory({ + getStreamBranchByName: getStreamBranchByNameFactory({ db }), + getPaginatedBranchCommitsItems: getPaginatedBranchCommitsItemsFactory({ db }) +}) describe('Actors & Tokens @user-services', () => { const myTestActor = { diff --git a/packages/server/modules/previews/index.ts b/packages/server/modules/previews/index.ts index 194461280..503abc8af 100644 --- a/packages/server/modules/previews/index.ts +++ b/packages/server/modules/previews/index.ts @@ -1,7 +1,5 @@ /* istanbul ignore file */ -'use strict' import { validateScopes, authorizeResolver } from '@/modules/shared' -import { getCommitsByBranchName } from '@/modules/core/services/commits' import { makeOgImage } from '@/modules/previews/ogImage' import { moduleLogger } from '@/logging/logging' @@ -24,10 +22,13 @@ import { publish } from '@/modules/shared/utils/subscriptions' import { getCommitFactory, getObjectCommitsWithStreamIdsFactory, + getPaginatedBranchCommitsItemsFactory, legacyGetPaginatedStreamCommitsPageFactory } from '@/modules/core/repositories/commits' import { SpeckleModule } from '@/modules/shared/helpers/typeHelper' import { getStreamFactory } from '@/modules/core/repositories/streams' +import { getPaginatedBranchCommitsItemsByNameFactory } from '@/modules/core/services/commit/retrieval' +import { getStreamBranchByNameFactory } from '@/modules/core/repositories/branches' const httpErrorImage = (httpErrorCode: number) => require.resolve(`#/assets/previews/images/preview_${httpErrorCode}.png`) @@ -59,6 +60,10 @@ export const init: SpeckleModule['init'] = (app, isInitial) => { authorizeResolver, getStream }) + const getCommitsByBranchName = getPaginatedBranchCommitsItemsByNameFactory({ + getStreamBranchByName: getStreamBranchByNameFactory({ db }), + getPaginatedBranchCommitsItems: getPaginatedBranchCommitsItemsFactory({ db }) + }) app.options('/preview/:streamId/:angle?', cors()) app.get('/preview/:streamId/:angle?', cors(), async (req, res) => {