( workflowId: string, options: RestoreWorkflowOptions )
| 211 | } |
| 212 | |
| 213 | export async function restoreWorkflow( |
| 214 | workflowId: string, |
| 215 | options: RestoreWorkflowOptions |
| 216 | ): Promise<{ restored: boolean; workflow: Awaited<ReturnType<typeof getWorkflowById>> | null }> { |
| 217 | const existingWorkflow = await getWorkflowById(workflowId, { includeArchived: true }) |
| 218 | |
| 219 | if (!existingWorkflow) { |
| 220 | return { restored: false, workflow: null } |
| 221 | } |
| 222 | |
| 223 | if (!existingWorkflow.archivedAt) { |
| 224 | return { restored: false, workflow: existingWorkflow } |
| 225 | } |
| 226 | |
| 227 | if (existingWorkflow.workspaceId) { |
| 228 | const { getWorkspaceWithOwner } = await import('@/lib/workspaces/permissions/utils') |
| 229 | const ws = await getWorkspaceWithOwner(existingWorkflow.workspaceId) |
| 230 | if (!ws || ws.archivedAt) { |
| 231 | throw new Error('Cannot restore workflow into an archived workspace') |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | let clearFolderId = false |
| 236 | if (existingWorkflow.folderId) { |
| 237 | const [folder] = await db |
| 238 | .select({ archivedAt: workflowFolder.archivedAt }) |
| 239 | .from(workflowFolder) |
| 240 | .where(eq(workflowFolder.id, existingWorkflow.folderId)) |
| 241 | |
| 242 | if (!folder || folder.archivedAt) { |
| 243 | clearFolderId = true |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | const now = new Date() |
| 248 | |
| 249 | await db.transaction(async (tx) => { |
| 250 | await tx |
| 251 | .update(workflow) |
| 252 | .set({ |
| 253 | archivedAt: null, |
| 254 | updatedAt: now, |
| 255 | ...(clearFolderId && { folderId: null }), |
| 256 | }) |
| 257 | .where(eq(workflow.id, workflowId)) |
| 258 | |
| 259 | await tx |
| 260 | .update(workflowSchedule) |
| 261 | .set({ archivedAt: null, updatedAt: now }) |
| 262 | .where(eq(workflowSchedule.workflowId, workflowId)) |
| 263 | |
| 264 | await tx |
| 265 | .update(webhook) |
| 266 | .set({ archivedAt: null, updatedAt: now }) |
| 267 | .where(eq(webhook.workflowId, workflowId)) |
| 268 | |
| 269 | await tx |
| 270 | .update(chat) |
no test coverage detected