diff --git a/backend/app/crud/crud_local_flight.py b/backend/app/crud/crud_local_flight.py index d436167..31924b2 100644 --- a/backend/app/crud/crud_local_flight.py +++ b/backend/app/crud/crud_local_flight.py @@ -187,7 +187,7 @@ class CRUDLocalFlight: current_time = timestamp if timestamp is not None else datetime.utcnow() if status == LocalFlightStatus.GROUND: db_obj.contact_dt = current_time - elif status == LocalFlightStatus.DEPARTED: + elif status == LocalFlightStatus.DEPARTED and not db_obj.departed_dt: db_obj.departed_dt = current_time elif status == LocalFlightStatus.LANDED and not db_obj.landed_dt: db_obj.landed_dt = current_time @@ -200,6 +200,8 @@ class CRUDLocalFlight: # Takeoff: happens once when transitioning away from GROUND if old_status == LocalFlightStatus.GROUND and status in (LocalFlightStatus.DEPARTED, LocalFlightStatus.LOCAL, LocalFlightStatus.CIRCUIT) and not db_obj.takeoff_dt: db_obj.takeoff_dt = current_time + if not db_obj.departed_dt: + db_obj.departed_dt = current_time db.add(db_obj) db.commit() diff --git a/backend/tests/test_flight_strip_apis.py b/backend/tests/test_flight_strip_apis.py index 088ce8f..5c595f3 100644 --- a/backend/tests/test_flight_strip_apis.py +++ b/backend/tests/test_flight_strip_apis.py @@ -194,6 +194,7 @@ def test_local_flight_lifecycle_special_lists_and_not_found_paths(auth_client, d assert departed_response.status_code == 200 assert departed_response.json()["takeoff_dt"] == "2026-06-20T10:05:00" + assert departed_response.json()["departed_dt"] == "2026-06-20T10:05:00" assert landed_response.status_code == 200 assert landed_response.json()["landed_dt"] == "2026-06-20T10:45:00" @@ -222,6 +223,40 @@ def test_local_flight_lifecycle_special_lists_and_not_found_paths(auth_client, d assert auth_client.delete("/api/v1/local-flights/404").status_code == 404 +def test_local_flight_takeoff_to_local_sets_departed_dt(auth_client): + create_response = auth_client.post( + "/api/v1/local-flights/", + json={ + "registration": "g-air", + "type": "PA28", + "pob": 2, + "flight_type": "LOCAL", + "duration": 30, + "etd": "2026-06-20T09:00:00", + }, + ) + assert create_response.status_code == 200 + + takeoff_response = auth_client.patch( + f"/api/v1/local-flights/{create_response.json()['id']}/status", + json={"status": "LOCAL", "timestamp": "2026-06-20T09:05:00"}, + ) + + assert takeoff_response.status_code == 200 + assert takeoff_response.json()["status"] == "LOCAL" + assert takeoff_response.json()["takeoff_dt"] == "2026-06-20T09:05:00" + assert takeoff_response.json()["departed_dt"] == "2026-06-20T09:05:00" + + landing_response = auth_client.patch( + f"/api/v1/local-flights/{create_response.json()['id']}/status", + json={"status": "LANDED", "timestamp": "2026-06-20T09:35:00"}, + ) + + assert landing_response.status_code == 200 + assert landing_response.json()["status"] == "LANDED" + assert landing_response.json()["landed_dt"] == "2026-06-20T09:35:00" + + def test_overflight_lifecycle_special_lists_and_not_found_paths(auth_client, db): payload = { "registration": "g-ovr",