MCPcopy Index your code
hub / github.com/simstudioai/sim / createWorkflowRecord

Function createWorkflowRecord

apps/sim/lib/workflows/utils.ts:378–459  ·  view source on GitHub ↗
(params: CreateWorkflowInput)

Source from the content-addressed store, hash-verified

376}
377
378export async function createWorkflowRecord(params: CreateWorkflowInput) {
379 const { userId, workspaceId, name, description = null, folderId = null } = params
380 const workflowId = generateId()
381 const now = new Date()
382
383 const duplicateConditions = [
384 eq(workflowTable.workspaceId, workspaceId),
385 isNull(workflowTable.archivedAt),
386 eq(workflowTable.name, name),
387 ...(folderId ? [eq(workflowTable.folderId, folderId)] : [isNull(workflowTable.folderId)]),
388 ]
389 const [duplicateWorkflow] = await db
390 .select({ id: workflowTable.id })
391 .from(workflowTable)
392 .where(and(...duplicateConditions))
393 .limit(1)
394 if (duplicateWorkflow) {
395 throw new Error(
396 `A workflow named "${name}" already exists in this folder. Use a different name.`
397 )
398 }
399
400 const workflowParentCondition = folderId
401 ? eq(workflowTable.folderId, folderId)
402 : isNull(workflowTable.folderId)
403 const folderParentCondition = folderId
404 ? eq(workflowFolder.parentId, folderId)
405 : isNull(workflowFolder.parentId)
406
407 const [[workflowMinResult], [folderMinResult]] = await Promise.all([
408 db
409 .select({ minOrder: min(workflowTable.sortOrder) })
410 .from(workflowTable)
411 .where(
412 and(
413 eq(workflowTable.workspaceId, workspaceId),
414 workflowParentCondition,
415 isNull(workflowTable.archivedAt)
416 )
417 ),
418 db
419 .select({ minOrder: min(workflowFolder.sortOrder) })
420 .from(workflowFolder)
421 .where(and(eq(workflowFolder.workspaceId, workspaceId), folderParentCondition)),
422 ])
423
424 const minSortOrder = [workflowMinResult?.minOrder, folderMinResult?.minOrder].reduce<
425 number | null
426 >((currentMin, candidate) => {
427 if (candidate == null) return currentMin
428 if (currentMin == null) return candidate
429 return Math.min(currentMin, candidate)
430 }, null)
431
432 const sortOrder = minSortOrder != null ? minSortOrder - 1 : 0
433
434 await db.insert(workflowTable).values({
435 id: workflowId,

Callers

nothing calls this directly

Calls 5

generateIdFunction · 0.90
eqFunction · 0.50

Tested by

no test coverage detected