Merge pull request #3064 from specklesystems/fabians/automate-ioc-11
chore(server): automate IoC 11 - manuallyTriggerAutomationFactory
This commit is contained in:
@@ -112,6 +112,14 @@ export type UpsertAutomationRun = (
|
||||
automationRun: InsertableAutomationRun
|
||||
) => Promise<void>
|
||||
|
||||
export type GetAutomationTriggerDefinitions = <
|
||||
T extends AutomationTriggerType = AutomationTriggerType
|
||||
>(params: {
|
||||
automationId: string
|
||||
projectId?: string
|
||||
triggerType?: T
|
||||
}) => Promise<Array<AutomationTriggerDefinitionRecord<T> & { automationId: string }>>
|
||||
|
||||
export type CreateStoredAuthCode = (
|
||||
params: Omit<AuthCodePayload, 'code'>
|
||||
) => Promise<AuthCodePayload>
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
getAutomationRunsItems,
|
||||
getAutomationRunsTotalCount,
|
||||
getAutomationTokenFactory,
|
||||
getAutomationTriggerDefinitions,
|
||||
getAutomationTriggerDefinitionsFactory,
|
||||
getFullAutomationRevisionMetadataFactory,
|
||||
getFunctionRunFactory,
|
||||
getLatestAutomationRevision,
|
||||
@@ -64,7 +64,7 @@ import {
|
||||
} from '@/modules/core/repositories/branches'
|
||||
import {
|
||||
createTestAutomationRun,
|
||||
manuallyTriggerAutomation,
|
||||
manuallyTriggerAutomationFactory,
|
||||
triggerAutomationRevisionRunFactory
|
||||
} from '@/modules/automate/services/trigger'
|
||||
import {
|
||||
@@ -125,6 +125,7 @@ const getFullAutomationRevisionMetadata = getFullAutomationRevisionMetadataFacto
|
||||
})
|
||||
const getAutomationToken = getAutomationTokenFactory({ db })
|
||||
const upsertAutomationRun = upsertAutomationRunFactory({ db })
|
||||
const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({ db })
|
||||
|
||||
export = (FF_AUTOMATE_MODULE_ENABLED
|
||||
? {
|
||||
@@ -532,7 +533,7 @@ export = (FF_AUTOMATE_MODULE_ENABLED
|
||||
})
|
||||
},
|
||||
async trigger(parent, { automationId }, ctx) {
|
||||
const trigger = manuallyTriggerAutomation({
|
||||
const trigger = manuallyTriggerAutomationFactory({
|
||||
getAutomationTriggerDefinitions,
|
||||
getAutomation,
|
||||
getBranchLatestCommits,
|
||||
@@ -546,7 +547,8 @@ export = (FF_AUTOMATE_MODULE_ENABLED
|
||||
automateRunsEmitter: AutomateRunsEmitter.emit,
|
||||
getAutomationToken,
|
||||
upsertAutomationRun
|
||||
})
|
||||
}),
|
||||
validateStreamAccess
|
||||
})
|
||||
|
||||
const { automationRunId } = await trigger({
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
GetAutomationRunFullTriggers,
|
||||
GetAutomations,
|
||||
GetAutomationToken,
|
||||
GetAutomationTriggerDefinitions,
|
||||
GetFullAutomationRevisionMetadata,
|
||||
GetFullAutomationRunById,
|
||||
GetFunctionRun,
|
||||
@@ -493,47 +494,52 @@ export const updateAutomationFactory =
|
||||
return ret
|
||||
}
|
||||
|
||||
export async function getAutomationTriggerDefinitions<
|
||||
T extends AutomationTriggerType = AutomationTriggerType
|
||||
>(params: { automationId: string; projectId?: string; triggerType?: T }) {
|
||||
const { automationId, projectId, triggerType } = params
|
||||
export const getAutomationTriggerDefinitionsFactory =
|
||||
(deps: { db: Knex }): GetAutomationTriggerDefinitions =>
|
||||
async <T extends AutomationTriggerType = AutomationTriggerType>(params: {
|
||||
automationId: string
|
||||
projectId?: string
|
||||
triggerType?: T
|
||||
}) => {
|
||||
const { automationId, projectId, triggerType } = params
|
||||
|
||||
const revisionQuery = AutomationRevisions.knex()
|
||||
.select([AutomationRevisions.col.id])
|
||||
.where(AutomationRevisions.col.automationId, automationId)
|
||||
.andWhere(AutomationRevisions.col.active, true)
|
||||
.innerJoin(
|
||||
AutomationTriggers.name,
|
||||
AutomationTriggers.col.automationRevisionId,
|
||||
AutomationRevisions.col.id
|
||||
)
|
||||
.limit(1)
|
||||
|
||||
if (projectId) {
|
||||
revisionQuery
|
||||
const revisionQuery = tables
|
||||
.automationRevisions(deps.db)
|
||||
.select([AutomationRevisions.col.id])
|
||||
.where(AutomationRevisions.col.automationId, automationId)
|
||||
.andWhere(AutomationRevisions.col.active, true)
|
||||
.innerJoin(
|
||||
Automations.name,
|
||||
Automations.col.id,
|
||||
AutomationRevisions.col.automationId
|
||||
AutomationTriggers.name,
|
||||
AutomationTriggers.col.automationRevisionId,
|
||||
AutomationRevisions.col.id
|
||||
)
|
||||
.andWhere(Automations.col.projectId, projectId)
|
||||
.limit(1)
|
||||
|
||||
if (projectId) {
|
||||
revisionQuery
|
||||
.innerJoin(
|
||||
Automations.name,
|
||||
Automations.col.id,
|
||||
AutomationRevisions.col.automationId
|
||||
)
|
||||
.andWhere(Automations.col.projectId, projectId)
|
||||
}
|
||||
|
||||
const mainQ = tables
|
||||
.automationTriggers(deps.db)
|
||||
.select<AutomationTriggerDefinitionRecord<T>[]>('*')
|
||||
.where(AutomationTriggers.col.automationRevisionId, revisionQuery)
|
||||
|
||||
if (triggerType) {
|
||||
mainQ.andWhere(AutomationTriggers.col.triggerType, triggerType)
|
||||
}
|
||||
|
||||
return (await mainQ).map((r) => ({
|
||||
...r,
|
||||
automationId
|
||||
}))
|
||||
}
|
||||
|
||||
const mainQ = AutomationTriggers.knex<AutomationTriggerDefinitionRecord<T>[]>().where(
|
||||
AutomationTriggers.col.automationRevisionId,
|
||||
revisionQuery
|
||||
)
|
||||
|
||||
if (triggerType) {
|
||||
mainQ.andWhere(AutomationTriggers.col.triggerType, triggerType)
|
||||
}
|
||||
|
||||
return (await mainQ).map((r) => ({
|
||||
...r,
|
||||
automationId
|
||||
}))
|
||||
}
|
||||
|
||||
export async function updateFunctionRun(
|
||||
run: SetRequired<Partial<AutomationFunctionRunRecord>, 'id'>
|
||||
) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {
|
||||
InsertableAutomationRun,
|
||||
getAutomationTriggerDefinitions,
|
||||
getLatestAutomationRevision,
|
||||
getFullAutomationRevisionMetadataFactory
|
||||
} from '@/modules/automate/repositories/automations'
|
||||
@@ -46,6 +45,7 @@ import {
|
||||
GetAutomation,
|
||||
GetAutomationRevision,
|
||||
GetAutomationToken,
|
||||
GetAutomationTriggerDefinitions,
|
||||
GetEncryptionKeyPairFor,
|
||||
GetFullAutomationRevisionMetadata,
|
||||
TriggerAutomationRevisionRun,
|
||||
@@ -461,13 +461,14 @@ async function composeTriggerData(params: {
|
||||
}
|
||||
|
||||
export type ManuallyTriggerAutomationDeps = {
|
||||
getAutomationTriggerDefinitions: typeof getAutomationTriggerDefinitions
|
||||
getAutomationTriggerDefinitions: GetAutomationTriggerDefinitions
|
||||
getAutomation: GetAutomation
|
||||
getBranchLatestCommits: typeof getBranchLatestCommits
|
||||
triggerFunction: ReturnType<typeof triggerAutomationRevisionRunFactory>
|
||||
triggerFunction: TriggerAutomationRevisionRun
|
||||
validateStreamAccess: typeof validateStreamAccess
|
||||
}
|
||||
|
||||
export const manuallyTriggerAutomation =
|
||||
export const manuallyTriggerAutomationFactory =
|
||||
(deps: ManuallyTriggerAutomationDeps) =>
|
||||
async (params: {
|
||||
automationId: string
|
||||
@@ -480,7 +481,8 @@ export const manuallyTriggerAutomation =
|
||||
getAutomationTriggerDefinitions,
|
||||
getAutomation,
|
||||
getBranchLatestCommits,
|
||||
triggerFunction
|
||||
triggerFunction,
|
||||
validateStreamAccess
|
||||
} = deps
|
||||
|
||||
const [automation, triggerDefs] = await Promise.all([
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
import {
|
||||
ManuallyTriggerAutomationDeps,
|
||||
ensureRunConditionsFactory,
|
||||
manuallyTriggerAutomation,
|
||||
manuallyTriggerAutomationFactory,
|
||||
onModelVersionCreateFactory,
|
||||
triggerAutomationRevisionRunFactory
|
||||
} from '@/modules/automate/services/trigger'
|
||||
@@ -33,7 +33,6 @@ import {
|
||||
import { createTestCommit } from '@/test/speckle-helpers/commitHelper'
|
||||
import {
|
||||
InsertableAutomationRun,
|
||||
getAutomationTriggerDefinitions,
|
||||
updateAutomationRevision,
|
||||
updateAutomationRun,
|
||||
storeAutomationFactory,
|
||||
@@ -45,7 +44,8 @@ import {
|
||||
upsertAutomationFunctionRunFactory,
|
||||
getFullAutomationRunByIdFactory,
|
||||
upsertAutomationRunFactory,
|
||||
getAutomationTokenFactory
|
||||
getAutomationTokenFactory,
|
||||
getAutomationTriggerDefinitionsFactory
|
||||
} from '@/modules/automate/repositories/automations'
|
||||
import { beforeEachContext, truncateTables } from '@/test/hooks'
|
||||
import { Automate } from '@speckle/shared'
|
||||
@@ -75,6 +75,7 @@ import { mapGqlStatusToDbStatus } from '@/modules/automate/utils/automateFunctio
|
||||
import { db } from '@/db/knex'
|
||||
import { AutomateRunsEmitter } from '@/modules/automate/events/runs'
|
||||
import { createAppToken } from '@/modules/core/services/tokens'
|
||||
import { validateStreamAccess } from '@/modules/core/services/streams/streamAccessService'
|
||||
|
||||
const { FF_AUTOMATE_MODULE_ENABLED } = getFeatureFlags()
|
||||
|
||||
@@ -88,6 +89,7 @@ const upsertAutomationFunctionRun = upsertAutomationFunctionRunFactory({ db })
|
||||
const getFullAutomationRunById = getFullAutomationRunByIdFactory({ db })
|
||||
const upsertAutomationRun = upsertAutomationRunFactory({ db })
|
||||
const getAutomationToken = getAutomationTokenFactory({ db })
|
||||
const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({ db })
|
||||
|
||||
;(FF_AUTOMATE_MODULE_ENABLED ? describe : describe.skip)(
|
||||
'Automate triggers @automate',
|
||||
@@ -965,7 +967,7 @@ const getAutomationToken = getAutomationTokenFactory({ db })
|
||||
const buildManuallyTriggerAutomation = (
|
||||
overrides?: Partial<ManuallyTriggerAutomationDeps>
|
||||
) => {
|
||||
const trigger = manuallyTriggerAutomation({
|
||||
const trigger = manuallyTriggerAutomationFactory({
|
||||
getAutomationTriggerDefinitions,
|
||||
getAutomation,
|
||||
getBranchLatestCommits,
|
||||
@@ -982,6 +984,7 @@ const getAutomationToken = getAutomationTokenFactory({ db })
|
||||
getAutomationToken,
|
||||
upsertAutomationRun
|
||||
}),
|
||||
validateStreamAccess,
|
||||
...(overrides || {})
|
||||
})
|
||||
return trigger
|
||||
|
||||
Reference in New Issue
Block a user