diff --git a/src/components/tasks/apply-workflow-dialog.tsx b/src/components/tasks/apply-workflow-dialog.tsx index 5f39b3a..f77f25a 100644 --- a/src/components/tasks/apply-workflow-dialog.tsx +++ b/src/components/tasks/apply-workflow-dialog.tsx @@ -71,54 +71,59 @@ export function ApplyWorkflowDialog({ toast.error("Template has no tasks."); return; } - const inserts = items.map((it: any, idx: number) => ({ - title: it.title, - description: it.description, - priority: it.priority, - due_date: format(addDays(startDate, it.days_from_start), "yyyy-MM-dd"), - case_id: caseId, - workflow_template_id: templateId, - created_by: user.id, - sort_order: idx, - })); - const { data: created, error } = await supabase.from("tasks").insert(inserts).select("id"); + // Sequential mode: only spawn the FIRST task. Subsequent tasks are + // auto-spawned when the previous one is marked complete. + const first = items[0]; + const { data: created, error } = await supabase + .from("tasks") + .insert({ + title: first.title, + description: first.description, + priority: first.priority, + due_date: format(addDays(startDate, first.days_from_start), "yyyy-MM-dd"), + case_id: caseId, + workflow_template_id: templateId, + template_task_id: first.id, + created_by: user.id, + sort_order: 0, + }) + .select("id") + .single(); if (error || !created) { setSubmitting(false); toast.error(error?.message || "Failed to apply"); return; } - // Insert default assignees + history - const assigneeRows: any[] = []; - const historyRows: any[] = []; - const notifs: any[] = []; - created.forEach((row, idx) => { - const def = items[idx].default_assignee_id; - if (def) { - assigneeRows.push({ task_id: row.id, user_id: def, assigned_by: user.id }); - if (def !== user.id) { - notifs.push({ - user_id: def, + if (first.default_assignee_id) { + await supabase.from("task_assignees").insert({ + task_id: created.id, + user_id: first.default_assignee_id, + assigned_by: user.id, + }); + if (first.default_assignee_id !== user.id) { + await createNotifications([ + { + user_id: first.default_assignee_id, kind: "task_assigned", - title: `Assigned: ${items[idx].title}`, - link: `/tasks?task=${row.id}`, - task_id: row.id, + title: `Assigned: ${first.title}`, + link: `/tasks?task=${created.id}`, + task_id: created.id, case_id: caseId, created_by: user.id, - }); - } + }, + ]); } - historyRows.push({ - task_id: row.id, - actor_id: user.id, - event: "created_from_workflow", - detail: { template_id: templateId }, - }); + } + await supabase.from("task_history").insert({ + task_id: created.id, + actor_id: user.id, + event: "created_from_workflow", + detail: { template_id: templateId, sequential: true, total_steps: items.length }, }); - if (assigneeRows.length) await supabase.from("task_assignees").insert(assigneeRows); - if (historyRows.length) await supabase.from("task_history").insert(historyRows); - if (notifs.length) await createNotifications(notifs); setSubmitting(false); - toast.success(`Created ${created.length} tasks`); + toast.success( + `Workflow started — ${items.length} step${items.length === 1 ? "" : "s"} will run sequentially.`, + ); onApplied?.(); onOpenChange(false); };