Skip to content

Commit ab1366b

Browse files
committed
solves review comments
1 parent 08a0708 commit ab1366b

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

radioshaq/radioshaq/api/routes/emergency.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,16 @@ async def approve_emergency_event(
179179
if not emergency_messaging_allowed(region, getattr(config, "emergency_contact", None)):
180180
raise HTTPException(status_code=403, detail="Emergency SMS/WhatsApp not allowed in this region")
181181
db = getattr(request.app.state, "db", None)
182-
if db is None or not hasattr(db, "claim_emergency_event_pending") or not hasattr(db, "get_coordination_event_by_id") or not hasattr(db, "update_coordination_event"):
182+
if db is None or not hasattr(db, "claim_emergency_event_pending") or not hasattr(db, "update_coordination_event"):
183+
raise HTTPException(status_code=503, detail="Database not available")
184+
if not (hasattr(db, "get_coordination_event_by_id_raw") or hasattr(db, "get_coordination_event_by_id")):
183185
raise HTTPException(status_code=503, detail="Database not available")
186+
get_event = getattr(db, "get_coordination_event_by_id_raw", db.get_coordination_event_by_id)
184187
# Atomic claim: only one concurrent approval can transition pending -> approving
185188
claimed = await db.claim_emergency_event_pending(event_id)
186189
if claimed is None:
187190
raise HTTPException(status_code=400, detail="Event already processed")
188-
event = await db.get_coordination_event_by_id(event_id)
191+
event = await get_event(event_id)
189192
if not event or event.get("event_type") != "emergency":
190193
await db.update_coordination_event(event_id, status="pending") # roll back claim
191194
raise HTTPException(status_code=400, detail="Not an emergency event")
@@ -253,7 +256,13 @@ async def reject_emergency_event(
253256
raise HTTPException(status_code=400, detail="Not an emergency event")
254257
rejector = getattr(_user, "sub", None) or getattr(_user, "callsign", "api")
255258
now = datetime.now(timezone.utc).isoformat()
256-
extra = event.get("extra_data") or {}
257-
extra.update({"rejected_at": now, "rejected_by": str(rejector), **({"notes": body.notes} if body.notes else {})})
258-
await db.update_coordination_event(event_id, status="rejected", extra_data=extra)
259+
await db.update_coordination_event(
260+
event_id,
261+
status="rejected",
262+
extra_data={
263+
"rejected_at": now,
264+
"rejected_by": str(rejector),
265+
**({"notes": body.notes} if body.notes else {}),
266+
},
267+
)
259268
return {"ok": True, "event_id": event_id, "status": "rejected"}

radioshaq/radioshaq/database/postgres_gis.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,34 @@ async def store_coordination_event(
513513
return event.id
514514

515515
async def get_coordination_event_by_id(self, event_id: int) -> dict[str, Any] | None:
516-
"""Get a single coordination event by id. Returns None if not found."""
516+
"""Get a single coordination event by id. Returns None if not found. Uses to_dict() (redacted for API)."""
517517
async with self.async_session() as session:
518518
result = await session.execute(select(CoordinationEvent).where(CoordinationEvent.id == event_id))
519519
row = result.scalar_one_or_none()
520520
return row.to_dict() if row else None
521521

522+
async def get_coordination_event_by_id_raw(self, event_id: int) -> dict[str, Any] | None:
523+
"""Get a coordination event with unredacted extra_data (for internal use e.g. approve handler)."""
524+
async with self.async_session() as session:
525+
result = await session.execute(select(CoordinationEvent).where(CoordinationEvent.id == event_id))
526+
row = result.scalar_one_or_none()
527+
if not row:
528+
return None
529+
return {
530+
"id": row.id,
531+
"event_type": row.event_type,
532+
"initiator_callsign": row.initiator_callsign,
533+
"target_callsign": row.target_callsign,
534+
"scheduled_time": row.scheduled_time.isoformat() if row.scheduled_time else None,
535+
"frequency_hz": row.frequency_hz,
536+
"mode": row.mode,
537+
"status": row.status,
538+
"priority": row.priority,
539+
"notes": row.notes,
540+
"created_at": row.created_at.isoformat() if row.created_at else None,
541+
"extra_data": dict(row.extra_data) if row.extra_data else {},
542+
}
543+
522544
async def update_coordination_event(
523545
self,
524546
event_id: int,

0 commit comments

Comments
 (0)