Download app/crud/reserva.py from Modulo-6/api: direct link, hf CLI and curl.
- Browser
- Download file 7.19 kB
-
https://huggingface.co/spaces/Modulo-6/api/resolve/main/app/crud/reserva.py
- Command line
-
hf download hf://spaces/Modulo-6/api/app/crud/reserva.py
-
curl -L -o reserva.py https://huggingface.co/spaces/Modulo-6/api/resolve/main/app/crud/reserva.py
7.19 kB
| """ | |
| Operaciones de acceso a datos para ReservaQuirofano. Sin lógica de negocio ni HTTPException. | |
| """ | |
| from __future__ import annotations | |
| from datetime import datetime, timezone | |
| from supabase import Client | |
| from app.models.reserva import ReservaQuirofano | |
| from app.schemas.reserva import ReservaQuirofanoCreateRequest, CirugiaUrgenteWebhook | |
| _TABLE = "reservas_quirofano" | |
| def get_by_id(db: Client, reserva_id: int) -> ReservaQuirofano | None: | |
| resp = db.table(_TABLE).select("*").eq("id", reserva_id).execute() | |
| return ReservaQuirofano.from_dict(resp.data[0]) if resp.data else None | |
| def get_by_cama_and_id(db: Client, cama_id: int, reserva_id: int) -> ReservaQuirofano | None: | |
| resp = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("id", reserva_id) | |
| .eq("cama_id", cama_id) | |
| .execute() | |
| ) | |
| return ReservaQuirofano.from_dict(resp.data[0]) if resp.data else None | |
| def get_by_turno(db: Client, turno_id: int) -> ReservaQuirofano | None: | |
| resp = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("turno_id", turno_id) | |
| .eq("origen", "PROGRAMADA") | |
| .execute() | |
| ) | |
| return ReservaQuirofano.from_dict(resp.data[0]) if resp.data else None | |
| def get_by_urgencia_hce_id(db: Client, hce_id: str) -> ReservaQuirofano | None: | |
| resp = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("solicitud_urgencia_hce_id", hce_id) | |
| .execute() | |
| ) | |
| return ReservaQuirofano.from_dict(resp.data[0]) if resp.data else None | |
| def get_all( | |
| db: Client, | |
| cama_id: int, | |
| estado: str | None = None, | |
| prioridad: str | None = None, | |
| fecha_desde: str | None = None, | |
| fecha_hasta: str | None = None, | |
| ) -> list[ReservaQuirofano]: | |
| q = db.table(_TABLE).select("*").eq("cama_id", cama_id) | |
| if estado: | |
| q = q.eq("estado", estado) | |
| else: | |
| q = q.in_("estado", ["RESERVADA", "EN_CURSO"]) | |
| if prioridad: | |
| q = q.eq("prioridad", prioridad) | |
| if fecha_desde: | |
| q = q.gte("fecha_hora_inicio", fecha_desde) | |
| if fecha_hasta: | |
| q = q.lte("fecha_hora_fin_estimada", fecha_hasta) | |
| resp = q.execute() | |
| return [ReservaQuirofano.from_dict(row) for row in resp.data] | |
| def get_superpuestas( | |
| db: Client, | |
| cama_id: int, | |
| inicio: datetime, | |
| fin: datetime, | |
| exclude_id: int | None = None, | |
| ) -> list[ReservaQuirofano]: | |
| q = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("cama_id", cama_id) | |
| .in_("estado", ["RESERVADA", "EN_CURSO"]) | |
| .lt("fecha_hora_inicio", fin.isoformat()) | |
| .gt("fecha_hora_fin_estimada", inicio.isoformat()) | |
| ) | |
| if exclude_id: | |
| q = q.neq("id", exclude_id) | |
| resp = q.execute() | |
| return [ReservaQuirofano.from_dict(row) for row in resp.data] | |
| def get_futuras(db: Client, cama_id: int, desde: datetime) -> list[ReservaQuirofano]: | |
| """Reservas activas (RESERVADA/EN_CURSO) del quirófano que todavía no terminaron | |
| en `desde`, ordenadas por inicio. Base para calcular el inicio más temprano factible | |
| de una cirugía urgente (algoritmo ASAP, ver cama_service.crear_reserva_urgente).""" | |
| resp = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("cama_id", cama_id) | |
| .in_("estado", ["RESERVADA", "EN_CURSO"]) | |
| .gt("fecha_hora_fin_estimada", desde.isoformat()) | |
| .order("fecha_hora_inicio") | |
| .execute() | |
| ) | |
| return [ReservaQuirofano.from_dict(row) for row in resp.data] | |
| def get_pendientes_de_inicio(db: Client, ahora: datetime) -> list[ReservaQuirofano]: | |
| """Reservas `RESERVADA` cuyo horario de inicio ya llegó — candidatas a pasar a | |
| `EN_CURSO` automáticamente. Una reserva `CANCELADA` nunca aparece acá: el filtro por | |
| estado ya resuelve "si no se canceló, arranca sola" sin lógica adicional.""" | |
| resp = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("estado", "RESERVADA") | |
| .lte("fecha_hora_inicio", ahora.isoformat()) | |
| .execute() | |
| ) | |
| return [ReservaQuirofano.from_dict(row) for row in resp.data] | |
| def get_pendientes_de_fin(db: Client, ahora: datetime) -> list[ReservaQuirofano]: | |
| """Reservas `EN_CURSO` cuyo horario de fin estimado ya llegó — candidatas a pasar a | |
| `COMPLETADA` automáticamente.""" | |
| resp = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("estado", "EN_CURSO") | |
| .lte("fecha_hora_fin_estimada", ahora.isoformat()) | |
| .execute() | |
| ) | |
| return [ReservaQuirofano.from_dict(row) for row in resp.data] | |
| def get_activa_por_turno(db: Client, turno_id: int) -> ReservaQuirofano | None: | |
| resp = ( | |
| db.table(_TABLE) | |
| .select("*") | |
| .eq("turno_id", turno_id) | |
| .in_("estado", ["RESERVADA", "EN_CURSO"]) | |
| .limit(1) | |
| .execute() | |
| ) | |
| return ReservaQuirofano.from_dict(resp.data[0]) if resp.data else None | |
| def create_programada(db: Client, cama_id: int, data: ReservaQuirofanoCreateRequest) -> ReservaQuirofano: | |
| row = { | |
| "cama_id": cama_id, | |
| "turno_id": data.turno_id, | |
| "origen": "PROGRAMADA", | |
| "estado": "RESERVADA", | |
| "prioridad": data.prioridad.value, | |
| "paciente_id": data.paciente_id, | |
| "medico_cirujano_id": data.medico_cirujano_id, | |
| "fecha_hora_inicio": data.fecha_hora_inicio.isoformat(), | |
| "fecha_hora_fin_estimada": data.fecha_hora_fin_estimada.isoformat(), | |
| "observaciones": data.observaciones, | |
| "created_at": datetime.now(timezone.utc).isoformat(), | |
| } | |
| resp = db.table(_TABLE).insert(row).execute() | |
| return ReservaQuirofano.from_dict(resp.data[0]) | |
| def create_urgente(db: Client, cama_id: int, data: CirugiaUrgenteWebhook) -> ReservaQuirofano: | |
| row = { | |
| "cama_id": cama_id, | |
| "solicitud_urgencia_hce_id": data.solicitud_urgencia_hce_id, | |
| "origen": "EMERGENCIA", | |
| "estado": "RESERVADA", | |
| "prioridad": "URGENTE", | |
| "paciente_id": data.paciente_id, | |
| "medico_cirujano_id": data.medico_cirujano_id, | |
| "fecha_hora_inicio": data.fecha_hora_inicio.isoformat(), | |
| "fecha_hora_fin_estimada": data.fecha_hora_fin_estimada.isoformat(), | |
| "observaciones": f"Cirugía urgente solicitada por HCE ({data.solicitud_urgencia_hce_id})", | |
| "created_at": datetime.now(timezone.utc).isoformat(), | |
| } | |
| resp = db.table(_TABLE).insert(row).execute() | |
| return ReservaQuirofano.from_dict(resp.data[0]) | |
| def save(db: Client, reserva: ReservaQuirofano) -> ReservaQuirofano: | |
| updates = { | |
| "estado": reserva.estado, | |
| "observaciones": reserva.observaciones, | |
| "desplazada_por_reserva_id": reserva.desplazada_por_reserva_id, | |
| # fecha_hora_inicio/fin también se persisten: una reserva reprogramada por | |
| # una urgencia (cama_service.reprogramar) cambia su ventana horaria y eso | |
| # tiene que quedar en la DB de M6, no solo notificarse a M2. | |
| "fecha_hora_inicio": reserva.fecha_hora_inicio.isoformat() if reserva.fecha_hora_inicio else None, | |
| "fecha_hora_fin_estimada": ( | |
| reserva.fecha_hora_fin_estimada.isoformat() if reserva.fecha_hora_fin_estimada else None | |
| ), | |
| } | |
| db.table(_TABLE).update(updates).eq("id", reserva.id).execute() | |
| return reserva | |