Merge pull request #3066 from specklesystems/fabians/automate-ioc-12
chore(server): automate IoC 12 - createTestAutomationRunFactory
This commit is contained in:
@@ -120,6 +120,14 @@ export type GetAutomationTriggerDefinitions = <
|
||||
triggerType?: T
|
||||
}) => Promise<Array<AutomationTriggerDefinitionRecord<T> & { automationId: string }>>
|
||||
|
||||
export type GetLatestAutomationRevisions = (params: {
|
||||
automationIds: string[]
|
||||
}) => Promise<{ [automationId: string]: AutomationRevisionRecord }>
|
||||
|
||||
export type GetLatestAutomationRevision = (params: {
|
||||
automationId: string
|
||||
}) => Promise<Nullable<AutomationRevisionRecord>>
|
||||
|
||||
export type CreateStoredAuthCode = (
|
||||
params: Omit<AuthCodePayload, 'code'>
|
||||
) => Promise<AuthCodePayload>
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
getAutomationTriggerDefinitionsFactory,
|
||||
getFullAutomationRevisionMetadataFactory,
|
||||
getFunctionRunFactory,
|
||||
getLatestAutomationRevision,
|
||||
getLatestAutomationRevisionFactory,
|
||||
getLatestVersionAutomationRunsFactory,
|
||||
getProjectAutomationsItems,
|
||||
getProjectAutomationsTotalCount,
|
||||
@@ -63,7 +63,7 @@ import {
|
||||
getBranchesByIds
|
||||
} from '@/modules/core/repositories/branches'
|
||||
import {
|
||||
createTestAutomationRun,
|
||||
createTestAutomationRunFactory,
|
||||
manuallyTriggerAutomationFactory,
|
||||
triggerAutomationRevisionRunFactory
|
||||
} from '@/modules/automate/services/trigger'
|
||||
@@ -126,6 +126,7 @@ const getFullAutomationRevisionMetadata = getFullAutomationRevisionMetadataFacto
|
||||
const getAutomationToken = getAutomationTokenFactory({ db })
|
||||
const upsertAutomationRun = upsertAutomationRunFactory({ db })
|
||||
const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({ db })
|
||||
const getLatestAutomationRevision = getLatestAutomationRevisionFactory({ db })
|
||||
|
||||
export = (FF_AUTOMATE_MODULE_ENABLED
|
||||
? {
|
||||
@@ -546,7 +547,8 @@ export = (FF_AUTOMATE_MODULE_ENABLED
|
||||
createAppToken,
|
||||
automateRunsEmitter: AutomateRunsEmitter.emit,
|
||||
getAutomationToken,
|
||||
upsertAutomationRun
|
||||
upsertAutomationRun,
|
||||
getFullAutomationRevisionMetadata
|
||||
}),
|
||||
validateStreamAccess
|
||||
})
|
||||
@@ -578,7 +580,7 @@ export = (FF_AUTOMATE_MODULE_ENABLED
|
||||
})
|
||||
},
|
||||
async createTestAutomationRun(parent, { automationId }, ctx) {
|
||||
const create = createTestAutomationRun({
|
||||
const create = createTestAutomationRunFactory({
|
||||
getEncryptionKeyPairFor,
|
||||
getFunctionInputDecryptor: getFunctionInputDecryptorFactory({
|
||||
buildDecryptor
|
||||
@@ -586,7 +588,9 @@ export = (FF_AUTOMATE_MODULE_ENABLED
|
||||
getAutomation,
|
||||
getLatestAutomationRevision,
|
||||
getFullAutomationRevisionMetadata,
|
||||
upsertAutomationRun
|
||||
upsertAutomationRun,
|
||||
getBranchLatestCommits,
|
||||
validateStreamAccess
|
||||
})
|
||||
|
||||
return await create({
|
||||
|
||||
@@ -70,6 +70,9 @@ async function initScopes() {
|
||||
|
||||
const initializeEventListeners = () => {
|
||||
const getAutomationRunFullTriggers = getAutomationRunFullTriggersFactory({ db })
|
||||
const getFullAutomationRevisionMetadata = getFullAutomationRevisionMetadataFactory({
|
||||
db
|
||||
})
|
||||
|
||||
const triggerFn = triggerAutomationRevisionRunFactory({
|
||||
automateRunTrigger: triggerAutomationRun,
|
||||
@@ -80,7 +83,8 @@ const initializeEventListeners = () => {
|
||||
createAppToken,
|
||||
automateRunsEmitter: AutomateRunsEmitter.emit,
|
||||
getAutomationToken: getAutomationTokenFactory({ db }),
|
||||
upsertAutomationRun: upsertAutomationRunFactory({ db })
|
||||
upsertAutomationRun: upsertAutomationRunFactory({ db }),
|
||||
getFullAutomationRevisionMetadata
|
||||
})
|
||||
const setupStatusUpdateSubscriptionsInvoke = setupStatusUpdateSubscriptionsFactory({
|
||||
getAutomationRunFullTriggers,
|
||||
@@ -93,7 +97,7 @@ const initializeEventListeners = () => {
|
||||
publish
|
||||
})
|
||||
const setupRunFinishedTrackingInvoke = setupRunFinishedTrackingFactory({
|
||||
getFullAutomationRevisionMetadata: getFullAutomationRevisionMetadataFactory({ db }),
|
||||
getFullAutomationRevisionMetadata,
|
||||
getUserById,
|
||||
getCommit,
|
||||
getFullAutomationRunById: getFullAutomationRunByIdFactory({ db }),
|
||||
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
GetFullAutomationRevisionMetadata,
|
||||
GetFullAutomationRunById,
|
||||
GetFunctionRun,
|
||||
GetLatestAutomationRevision,
|
||||
GetLatestAutomationRevisions,
|
||||
GetLatestVersionAutomationRuns,
|
||||
StoreAutomation,
|
||||
StoreAutomationRevision,
|
||||
@@ -593,42 +595,45 @@ export const getAutomationRevisionFactory =
|
||||
return (revisions[0] || null) as Nullable<(typeof revisions)[0]>
|
||||
}
|
||||
|
||||
export async function getLatestAutomationRevisions(params: {
|
||||
automationIds: string[]
|
||||
}) {
|
||||
const { automationIds } = params
|
||||
if (!automationIds.length) return {}
|
||||
export const getLatestAutomationRevisionsFactory =
|
||||
(deps: { db: Knex }): GetLatestAutomationRevisions =>
|
||||
async (params: { automationIds: string[] }) => {
|
||||
const { automationIds } = params
|
||||
if (!automationIds.length) return {}
|
||||
|
||||
const innerQ = AutomationRevisions.knex()
|
||||
.select([
|
||||
AutomationRevisions.col.automationId,
|
||||
knex.raw('max(??) as ??', [AutomationRevisions.col.createdAt, 'maxCreatedAt'])
|
||||
])
|
||||
.whereIn(AutomationRevisions.col.automationId, automationIds)
|
||||
.andWhere(AutomationRevisions.col.active, true)
|
||||
.groupBy(AutomationRevisions.col.automationId)
|
||||
const innerQ = tables
|
||||
.automationRevisions(deps.db)
|
||||
.select([
|
||||
AutomationRevisions.col.automationId,
|
||||
knex.raw('max(??) as ??', [AutomationRevisions.col.createdAt, 'maxCreatedAt'])
|
||||
])
|
||||
.whereIn(AutomationRevisions.col.automationId, automationIds)
|
||||
.andWhere(AutomationRevisions.col.active, true)
|
||||
.groupBy(AutomationRevisions.col.automationId)
|
||||
|
||||
const outerQ = AutomationRevisions.knex<AutomationRevisionRecord[]>().innerJoin(
|
||||
innerQ.as('q1'),
|
||||
function () {
|
||||
this.on(AutomationRevisions.col.automationId, '=', 'q1.automationId')
|
||||
this.andOn(AutomationRevisions.col.createdAt, '=', 'q1.maxCreatedAt')
|
||||
}
|
||||
)
|
||||
const outerQ = tables
|
||||
.automationRevisions(deps.db)
|
||||
.select<AutomationRevisionRecord[]>('*')
|
||||
.innerJoin(innerQ.as('q1'), function () {
|
||||
this.on(AutomationRevisions.col.automationId, '=', 'q1.automationId')
|
||||
this.andOn(AutomationRevisions.col.createdAt, '=', 'q1.maxCreatedAt')
|
||||
})
|
||||
|
||||
const res = await outerQ
|
||||
return keyBy(res, (r) => r.automationId)
|
||||
}
|
||||
const res = await outerQ
|
||||
return keyBy(res, (r) => r.automationId)
|
||||
}
|
||||
|
||||
export async function getLatestAutomationRevision(params: { automationId: string }) {
|
||||
const { automationId } = params
|
||||
export const getLatestAutomationRevisionFactory =
|
||||
(deps: { db: Knex }): GetLatestAutomationRevision =>
|
||||
async (params: { automationId: string }) => {
|
||||
const { automationId } = params
|
||||
|
||||
const revisions = await getLatestAutomationRevisions({
|
||||
automationIds: [automationId]
|
||||
})
|
||||
const revisions = await getLatestAutomationRevisionsFactory(deps)({
|
||||
automationIds: [automationId]
|
||||
})
|
||||
|
||||
return (revisions[automationId] ?? null) as Nullable<(typeof revisions)[0]>
|
||||
}
|
||||
return (revisions[automationId] ?? null) as Nullable<(typeof revisions)[0]>
|
||||
}
|
||||
|
||||
export async function getRevisionsTriggerDefinitions(params: {
|
||||
automationRevisionIds: string[]
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
InsertableAutomationRun,
|
||||
getLatestAutomationRevision,
|
||||
getFullAutomationRevisionMetadataFactory
|
||||
} from '@/modules/automate/repositories/automations'
|
||||
import { InsertableAutomationRun } from '@/modules/automate/repositories/automations'
|
||||
import {
|
||||
AutomationWithRevision,
|
||||
AutomationTriggerDefinitionRecord,
|
||||
@@ -48,15 +44,10 @@ import {
|
||||
GetAutomationTriggerDefinitions,
|
||||
GetEncryptionKeyPairFor,
|
||||
GetFullAutomationRevisionMetadata,
|
||||
GetLatestAutomationRevision,
|
||||
TriggerAutomationRevisionRun,
|
||||
UpsertAutomationRun
|
||||
} from '@/modules/automate/domain/operations'
|
||||
import { db } from '@/db/knex'
|
||||
|
||||
// TODO: Fixed in next PRs
|
||||
const getFullAutomationRevisionMetadata = getFullAutomationRevisionMetadataFactory({
|
||||
db
|
||||
})
|
||||
|
||||
export type OnModelVersionCreateDeps = {
|
||||
getAutomation: GetAutomation
|
||||
@@ -224,6 +215,7 @@ export type TriggerAutomationRevisionRunDeps = {
|
||||
createAppToken: typeof createAppToken
|
||||
upsertAutomationRun: UpsertAutomationRun
|
||||
automateRunsEmitter: AutomateRunsEventsEmitter
|
||||
getFullAutomationRevisionMetadata: GetFullAutomationRevisionMetadata
|
||||
} & CreateAutomationRunDataDeps
|
||||
|
||||
/**
|
||||
@@ -241,7 +233,8 @@ export const triggerAutomationRevisionRunFactory =
|
||||
getAutomationToken,
|
||||
createAppToken,
|
||||
upsertAutomationRun,
|
||||
automateRunsEmitter
|
||||
automateRunsEmitter,
|
||||
getFullAutomationRevisionMetadata
|
||||
} = deps
|
||||
const { revisionId, manifest, source = RunTriggerSource.Automatic } = params
|
||||
|
||||
@@ -537,22 +530,26 @@ export const manuallyTriggerAutomationFactory =
|
||||
|
||||
export type CreateTestAutomationRunDeps = {
|
||||
getAutomation: GetAutomation
|
||||
getLatestAutomationRevision: typeof getLatestAutomationRevision
|
||||
getFullAutomationRevisionMetadata: typeof getFullAutomationRevisionMetadata
|
||||
getLatestAutomationRevision: GetLatestAutomationRevision
|
||||
getFullAutomationRevisionMetadata: GetFullAutomationRevisionMetadata
|
||||
upsertAutomationRun: UpsertAutomationRun
|
||||
validateStreamAccess: typeof validateStreamAccess
|
||||
getBranchLatestCommits: typeof getBranchLatestCommits
|
||||
} & CreateAutomationRunDataDeps
|
||||
|
||||
/**
|
||||
* TODO: Reduce duplication w/ other fns in this service
|
||||
*/
|
||||
export const createTestAutomationRun =
|
||||
export const createTestAutomationRunFactory =
|
||||
(deps: CreateTestAutomationRunDeps) =>
|
||||
async (params: { projectId: string; automationId: string; userId: string }) => {
|
||||
const {
|
||||
getAutomation,
|
||||
getLatestAutomationRevision,
|
||||
getFullAutomationRevisionMetadata,
|
||||
upsertAutomationRun
|
||||
upsertAutomationRun,
|
||||
validateStreamAccess,
|
||||
getBranchLatestCommits
|
||||
} = deps
|
||||
const { projectId, automationId, userId } = params
|
||||
|
||||
|
||||
@@ -45,7 +45,8 @@ import {
|
||||
getFullAutomationRunByIdFactory,
|
||||
upsertAutomationRunFactory,
|
||||
getAutomationTokenFactory,
|
||||
getAutomationTriggerDefinitionsFactory
|
||||
getAutomationTriggerDefinitionsFactory,
|
||||
getFullAutomationRevisionMetadataFactory
|
||||
} from '@/modules/automate/repositories/automations'
|
||||
import { beforeEachContext, truncateTables } from '@/test/hooks'
|
||||
import { Automate } from '@speckle/shared'
|
||||
@@ -90,6 +91,9 @@ const getFullAutomationRunById = getFullAutomationRunByIdFactory({ db })
|
||||
const upsertAutomationRun = upsertAutomationRunFactory({ db })
|
||||
const getAutomationToken = getAutomationTokenFactory({ db })
|
||||
const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({ db })
|
||||
const getFullAutomationRevisionMetadata = getFullAutomationRevisionMetadataFactory({
|
||||
db
|
||||
})
|
||||
|
||||
;(FF_AUTOMATE_MODULE_ENABLED ? describe : describe.skip)(
|
||||
'Automate triggers @automate',
|
||||
@@ -313,7 +317,8 @@ const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({
|
||||
createAppToken,
|
||||
automateRunsEmitter: AutomateRunsEmitter.emit,
|
||||
getAutomationToken,
|
||||
upsertAutomationRun
|
||||
upsertAutomationRun,
|
||||
getFullAutomationRevisionMetadata
|
||||
})({
|
||||
revisionId: cryptoRandomString({ length: 10 }),
|
||||
manifest: <VersionCreatedTriggerManifest>{
|
||||
@@ -404,7 +409,8 @@ const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({
|
||||
createAppToken,
|
||||
automateRunsEmitter: AutomateRunsEmitter.emit,
|
||||
getAutomationToken,
|
||||
upsertAutomationRun
|
||||
upsertAutomationRun,
|
||||
getFullAutomationRevisionMetadata
|
||||
})({
|
||||
revisionId: automationRevisionId,
|
||||
manifest: <VersionCreatedTriggerManifest>{
|
||||
@@ -501,7 +507,8 @@ const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({
|
||||
createAppToken,
|
||||
automateRunsEmitter: AutomateRunsEmitter.emit,
|
||||
getAutomationToken,
|
||||
upsertAutomationRun
|
||||
upsertAutomationRun,
|
||||
getFullAutomationRevisionMetadata
|
||||
})({
|
||||
revisionId: automationRevisionId,
|
||||
manifest: <VersionCreatedTriggerManifest>{
|
||||
@@ -982,7 +989,8 @@ const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({
|
||||
createAppToken,
|
||||
automateRunsEmitter: AutomateRunsEmitter.emit,
|
||||
getAutomationToken,
|
||||
upsertAutomationRun
|
||||
upsertAutomationRun,
|
||||
getFullAutomationRevisionMetadata
|
||||
}),
|
||||
validateStreamAccess,
|
||||
...(overrides || {})
|
||||
|
||||
@@ -65,7 +65,7 @@ import {
|
||||
getAutomationRunsTriggers,
|
||||
getAutomationsFactory,
|
||||
getFunctionAutomationCounts,
|
||||
getLatestAutomationRevisions,
|
||||
getLatestAutomationRevisionsFactory,
|
||||
getRevisionsFunctions,
|
||||
getRevisionsTriggerDefinitions
|
||||
} from '@/modules/automate/repositories/automations'
|
||||
@@ -92,6 +92,7 @@ const getStreamPendingModels = getStreamPendingModelsFactory({ db })
|
||||
const getAppScopes = getAppScopesFactory({ db })
|
||||
const getAutomations = getAutomationsFactory({ db })
|
||||
const getAutomationRevisions = getAutomationRevisionsFactory({ db })
|
||||
const getLatestAutomationRevisions = getLatestAutomationRevisionsFactory({ db })
|
||||
|
||||
/**
|
||||
* TODO: Lazy load DataLoaders to reduce memory usage
|
||||
|
||||
Reference in New Issue
Block a user