Merge pull request #3324 from specklesystems/fabians/core-ioc-86

chore(server): core IoC #86 - remaining commits.js service
This commit is contained in:
Alessandro Magionami
2024-10-21 09:43:05 +02:00
committed by GitHub
6 changed files with 76 additions and 44 deletions
@@ -219,6 +219,21 @@ export type GetPaginatedBranchCommits = (
cursor: string | null
}>
export type GetBranchCommitsTotalCountByName = (params: {
streamId: string
branchName: string
}) => Promise<number>
export type GetPaginatedBranchCommitsItemsByName = (params: {
streamId: string
branchName: string
limit: number
cursor?: Nullable<string>
}) => Promise<{
commits: Commit[]
cursor: string | null
}>
export type MoveCommitsToBranch = (
commitIds: string[],
branchId: string
@@ -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 })
}
@@ -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 })
}
}
@@ -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 = {
@@ -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 = {
+7 -2
View File
@@ -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) => {