( name: string, workspaceId: string, folderId: string | null | undefined, executor: Pick<typeof db, 'select'> = db )
| 59 | * lookup observes workflows inserted earlier in the same transaction. |
| 60 | */ |
| 61 | export async function deduplicateWorkflowName( |
| 62 | name: string, |
| 63 | workspaceId: string, |
| 64 | folderId: string | null | undefined, |
| 65 | executor: Pick<typeof db, 'select'> = db |
| 66 | ): Promise<string> { |
| 67 | const folderCondition = folderId |
| 68 | ? eq(workflowTable.folderId, folderId) |
| 69 | : isNull(workflowTable.folderId) |
| 70 | |
| 71 | const [existing] = await executor |
| 72 | .select({ id: workflowTable.id }) |
| 73 | .from(workflowTable) |
| 74 | .where( |
| 75 | and( |
| 76 | eq(workflowTable.workspaceId, workspaceId), |
| 77 | folderCondition, |
| 78 | eq(workflowTable.name, name), |
| 79 | isNull(workflowTable.archivedAt) |
| 80 | ) |
| 81 | ) |
| 82 | .limit(1) |
| 83 | |
| 84 | if (!existing) { |
| 85 | return name |
| 86 | } |
| 87 | |
| 88 | for (let i = 2; i < 100; i++) { |
| 89 | const candidate = `${name} (${i})` |
| 90 | const [dup] = await executor |
| 91 | .select({ id: workflowTable.id }) |
| 92 | .from(workflowTable) |
| 93 | .where( |
| 94 | and( |
| 95 | eq(workflowTable.workspaceId, workspaceId), |
| 96 | folderCondition, |
| 97 | eq(workflowTable.name, candidate), |
| 98 | isNull(workflowTable.archivedAt) |
| 99 | ) |
| 100 | ) |
| 101 | .limit(1) |
| 102 | |
| 103 | if (!dup) { |
| 104 | return candidate |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return `${name} (${generateId().slice(0, 6)})` |
| 109 | } |
| 110 | |
| 111 | export type WorkflowResolutionResult = |
| 112 | | { |
no test coverage detected