diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d652f9..2c7c66e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- `POST /entry` saves the day's `observation` (it was silently dropped) and `GET /entry/` returns it. A request without the field keeps the note already saved. + ## [2.0.1] - 2026-10-03 ### Changed diff --git a/app/routes/manual_entry.py b/app/routes/manual_entry.py index a72af23..855205b 100644 --- a/app/routes/manual_entry.py +++ b/app/routes/manual_entry.py @@ -1,12 +1,11 @@ from datetime import datetime -from typing import Any, Dict, Optional, cast from flask import Blueprint, jsonify, redirect, request, url_for from flask_babel import gettext as _ from app.auth import current_employee_id from app.db.database import db -from app.models.models import AbsenceCode, Employee, ScheduleEntry +from app.models.models import AbsenceCode, ScheduleEntry from app.utils.time_calculator import calculate_daily_hours from app.utils.validators import validate_date, validate_entries @@ -58,19 +57,15 @@ def save_entry(): entries = data.get("entries", []) - if existing_entry: - existing_entry.entries = [] if absence_code else entries - existing_entry.absence_code = absence_code - db.session.commit() - else: - schedule_entry = ScheduleEntry( - employee_id=employee_id, - date=entry_date, - entries=[] if absence_code else entries, - absence_code=absence_code, - ) - db.session.add(schedule_entry) - db.session.commit() + if existing_entry is None: + existing_entry = ScheduleEntry(employee_id=employee_id, date=entry_date) + db.session.add(existing_entry) + existing_entry.entries = [] if absence_code else entries + existing_entry.absence_code = absence_code + # A request without the key keeps the note already saved for the day. + if "observation" in data: + existing_entry.observation = (data["observation"] or "").strip() or None + db.session.commit() if absence_code is None: hours = calculate_daily_hours(entries) @@ -94,6 +89,7 @@ def get_entry(date): { "entries": entry.entries, "absence_code": entry.absence_code, + "observation": entry.observation, "hours": ( calculate_daily_hours(entry.entries) if not entry.absence_code diff --git a/tests/test_routes.py b/tests/test_routes.py index b09fd28..307d85a 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -183,6 +183,33 @@ def test_manual_entry_update_existing(self): self.assertEqual(len(entry.entries), 1) self.assertEqual(entry.entries[0]["exit"], "13:00") + def test_manual_entry_saves_observation(self): + entry_data = { + "date": "2025-03-20", + "entries": [{"entry": "09:00", "exit": "17:00"}], + "observation": " Dentist at noon ", + } + self.client.post( + "/entry", data=json.dumps(entry_data), content_type="application/json" + ) + data = json.loads(self.client.get("/entry/2025-03-20").data) + self.assertEqual(data["observation"], "Dentist at noon") + + # Without the key the note is kept; an empty string clears it. + entry_data.pop("observation") + self.client.post( + "/entry", data=json.dumps(entry_data), content_type="application/json" + ) + data = json.loads(self.client.get("/entry/2025-03-20").data) + self.assertEqual(data["observation"], "Dentist at noon") + + entry_data["observation"] = "" + self.client.post( + "/entry", data=json.dumps(entry_data), content_type="application/json" + ) + data = json.loads(self.client.get("/entry/2025-03-20").data) + self.assertIsNone(data["observation"]) + def test_get_entry_not_found(self): response = self.client.get("/entry/2025-01-01") self.assertEqual(response.status_code, 200)