Merge pull request #3367 from specklesystems/fabians/core-ioc-99
chore(server): core IoC #99 - batchDeleteCommitsFactory
This commit is contained in:
@@ -9,8 +9,10 @@ import {
|
||||
LegacyStreamCommit
|
||||
} from '@/modules/core/domain/commits/types'
|
||||
import {
|
||||
CommitsDeleteInput,
|
||||
CommitsMoveInput,
|
||||
CommitUpdateInput,
|
||||
DeleteVersionsInput,
|
||||
ModelVersionsFilter,
|
||||
MoveVersionsInput,
|
||||
StreamCommitsArgs,
|
||||
@@ -244,6 +246,11 @@ export type ValidateAndBatchMoveCommits = (
|
||||
userId: string
|
||||
) => Promise<Branch>
|
||||
|
||||
export type ValidateAndBatchDeleteCommits = (
|
||||
params: CommitsDeleteInput | DeleteVersionsInput,
|
||||
userId: string
|
||||
) => Promise<void>
|
||||
|
||||
export type GetObjectCommitsWithStreamIds = (
|
||||
objectIds: string[],
|
||||
options?: {
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
getRateLimitResult
|
||||
} from '@/modules/core/services/ratelimiter'
|
||||
import {
|
||||
batchDeleteCommits,
|
||||
batchDeleteCommitsFactory,
|
||||
batchMoveCommitsFactory
|
||||
} from '@/modules/core/services/commit/batchCommitActions'
|
||||
import { StreamInvalidAccessError } from '@/modules/core/errors/stream'
|
||||
@@ -44,7 +44,8 @@ import {
|
||||
legacyGetPaginatedUserCommitsPage,
|
||||
legacyGetPaginatedUserCommitsTotalCount,
|
||||
legacyGetPaginatedStreamCommitsPageFactory,
|
||||
getStreamCommitCountFactory
|
||||
getStreamCommitCountFactory,
|
||||
deleteCommitsFactory
|
||||
} from '@/modules/core/repositories/commits'
|
||||
import { db } from '@/db/knex'
|
||||
import {
|
||||
@@ -150,6 +151,15 @@ const getPaginatedStreamCommits = legacyGetPaginatedStreamCommits({
|
||||
legacyGetPaginatedStreamCommitsPage: getCommitsByStreamId,
|
||||
getStreamCommitCount: getStreamCommitCountFactory({ db })
|
||||
})
|
||||
const batchDeleteCommits = batchDeleteCommitsFactory({
|
||||
getCommits: getCommitsFactory({ db }),
|
||||
getStreams,
|
||||
deleteCommits: deleteCommitsFactory({ db }),
|
||||
addCommitDeletedActivity: addCommitDeletedActivityFactory({
|
||||
saveActivity: saveActivityFactory({ db }),
|
||||
publish
|
||||
})
|
||||
})
|
||||
|
||||
const getAuthorId = (commit: CommitGraphQLReturn) => {
|
||||
if ('author' in commit) return commit.author
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
} from '@/modules/shared/utils/subscriptions'
|
||||
import { getServerOrigin } from '@/modules/shared/helpers/envHelper'
|
||||
import {
|
||||
batchDeleteCommits,
|
||||
batchDeleteCommitsFactory,
|
||||
batchMoveCommitsFactory
|
||||
} from '@/modules/core/services/commit/batchCommitActions'
|
||||
import { CommitNotFoundError, CommitUpdateError } from '@/modules/core/errors/commit'
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
import { RateLimitError } from '@/modules/core/errors/ratelimit'
|
||||
import {
|
||||
createCommitFactory,
|
||||
deleteCommitsFactory,
|
||||
getCommitBranchFactory,
|
||||
getCommitFactory,
|
||||
getCommitsFactory,
|
||||
@@ -49,6 +50,7 @@ import {
|
||||
import { VersionsEmitter } from '@/modules/core/events/versionsEmitter'
|
||||
import {
|
||||
addCommitCreatedActivityFactory,
|
||||
addCommitDeletedActivityFactory,
|
||||
addCommitMovedActivityFactory,
|
||||
addCommitUpdatedActivityFactory
|
||||
} from '@/modules/activitystream/services/commitActivity'
|
||||
@@ -102,6 +104,15 @@ const batchMoveCommits = batchMoveCommitsFactory({
|
||||
publish
|
||||
})
|
||||
})
|
||||
const batchDeleteCommits = batchDeleteCommitsFactory({
|
||||
getCommits: getCommitsFactory({ db }),
|
||||
getStreams,
|
||||
deleteCommits: deleteCommitsFactory({ db }),
|
||||
addCommitDeletedActivity: addCommitDeletedActivityFactory({
|
||||
saveActivity: saveActivityFactory({ db }),
|
||||
publish
|
||||
})
|
||||
})
|
||||
|
||||
export = {
|
||||
Project: {
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { db } from '@/db/knex'
|
||||
import { AddCommitMovedActivity } from '@/modules/activitystream/domain/operations'
|
||||
import { saveActivityFactory } from '@/modules/activitystream/repositories'
|
||||
import { addCommitDeletedActivityFactory } from '@/modules/activitystream/services/commitActivity'
|
||||
import {
|
||||
AddCommitDeletedActivity,
|
||||
AddCommitMovedActivity
|
||||
} from '@/modules/activitystream/domain/operations'
|
||||
import {
|
||||
GetStreamBranchByName,
|
||||
StoreBranch
|
||||
} from '@/modules/core/domain/branches/operations'
|
||||
import {
|
||||
DeleteCommits,
|
||||
GetCommits,
|
||||
MoveCommitsToBranch,
|
||||
ValidateAndBatchDeleteCommits,
|
||||
ValidateAndBatchMoveCommits
|
||||
} from '@/modules/core/domain/commits/operations'
|
||||
import { GetStreams } from '@/modules/core/domain/streams/operations'
|
||||
@@ -23,13 +25,7 @@ import {
|
||||
MoveVersionsInput
|
||||
} from '@/modules/core/graph/generated/graphql'
|
||||
import { Roles } from '@/modules/core/helpers/mainConstants'
|
||||
import {
|
||||
deleteCommitsFactory,
|
||||
getCommitsFactory
|
||||
} from '@/modules/core/repositories/commits'
|
||||
import { getStreamsFactory } from '@/modules/core/repositories/streams'
|
||||
import { ensureError } from '@/modules/shared/helpers/errorHelper'
|
||||
import { publish } from '@/modules/shared/utils/subscriptions'
|
||||
import { difference, groupBy, has, keyBy } from 'lodash'
|
||||
|
||||
type OldBatchInput = CommitsMoveInput | CommitsDeleteInput
|
||||
@@ -142,16 +138,12 @@ const validateCommitsMoveFactory =
|
||||
/**
|
||||
* Validate batch delete params
|
||||
*/
|
||||
async function validateCommitsDelete(
|
||||
params: CommitsDeleteInput | DeleteVersionsInput,
|
||||
userId: string
|
||||
) {
|
||||
const validateBatchBaseRules = validateBatchBaseRulesFactory({
|
||||
getCommits: getCommitsFactory({ db }),
|
||||
getStreams: getStreamsFactory({ db })
|
||||
})
|
||||
return await validateBatchBaseRules(params, userId)
|
||||
}
|
||||
const validateCommitsDeleteFactory =
|
||||
(deps: ValidateBatchBaseRulesDeps) =>
|
||||
async (params: CommitsDeleteInput | DeleteVersionsInput, userId: string) => {
|
||||
const validateBatchBaseRules = validateBatchBaseRulesFactory(deps)
|
||||
return await validateBatchBaseRules(params, userId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a batch of commits belonging to the same stream to another branch
|
||||
@@ -206,32 +198,36 @@ export const batchMoveCommitsFactory =
|
||||
/**
|
||||
* Delete a batch of commits
|
||||
*/
|
||||
export async function batchDeleteCommits(
|
||||
params: CommitsDeleteInput | DeleteVersionsInput,
|
||||
userId: string
|
||||
) {
|
||||
const commitIds = isOldBatchInput(params) ? params.commitIds : params.versionIds
|
||||
export const batchDeleteCommitsFactory =
|
||||
(
|
||||
deps: ValidateBatchBaseRulesDeps & {
|
||||
deleteCommits: DeleteCommits
|
||||
addCommitDeletedActivity: AddCommitDeletedActivity
|
||||
}
|
||||
): ValidateAndBatchDeleteCommits =>
|
||||
async (params: CommitsDeleteInput | DeleteVersionsInput, userId: string) => {
|
||||
const commitIds = isOldBatchInput(params) ? params.commitIds : params.versionIds
|
||||
|
||||
const { commitsWithStreams } = await validateCommitsDelete(params, userId)
|
||||
|
||||
try {
|
||||
await deleteCommitsFactory({ db })(commitIds)
|
||||
await Promise.all(
|
||||
commitsWithStreams.map(({ commit, stream }) =>
|
||||
addCommitDeletedActivityFactory({
|
||||
saveActivity: saveActivityFactory({ db }),
|
||||
publish
|
||||
})({
|
||||
commitId: commit.id,
|
||||
streamId: stream.id,
|
||||
userId,
|
||||
commit,
|
||||
branchId: commit.branchId
|
||||
})
|
||||
)
|
||||
const { commitsWithStreams } = await validateCommitsDeleteFactory(deps)(
|
||||
params,
|
||||
userId
|
||||
)
|
||||
} catch (e) {
|
||||
const err = ensureError(e)
|
||||
throw new CommitBatchUpdateError('Batch commit delete failed', { cause: err })
|
||||
|
||||
try {
|
||||
await deps.deleteCommits(commitIds)
|
||||
await Promise.all(
|
||||
commitsWithStreams.map(({ commit, stream }) =>
|
||||
deps.addCommitDeletedActivity({
|
||||
commitId: commit.id,
|
||||
streamId: stream.id,
|
||||
userId,
|
||||
commit,
|
||||
branchId: commit.branchId
|
||||
})
|
||||
)
|
||||
)
|
||||
} catch (e) {
|
||||
const err = ensureError(e)
|
||||
throw new CommitBatchUpdateError('Batch commit delete failed', { cause: err })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user