use super::types::{McScheduleItem, McScheduleKind};
use crate::db::Pool;
use crate::db::models::CronJob;
use crate::db::repository::CronJobRepository;
pub async fn list(pool: Pool) -> Vec<McScheduleItem> {
let repo = CronJobRepository::new(pool);
let jobs = match repo.list_all().await {
Ok(j) => j,
Err(e) => {
tracing::warn!("schedule_service: failed to list cron jobs: {e}");
return Vec::new();
}
};
jobs.into_iter().map(item_from_cron).collect()
}
fn item_from_cron(job: CronJob) -> McScheduleItem {
let schedule = format_cron_schedule(&job);
McScheduleItem {
id: job.id.to_string(),
label: job.name,
schedule,
kind: McScheduleKind::Cron,
awaiting_user: !job.enabled,
}
}
fn format_cron_schedule(job: &CronJob) -> String {
let mut parts: Vec<String> = vec![job.cron_expr.clone()];
if !job.timezone.is_empty() && job.timezone != "UTC" {
parts.push(format!("({})", job.timezone));
}
if !job.enabled {
parts.push("paused".to_string());
} else if let Some(next) = job.next_run_at {
parts.push(format!("next {}", next.format("%Y-%m-%d %H:%M")));
}
parts.join(" ")
}