Spaces:
Runtime error
Runtime error
Create db_service.py
Browse files- services/db_service.py +92 -0
services/db_service.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
from datetime import datetime, date
|
| 4 |
+
from typing import Dict, Any, Optional
|
| 5 |
+
|
| 6 |
+
from supabase import create_client, Client
|
| 7 |
+
from config.settings import get_settings
|
| 8 |
+
|
| 9 |
+
_supabase_client = None
|
| 10 |
+
|
| 11 |
+
def get_supabase_client() -> Client:
|
| 12 |
+
|
| 13 |
+
global _supabase_client
|
| 14 |
+
|
| 15 |
+
if _supabase_client is None:
|
| 16 |
+
supabase_url = os.getenv("SUPABASE_URL")
|
| 17 |
+
supabase_key = os.getenv("SUPABASE_KEY")
|
| 18 |
+
|
| 19 |
+
if not supabase_url or not supabase_key:
|
| 20 |
+
raise ValueError("SUPABASE_URL and SUPABASE_KEY environment variables must be set")
|
| 21 |
+
|
| 22 |
+
_supabase_client = create_client(supabase_url, supabase_key)
|
| 23 |
+
|
| 24 |
+
return _supabase_client
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def format_candidate_data(resume_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 28 |
+
db_record = {
|
| 29 |
+
"id": str(uuid.uuid4()),
|
| 30 |
+
"created_at": datetime.now().isoformat(),
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
direct_fields = [
|
| 34 |
+
"candidate_name", "gender", "email", "address",
|
| 35 |
+
"pan_number", "aadhar_number", "highest_education",
|
| 36 |
+
"designation", "skills", "region", "relevant_experience",
|
| 37 |
+
"experience_summary", "mobile_number"
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
for field in direct_fields:
|
| 41 |
+
db_record[field] = resume_data.get(field)
|
| 42 |
+
|
| 43 |
+
dob = resume_data.get("date_of_birth")
|
| 44 |
+
if dob:
|
| 45 |
+
try:
|
| 46 |
+
if '/' in dob:
|
| 47 |
+
year, month, day = dob.split('/')
|
| 48 |
+
db_record["date_of_birth"] = date(int(year), int(month), int(day)).isoformat()
|
| 49 |
+
elif '-' in dob:
|
| 50 |
+
db_record["date_of_birth"] = dob
|
| 51 |
+
else:
|
| 52 |
+
db_record["date_of_birth"] = None
|
| 53 |
+
except (ValueError, TypeError):
|
| 54 |
+
db_record["date_of_birth"] = None
|
| 55 |
+
else:
|
| 56 |
+
db_record["date_of_birth"] = None
|
| 57 |
+
|
| 58 |
+
experience = resume_data.get("total_experience")
|
| 59 |
+
if experience:
|
| 60 |
+
try:
|
| 61 |
+
db_record["total_experience"] = float(experience)
|
| 62 |
+
except (ValueError, TypeError):
|
| 63 |
+
db_record["total_experience"] = None
|
| 64 |
+
else:
|
| 65 |
+
db_record["total_experience"] = None
|
| 66 |
+
|
| 67 |
+
file_date = resume_data.get("file_modified_date")
|
| 68 |
+
if isinstance(file_date, datetime):
|
| 69 |
+
db_record["file_modified_date"] = file_date.isoformat()
|
| 70 |
+
else:
|
| 71 |
+
db_record["file_modified_date"] = str(file_date) if file_date else None
|
| 72 |
+
|
| 73 |
+
return db_record
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
async def store_candidate_data(resume_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
supabase = get_supabase_client()
|
| 80 |
+
|
| 81 |
+
db_record = format_candidate_data(resume_data)
|
| 82 |
+
|
| 83 |
+
response = supabase.table("candidates").insert(db_record).execute()
|
| 84 |
+
|
| 85 |
+
if hasattr(response, 'error') and response.error:
|
| 86 |
+
raise Exception(f"Database error: {response.error}")
|
| 87 |
+
|
| 88 |
+
return db_record
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"Error storing candidate data: {str(e)}")
|
| 92 |
+
raise
|