import { apiRequestData } from "./http"; export type WorkflowDefinitionEditorModel = { id: string; entityType: "reimbursement" | "leave" | "purchase_request" | "travel_request"; name: string; version: number; status: "draft" | "published" | "archived" | "derived_readonly"; steps: Array<{ id: string; name: string; assigneeRule: | { type: "department_manager" } | { type: "role"; value: string } | { type: "specific_user"; value: string }; }>; notes?: string; source: "manual" | "approval_rules_projection"; createdAt: string | null; updatedAt: string | null; publishedAt: string | null; archivedAt: string | null; }; export async function fetchWorkflowDefinitions() { return apiRequestData<{ items: WorkflowDefinitionEditorModel[] }>("/api/workflow-definitions"); } export async function fetchWorkflowDefinition(id: string) { return apiRequestData<{ item: WorkflowDefinitionEditorModel }>(`/api/workflow-definitions/${id}`); } export async function fetchActiveWorkflowDefinition(entityType: WorkflowDefinitionEditorModel["entityType"]) { return apiRequestData<{ item: WorkflowDefinitionEditorModel | null }>(`/api/workflow-definitions/entity/${entityType}/active`); } export async function createWorkflowDefinition(payload: { entityType: WorkflowDefinitionEditorModel["entityType"]; name: string; steps: WorkflowDefinitionEditorModel["steps"]; notes?: string }) { return apiRequestData<{ item: WorkflowDefinitionEditorModel }>("/api/workflow-definitions", { method: "POST", body: JSON.stringify(payload) }); } export async function updateWorkflowDefinition(id: string, payload: Partial<{ name: string; steps: WorkflowDefinitionEditorModel["steps"]; notes?: string }>) { return apiRequestData<{ item: WorkflowDefinitionEditorModel }>(`/api/workflow-definitions/${id}`, { method: "PATCH", body: JSON.stringify(payload) }); } export async function publishWorkflowDefinition(id: string) { return apiRequestData<{ item: WorkflowDefinitionEditorModel }>(`/api/workflow-definitions/${id}/publish`, { method: "POST", body: JSON.stringify({}) }); } export async function archiveWorkflowDefinition(id: string) { return apiRequestData<{ item: WorkflowDefinitionEditorModel }>(`/api/workflow-definitions/${id}/archive`, { method: "POST", body: JSON.stringify({}) }); }