use axum::Extension; use axum::extract::{FromRef, Path, Query, State}; use axum::response::Json; use validator::Validate; use erp_core::error::AppError; use erp_core::rbac::require_permission; use erp_core::types::{ApiResponse, PaginatedResponse, Pagination, TenantContext}; use uuid::Uuid; use crate::dto::{CreateProcessDefinitionReq, ProcessDefinitionResp, UpdateProcessDefinitionReq}; use crate::service::definition_service::DefinitionService; use crate::workflow_state::WorkflowState; /// GET /api/v1/workflow/definitions pub async fn list_definitions( State(state): State, Extension(ctx): Extension, Query(pagination): Query, ) -> Result>>, AppError> where WorkflowState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "workflow:list")?; let (defs, total) = DefinitionService::list(ctx.tenant_id, &pagination, &state.db).await?; let page = pagination.page.unwrap_or(1); let page_size = pagination.limit(); let total_pages = total.div_ceil(page_size); Ok(Json(ApiResponse::ok(PaginatedResponse { data: defs, total, page, page_size, total_pages, }))) } /// POST /api/v1/workflow/definitions pub async fn create_definition( State(state): State, Extension(ctx): Extension, Json(req): Json, ) -> Result>, AppError> where WorkflowState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "workflow:create")?; req.validate() .map_err(|e| AppError::Validation(e.to_string()))?; let resp = DefinitionService::create( ctx.tenant_id, ctx.user_id, &req, &state.db, &state.event_bus, ) .await?; Ok(Json(ApiResponse::ok(resp))) } /// GET /api/v1/workflow/definitions/{id} pub async fn get_definition( State(state): State, Extension(ctx): Extension, Path(id): Path, ) -> Result>, AppError> where WorkflowState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "workflow:read")?; let resp = DefinitionService::get_by_id(id, ctx.tenant_id, &state.db).await?; Ok(Json(ApiResponse::ok(resp))) } /// PUT /api/v1/workflow/definitions/{id} pub async fn update_definition( State(state): State, Extension(ctx): Extension, Path(id): Path, Json(req): Json, ) -> Result>, AppError> where WorkflowState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "workflow:update")?; let resp = DefinitionService::update(id, ctx.tenant_id, ctx.user_id, &req, &state.db).await?; Ok(Json(ApiResponse::ok(resp))) } /// POST /api/v1/workflow/definitions/{id}/publish pub async fn publish_definition( State(state): State, Extension(ctx): Extension, Path(id): Path, ) -> Result>, AppError> where WorkflowState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "workflow:publish")?; let resp = DefinitionService::publish( id, ctx.tenant_id, ctx.user_id, &state.db, &state.event_bus, ) .await?; Ok(Json(ApiResponse::ok(resp))) }