use axum::Extension; use axum::extract::{FromRef, Json, Path, Query, State}; use erp_core::error::AppError; use erp_core::rbac::require_permission; use erp_core::types::{ApiResponse, TenantContext}; use uuid::Uuid; use crate::dto::shift_dto::*; use crate::service::shift_service; use crate::state::HealthState; #[derive(Debug, serde::Deserialize)] pub struct PaginationParams { pub page: Option, pub page_size: Option, } // --------------------------------------------------------------------------- // Shift // --------------------------------------------------------------------------- pub async fn list_shifts( State(state): State, Extension(ctx): Extension, Query(params): Query, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.list")?; let result = shift_service::list_shifts(&state, ctx.tenant_id, ¶ms).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_shift( State(state): State, Extension(ctx): Extension, Path(shift_id): Path, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.list")?; let result = shift_service::get_shift(&state, ctx.tenant_id, shift_id).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn create_shift( State(state): State, Extension(ctx): Extension, Json(mut body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; body.sanitize(); let result = shift_service::create_shift(&state, ctx.tenant_id, Some(ctx.user_id), body).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn update_shift( State(state): State, Extension(ctx): Extension, Path(shift_id): Path, Json(body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; let result = shift_service::update_shift(&state, ctx.tenant_id, shift_id, Some(ctx.user_id), body) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn delete_shift( State(state): State, Extension(ctx): Extension, Path(shift_id): Path, Query(params): Query, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; shift_service::delete_shift( &state, ctx.tenant_id, shift_id, Some(ctx.user_id), params.version, ) .await?; Ok(Json(ApiResponse::ok(()))) } // --------------------------------------------------------------------------- // PatientAssignment // --------------------------------------------------------------------------- pub async fn list_assignments( State(state): State, Extension(ctx): Extension, Path(shift_id): Path, Query(params): Query, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.list")?; let page = params.page.unwrap_or(1); let page_size = params.page_size.unwrap_or(20); let result = shift_service::list_assignments(&state, ctx.tenant_id, shift_id, page, page_size).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn create_assignment( State(state): State, Extension(ctx): Extension, Path(shift_id): Path, Json(mut body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; body.sanitize(); let result = shift_service::create_assignment(&state, ctx.tenant_id, shift_id, Some(ctx.user_id), body) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn batch_assign( State(state): State, Extension(ctx): Extension, Path(shift_id): Path, Json(body): Json, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; let result = shift_service::batch_assign(&state, ctx.tenant_id, shift_id, Some(ctx.user_id), body) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn update_assignment( State(state): State, Extension(ctx): Extension, Path((shift_id, assignment_id)): Path<(Uuid, Uuid)>, Json(body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; let result = shift_service::update_assignment( &state, ctx.tenant_id, shift_id, assignment_id, Some(ctx.user_id), body, ) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn delete_assignment( State(state): State, Extension(ctx): Extension, Path((shift_id, assignment_id)): Path<(Uuid, Uuid)>, Query(params): Query, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; shift_service::delete_assignment( &state, ctx.tenant_id, shift_id, assignment_id, Some(ctx.user_id), params.version, ) .await?; Ok(Json(ApiResponse::ok(()))) } // --------------------------------------------------------------------------- // HandoffLog // --------------------------------------------------------------------------- pub async fn list_handoffs( State(state): State, Extension(ctx): Extension, Query(params): Query, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.list")?; let result = shift_service::list_handoffs(&state, ctx.tenant_id, ¶ms).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn create_handoff( State(state): State, Extension(ctx): Extension, Json(body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.shifts.manage")?; let result = shift_service::create_handoff(&state, ctx.tenant_id, Some(ctx.user_id), body).await?; Ok(Json(ApiResponse::ok(result))) }