diff --git a/src/routes/pages.rs b/src/routes/pages.rs index ed8cf38..78fd6ca 100644 --- a/src/routes/pages.rs +++ b/src/routes/pages.rs @@ -29,6 +29,7 @@ pub fn router() -> Router { .route("/ui/dernieres-entrees", get(recent_fragment)) .route("/ui/timer/start", post(start_timer)) .route("/ui/timer/stop", post(stop_timer)) + .route("/ui/timer/debut", post(set_timer_start)) .route("/ui/clients", post(create_client)) .route("/ui/clients/{id}", post(save_client)) .route("/ui/clients/{id}/archive", post(archive_client)) @@ -315,6 +316,32 @@ async fn stop_timer(State(pool): State) -> PageResult { Ok(with_entries_changed(render(timer_state(&pool).await?)?)) } +#[derive(Deserialize)] +struct TimerStartForm { + started_at: String, +} + +/// Moves the start of the running timer (e.g. started a bit late). +async fn set_timer_start( + State(pool): State, + Form(f): Form, +) -> PageResult { + let started = parse_datetime(&f.started_at, "début")?; + let running = entries::running(&pool) + .await? + .ok_or_else(|| AppError::Conflict("aucun timer en cours".into()))?; + entries::update( + &pool, + running.id, + &UpdateEntry { + started_at: Some(started.fixed_offset()), + ..Default::default() + }, + ) + .await?; + Ok(with_entries_changed(render(timer_state(&pool).await?)?)) +} + // --- Clients & projects ----------------------------------------------------- struct ClientCard { diff --git a/static/app.css b/static/app.css index 5cf1466..5131af8 100644 --- a/static/app.css +++ b/static/app.css @@ -95,6 +95,8 @@ label { display: flex; align-items: center; gap: .35rem; color: var(--muted); } .timer.is-running { background: var(--running); border-color: var(--color, var(--accent)); border-left-width: 6px; } .current { display: grid; grid-template-columns: 1fr auto auto; gap: 1rem; align-items: center; margin-bottom: 1rem; } .current-project { font-size: 1.3rem; font-weight: 600; } +.timer-start { margin-top: .35rem; font-size: .9rem; } +.timer-start input { width: 11rem; } .clock { font-size: 2rem; font-weight: 600; font-variant-numeric: tabular-nums; } .start { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr) auto; gap: .5rem; } .note { color: var(--muted); font-size: .9rem; } diff --git a/templates/_timer.html b/templates/_timer.html index 6d722b7..e515f69 100644 --- a/templates/_timer.html +++ b/templates/_timer.html @@ -5,6 +5,10 @@
{{ e.client_name }}
{{ e.project_name }}
{% if !e.note.is_empty() %}
{{ e.note }}
{% endif %} +
+ + +
{{ e.duration() }}
diff --git a/tests/api.rs b/tests/api.rs index 43af77f..36fe729 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -665,3 +665,40 @@ async fn duplicate_entry() { let (status, _) = get_html(&app, "/ui/entries/99/dupliquer").await; assert_eq!(status, StatusCode::NOT_FOUND); } + +#[tokio::test] +async fn move_timer_start() { + let app = app().await; + let p = project(&app, "Acme", "Site web").await; + + let (status, _) = send_form(&app, "/ui/timer/debut", "started_at=2026-03-10T09%3A00").await; + assert_eq!(status, StatusCode::CONFLICT, "no running timer"); + + let (status, _) = send_form(&app, "/ui/timer/start", &format!("project_id={p}")).await; + assert_eq!(status, StatusCode::OK); + + let earlier = (Local::now() - chrono::Duration::minutes(90)) + .format("%Y-%m-%dT%H:%M") + .to_string(); + let (status, html) = send_form(&app, "/ui/timer/debut", &format!("started_at={earlier}")).await; + assert_eq!(status, StatusCode::OK); + assert!(html.contains(&earlier) && html.contains("Arrêter")); + + let (_, timer) = send(&app, "GET", "/api/timer", None).await; + let started = + chrono::DateTime::parse_from_rfc3339(timer["started_at"].as_str().unwrap()).unwrap(); + assert_eq!( + started + .with_timezone(&Local) + .format("%Y-%m-%dT%H:%M") + .to_string(), + earlier + ); + assert!(timer["ended_at"].is_null(), "still running"); + + let future = (Local::now() + chrono::Duration::hours(1)) + .format("%Y-%m-%dT%H:%M") + .to_string(); + let (status, _) = send_form(&app, "/ui/timer/debut", &format!("started_at={future}")).await; + assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY); +}