Spaces:
Runtime error
Runtime error
Download src/backend/services/firestore.py from HAK-Tech/ciro-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/spaces/HAK-Tech/ciro-backend/resolve/main/src/backend/services/firestore.py
- Command line
-
hf download hf://spaces/HAK-Tech/ciro-backend/src/backend/services/firestore.py
-
curl -L -o firestore.py https://huggingface.co/spaces/HAK-Tech/ciro-backend/resolve/main/src/backend/services/firestore.py
3.45 kB
| from typing import Dict, Any, List, Optional | |
| from fastapi import HTTPException | |
| from google.cloud.firestore import AsyncClient, SERVER_TIMESTAMP, ArrayUnion, FieldFilter | |
| from backend.core.firebase import get_async_db | |
| class FirestoreService: | |
| def db(self) -> AsyncClient: | |
| return get_async_db() | |
| async def get_user(self, cnic: str) -> Optional[Dict[str, Any]]: | |
| doc_ref = self.db.collection("users").document(cnic) | |
| doc = await doc_ref.get() | |
| if doc.exists: | |
| return doc.to_dict() | |
| return None | |
| async def create_user(self, data: Dict[str, Any]) -> Dict[str, Any]: | |
| cnic = data.get("cnic") | |
| if not cnic: | |
| raise ValueError("CNIC is required to create a user.") | |
| doc_ref = self.db.collection("users").document(cnic) | |
| await doc_ref.set(data) | |
| return data | |
| async def update_user(self, cnic: str, data: Dict[str, Any]) -> Dict[str, Any]: | |
| doc_ref = self.db.collection("users").document(cnic) | |
| doc = await doc_ref.get() | |
| if not doc.exists: | |
| raise HTTPException(status_code=404, detail="User not found") | |
| await doc_ref.update(data) | |
| return {"cnic": cnic, **data} | |
| async def create_crisis(self, data: Dict[str, Any]) -> Dict[str, Any]: | |
| crisis_id = data.get("crisis_id") | |
| if not crisis_id: | |
| _, doc_ref = await self.db.collection("crises").add(data) | |
| data["crisis_id"] = doc_ref.id | |
| await doc_ref.update({"crisis_id": doc_ref.id}) | |
| else: | |
| doc_ref = self.db.collection("crises").document(crisis_id) | |
| await doc_ref.set(data) | |
| return data | |
| async def get_crisis(self, crisis_id: str) -> Optional[Dict[str, Any]]: | |
| doc_ref = self.db.collection("crises").document(crisis_id) | |
| doc = await doc_ref.get() | |
| if doc.exists: | |
| return doc.to_dict() | |
| return None | |
| async def update_crisis(self, crisis_id: str, data: Dict[str, Any]) -> Dict[str, Any]: | |
| doc_ref = self.db.collection("crises").document(crisis_id) | |
| doc = await doc_ref.get() | |
| if not doc.exists: | |
| raise HTTPException(status_code=404, detail="Crisis not found") | |
| await doc_ref.update(data) | |
| return {"crisis_id": crisis_id, **data} | |
| async def get_ngos_for_area(self, area: str, crisis_type: str) -> List[Dict[str, Any]]: | |
| ngos_ref = self.db.collection("ngos") | |
| query = ( | |
| ngos_ref | |
| .where(filter=FieldFilter("coverage_areas", "array_contains", area)) | |
| .where(filter=FieldFilter("active", "==", True)) | |
| ) | |
| results = [] | |
| async for doc in query.stream(): | |
| data = doc.to_dict() | |
| if crisis_type in data.get("crisis_types", []): | |
| results.append({"ngo_id": doc.id, **data}) | |
| return results | |
| async def append_agent_log(self, crisis_id: str, step: Dict[str, Any]) -> bool: | |
| log_ref = self.db.collection("agent_logs").document(crisis_id) | |
| doc = await log_ref.get() | |
| if not doc.exists: | |
| await log_ref.set({ | |
| "crisis_id": crisis_id, | |
| "steps": [step], | |
| "created_at": SERVER_TIMESTAMP | |
| }) | |
| else: | |
| await log_ref.update({ | |
| "steps": ArrayUnion([step]) | |
| }) | |
| return True | |
| firestore_service = FirestoreService() | |