Skip to content

Commit 251046d

Browse files
icecrasher321waleedlatif1
authored andcommitted
fix(import): dedup workflow name (#3813)
1 parent 14089f7 commit 251046d

File tree

5 files changed

+49
-35
lines changed

5 files changed

+49
-35
lines changed

apps/sim/app/api/workflows/route.ts

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
88
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
99
import { generateRequestId } from '@/lib/core/utils/request'
1010
import { getNextWorkflowColor } from '@/lib/workflows/colors'
11-
import { listWorkflows, type WorkflowScope } from '@/lib/workflows/utils'
11+
import { deduplicateWorkflowName, listWorkflows, type WorkflowScope } from '@/lib/workflows/utils'
1212
import { getUserEntityPermissions, workspaceExists } from '@/lib/workspaces/permissions/utils'
1313
import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
1414

@@ -25,6 +25,7 @@ const CreateWorkflowSchema = z.object({
2525
workspaceId: z.string().optional(),
2626
folderId: z.string().nullable().optional(),
2727
sortOrder: z.number().int().optional(),
28+
deduplicate: z.boolean().optional(),
2829
})
2930

3031
// GET /api/workflows - Get workflows for user (optionally filtered by workspaceId)
@@ -126,12 +127,13 @@ export async function POST(req: NextRequest) {
126127
const body = await req.json()
127128
const {
128129
id: clientId,
129-
name,
130+
name: requestedName,
130131
description,
131132
color,
132133
workspaceId,
133134
folderId,
134135
sortOrder: providedSortOrder,
136+
deduplicate,
135137
} = CreateWorkflowSchema.parse(body)
136138

137139
if (!workspaceId) {
@@ -162,19 +164,6 @@ export async function POST(req: NextRequest) {
162164

163165
logger.info(`[${requestId}] Creating workflow ${workflowId} for user ${userId}`)
164166

165-
import('@/lib/core/telemetry')
166-
.then(({ PlatformEvents }) => {
167-
PlatformEvents.workflowCreated({
168-
workflowId,
169-
name,
170-
workspaceId: workspaceId || undefined,
171-
folderId: folderId || undefined,
172-
})
173-
})
174-
.catch(() => {
175-
// Silently fail
176-
})
177-
178167
let sortOrder: number
179168
if (providedSortOrder !== undefined) {
180169
sortOrder = providedSortOrder
@@ -214,31 +203,50 @@ export async function POST(req: NextRequest) {
214203
sortOrder = minSortOrder != null ? minSortOrder - 1 : 0
215204
}
216205

217-
const duplicateConditions = [
218-
eq(workflow.workspaceId, workspaceId),
219-
isNull(workflow.archivedAt),
220-
eq(workflow.name, name),
221-
]
206+
let name = requestedName
222207

223-
if (folderId) {
224-
duplicateConditions.push(eq(workflow.folderId, folderId))
208+
if (deduplicate) {
209+
name = await deduplicateWorkflowName(requestedName, workspaceId, folderId)
225210
} else {
226-
duplicateConditions.push(isNull(workflow.folderId))
227-
}
211+
const duplicateConditions = [
212+
eq(workflow.workspaceId, workspaceId),
213+
isNull(workflow.archivedAt),
214+
eq(workflow.name, requestedName),
215+
]
216+
217+
if (folderId) {
218+
duplicateConditions.push(eq(workflow.folderId, folderId))
219+
} else {
220+
duplicateConditions.push(isNull(workflow.folderId))
221+
}
228222

229-
const [duplicateWorkflow] = await db
230-
.select({ id: workflow.id })
231-
.from(workflow)
232-
.where(and(...duplicateConditions))
233-
.limit(1)
223+
const [duplicateWorkflow] = await db
224+
.select({ id: workflow.id })
225+
.from(workflow)
226+
.where(and(...duplicateConditions))
227+
.limit(1)
234228

235-
if (duplicateWorkflow) {
236-
return NextResponse.json(
237-
{ error: `A workflow named "${name}" already exists in this folder` },
238-
{ status: 409 }
239-
)
229+
if (duplicateWorkflow) {
230+
return NextResponse.json(
231+
{ error: `A workflow named "${requestedName}" already exists in this folder` },
232+
{ status: 409 }
233+
)
234+
}
240235
}
241236

237+
import('@/lib/core/telemetry')
238+
.then(({ PlatformEvents }) => {
239+
PlatformEvents.workflowCreated({
240+
workflowId,
241+
name,
242+
workspaceId: workspaceId || undefined,
243+
folderId: folderId || undefined,
244+
})
245+
})
246+
.catch(() => {
247+
// Silently fail
248+
})
249+
242250
await db.insert(workflow).values({
243251
id: workflowId,
244252
userId,

apps/sim/app/workspace/[workspaceId]/home/home.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function Home({ chatId }: HomeProps = {}) {
5454
description,
5555
color,
5656
workspaceId,
57+
deduplicate: true,
5758
}),
5859
})
5960

apps/sim/app/workspace/[workspaceId]/w/hooks/use-import-workflow.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function useImportWorkflow({ workspaceId }: UseImportWorkflowProps) {
5656
workspaceId,
5757
folderId,
5858
sortOrder,
59+
deduplicate: true,
5960
}),
6061
})
6162

apps/sim/app/workspace/[workspaceId]/w/hooks/use-import-workspace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export function useImportWorkspace({ onSuccess }: UseImportWorkspaceProps = {})
176176
color: workflowColor,
177177
workspaceId: newWorkspace.id,
178178
folderId: targetFolderId,
179+
deduplicate: true,
179180
}),
180181
})
181182

apps/sim/hooks/queries/workflows.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ interface CreateWorkflowVariables {
164164
folderId?: string | null
165165
sortOrder?: number
166166
id?: string
167+
deduplicate?: boolean
167168
}
168169

169170
interface CreateWorkflowResult {
@@ -300,7 +301,8 @@ export function useCreateWorkflow() {
300301

301302
return useMutation({
302303
mutationFn: async (variables: CreateWorkflowVariables): Promise<CreateWorkflowResult> => {
303-
const { workspaceId, name, description, color, folderId, sortOrder, id } = variables
304+
const { workspaceId, name, description, color, folderId, sortOrder, id, deduplicate } =
305+
variables
304306

305307
logger.info(`Creating new workflow in workspace: ${workspaceId}`)
306308

@@ -315,6 +317,7 @@ export function useCreateWorkflow() {
315317
workspaceId,
316318
folderId: folderId || null,
317319
sortOrder,
320+
deduplicate,
318321
}),
319322
})
320323

0 commit comments

Comments
 (0)