@@ -89,6 +89,18 @@ export async function createManyWorkflows(
8989 return await Promise . all ( workflowRequests ) ;
9090}
9191
92+ export async function createManyActiveWorkflows (
93+ amount : number ,
94+ attributes : Partial < IWorkflowDb > = { } ,
95+ userOrProject ?: User | Project ,
96+ ) {
97+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
98+ const workflowRequests = [ ...Array ( amount ) ] . map (
99+ async ( _ ) => await createActiveWorkflow ( attributes , userOrProject ) ,
100+ ) ;
101+ return await Promise . all ( workflowRequests ) ;
102+ }
103+
92104export async function shareWorkflowWithUsers ( workflow : IWorkflowBase , users : User [ ] ) {
93105 const sharedWorkflows : Array < DeepPartial < SharedWorkflow > > = await Promise . all (
94106 users . map ( async ( user ) => {
@@ -135,7 +147,7 @@ export async function getWorkflowSharing(workflow: IWorkflowBase) {
135147 */
136148export async function createWorkflowWithTrigger (
137149 attributes : Partial < IWorkflowDb > = { } ,
138- user ?: User ,
150+ userOrProject ?: User | Project ,
139151) {
140152 const workflow = await createWorkflow (
141153 {
@@ -170,7 +182,7 @@ export async function createWorkflowWithTrigger(
170182 } ,
171183 ...attributes ,
172184 } ,
173- user ,
185+ userOrProject ,
174186 ) ;
175187
176188 return workflow ;
@@ -201,12 +213,12 @@ export async function createWorkflowWithHistory(
201213 */
202214export async function createWorkflowWithTriggerAndHistory (
203215 attributes : Partial < IWorkflowDb > = { } ,
204- user ?: User ,
216+ userOrProject ?: User | Project ,
205217) {
206- const workflow = await createWorkflowWithTrigger ( attributes , user ) ;
218+ const workflow = await createWorkflowWithTrigger ( attributes , userOrProject ) ;
207219
208220 // Create workflow history for the initial version
209- await createWorkflowHistory ( workflow , user ) ;
221+ await createWorkflowHistory ( workflow , userOrProject ) ;
210222
211223 return workflow ;
212224}
@@ -227,12 +239,78 @@ export const getWorkflowById = async (id: string) =>
227239 * @param workflow workflow to create history for
228240 * @param user user who created the version (optional)
229241 */
230- export async function createWorkflowHistory ( workflow : IWorkflowDb , user ?: User ) : Promise < void > {
242+ export async function createWorkflowHistory (
243+ workflow : IWorkflowDb ,
244+ userOrProject ?: User | Project ,
245+ ) : Promise < void > {
231246 await Container . get ( WorkflowHistoryRepository ) . insert ( {
232247 workflowId : workflow . id ,
233248 versionId : workflow . versionId ,
234249 nodes : workflow . nodes ,
235250 connections : workflow . connections ,
251+ authors :
userOrProject instanceof User ?
userOrProject . email :
'[email protected] ' , 252+ } ) ;
253+ }
254+
255+ /**
256+ * Set the active version for a workflow
257+ * @param workflowId workflow ID
258+ * @param versionId version ID to set as active
259+ */
260+ export async function setActiveVersion ( workflowId : string , versionId : string ) : Promise < void > {
261+ await Container . get ( WorkflowRepository )
262+ . createQueryBuilder ( )
263+ . update ( )
264+ . set ( { activeVersionId : versionId } )
265+ . where ( 'id = :workflowId' , { workflowId } )
266+ . execute ( ) ;
267+ }
268+
269+ /**
270+ * Create an active workflow with trigger, history, and activeVersionId set to the current version.
271+ * This simulates a workflow that has been activated and is running.
272+ * @param attributes workflow attributes
273+ * @param user user to assign the workflow to
274+ */
275+ export async function createActiveWorkflow (
276+ attributes : Partial < IWorkflowDb > = { } ,
277+ userOrProject ?: User | Project ,
278+ ) {
279+ const workflow = await createWorkflowWithTriggerAndHistory (
280+ { active : true , ...attributes } ,
281+ userOrProject ,
282+ ) ;
283+
284+ await setActiveVersion ( workflow . id , workflow . versionId ) ;
285+
286+ workflow . activeVersionId = workflow . versionId ;
287+ return workflow ;
288+ }
289+
290+ /**
291+ * Create a workflow with a specific active version.
292+ * This simulates a workflow where the active version differs from the current version.
293+ * @param activeVersionId the version ID to set as active
294+ * @param attributes workflow attributes
295+ * @param user user to assign the workflow to
296+ */
297+ export async function createWorkflowWithActiveVersion (
298+ activeVersionId : string ,
299+ attributes : Partial < IWorkflowDb > = { } ,
300+ user ?: User ,
301+ ) {
302+ const workflow = await createWorkflowWithTriggerAndHistory ( { active : true , ...attributes } , user ) ;
303+
304+ await Container . get ( WorkflowHistoryRepository ) . insert ( {
305+ workflowId : workflow . id ,
306+ versionId : activeVersionId ,
307+ nodes : workflow . nodes ,
308+ connections : workflow . connections ,
236309 authors :
user ?. email ?? '[email protected] ' , 237310 } ) ;
311+
312+ await setActiveVersion ( workflow . id , activeVersionId ) ;
313+
314+ workflow . activeVersionId = activeVersionId ;
315+ return workflow ;
238316}
0 commit comments