use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;
use validator::Validate;
use ironflow_store::entities::{Schedule, ScheduleSource};
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Serialize)]
pub struct ScheduleResponse {
pub id: Uuid,
pub workflow_name: String,
pub cron_expression: String,
pub inputs: Value,
pub source: ScheduleSource,
pub disabled_at: Option<DateTime<Utc>>,
pub last_triggered_at: Option<DateTime<Utc>>,
pub next_trigger_at: Option<DateTime<Utc>>,
pub created_by_user_id: Uuid,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl From<Schedule> for ScheduleResponse {
fn from(s: Schedule) -> Self {
Self {
id: s.id,
workflow_name: s.workflow_name,
cron_expression: s.cron_expression,
inputs: s.inputs,
source: s.source,
disabled_at: s.disabled_at,
last_triggered_at: s.last_triggered_at,
next_trigger_at: s.next_trigger_at,
created_by_user_id: s.created_by_user_id,
created_at: s.created_at,
updated_at: s.updated_at,
}
}
}
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Deserialize, Validate)]
pub struct CreateScheduleRequest {
#[validate(length(min = 1, message = "workflow_name must not be empty"))]
pub workflow_name: String,
#[validate(length(min = 1, message = "cron_expression must not be empty"))]
pub cron_expression: String,
#[serde(default = "default_inputs")]
pub inputs: Value,
}
fn default_inputs() -> Value {
Value::Object(serde_json::Map::new())
}
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Deserialize)]
pub struct UpdateScheduleRequest {
pub cron_expression: Option<String>,
pub inputs: Option<Value>,
}