diff --git a/app/__init__.py b/app/__init__.py index e059599..cd64a97 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -101,6 +101,7 @@ def create_app(test_config: dict | None = None) -> Flask: start_time_local=body.start_time_local, timezone_name=body.timezone, amount_usd=body.amount_usd, + duration_minutes=body.duration_minutes, db_path=db_path, ) return jsonify(AppointmentResponse.model_validate(row).model_dump()), 201 @@ -139,6 +140,7 @@ def create_app(test_config: dict | None = None) -> Flask: topic=f"Sesión con {row['client_name']}", start_time_local=row["start_time_local"], tz_name=row["timezone"], + duration_minutes=row["duration_minutes"], ) updated = db.mark_appointment_confirmed( session_id=row["session_id"], diff --git a/app/db.py b/app/db.py index fed2871..41f3c93 100644 --- a/app/db.py +++ b/app/db.py @@ -39,6 +39,7 @@ def init_db(db_path: Path | None = None) -> None: client_name TEXT NOT NULL, start_time_local TEXT NOT NULL, timezone TEXT NOT NULL, + duration_minutes INTEGER NOT NULL DEFAULT 60, amount_usd REAL NOT NULL, status TEXT NOT NULL CHECK (status IN ('payment_pending', 'confirmed')), zoom_meeting_id TEXT, @@ -49,6 +50,11 @@ def init_db(db_path: Path | None = None) -> None: ) """ ) + cols = {row["name"] for row in conn.execute("PRAGMA table_info(appointments)").fetchall()} + if "duration_minutes" not in cols: + conn.execute( + "ALTER TABLE appointments ADD COLUMN duration_minutes INTEGER NOT NULL DEFAULT 60" + ) def _row_to_dict(row: sqlite3.Row | None) -> dict | None: @@ -60,6 +66,7 @@ def create_appointment( start_time_local: str, timezone_name: str, amount_usd: float, + duration_minutes: int = 60, db_path: Path | None = None, ) -> dict: """Insert a new appointment in payment_pending state.""" @@ -72,9 +79,9 @@ def create_appointment( """ INSERT INTO appointments ( session_id, payment_id, client_name, start_time_local, - timezone, amount_usd, status, created_at + timezone, duration_minutes, amount_usd, status, created_at ) - VALUES (?, ?, ?, ?, ?, ?, 'payment_pending', ?) + VALUES (?, ?, ?, ?, ?, ?, ?, 'payment_pending', ?) """, ( session_id, @@ -82,6 +89,7 @@ def create_appointment( client_name, start_time_local, timezone_name, + duration_minutes, amount_usd, created_at, ), diff --git a/app/schemas.py b/app/schemas.py index 28056fd..22f4c6d 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -57,6 +57,7 @@ class AppointmentResponse(BaseModel): client_name: str start_time_local: str timezone: str + duration_minutes: int = 60 amount_usd: float status: Literal["payment_pending", "confirmed"] zoom_meeting_id: str | None = None diff --git a/tests/test_app.py b/tests/test_app.py index 87bc52a..169e005 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -431,3 +431,56 @@ def test_db_init_sets_wal_once_not_on_every_connection(tmp_path: Path): with db.get_connection(db_file) as conn: mode_after = conn.execute("PRAGMA journal_mode;").fetchone()[0] assert mode_after.lower() == "delete" + + +def test_duration_minutes_persisted_and_forwarded_to_zoom(client, monkeypatch): + test_client, _ = client + captured_payloads = [] + + def fake_post(url, **kwargs): + if url == "https://zoom.us/oauth/token": + return FakeResponse(200, {"access_token": "mock_token"}) + if url == "https://api.zoom.us/v2/users/me/meetings": + captured_payloads.append(kwargs.get("json")) + return FakeResponse( + 201, + { + "id": 11223344, + "join_url": "https://zoom.us/j/11223344", + "start_url": "https://zoom.us/s/11223344?zak=xyz", + }, + ) + raise AssertionError(f"Unexpected POST URL: {url}") + + monkeypatch.setattr(zoom.requests, "post", fake_post) + monkeypatch.setattr( + zoom.requests, + "get", + lambda url, **kwargs: FakeResponse(200, {"meetings": []}), + ) + + c_resp = test_client.post( + "/api/appointments", + json={ + "credentials": SAMPLE_CREDS, + "client_name": "Ana López", + "start_time_local": "2026-11-25T15:00", + "duration_minutes": 45, + }, + ) + assert c_resp.status_code == 201 + appt = c_resp.get_json() + assert appt["duration_minutes"] == 45 + + g_resp = test_client.get(f"/api/appointments/{appt['session_id']}") + assert g_resp.status_code == 200 + assert g_resp.get_json()["duration_minutes"] == 45 + + p_resp = test_client.post( + f"/api/appointments/{appt['session_id']}/pay", + json={"credentials": SAMPLE_CREDS}, + ) + assert p_resp.status_code == 200 + assert p_resp.get_json()["duration_minutes"] == 45 + assert len(captured_payloads) == 1 + assert captured_payloads[0]["duration"] == 45