File size: 2,824 Bytes
84779a6
 
 
90cd03f
84779a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7b24b5
 
84779a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90cd03f
84779a6
90cd03f
84779a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import uuid
from datetime import datetime, date
from typing import Dict, Any

from supabase import create_client, Client

_supabase_client = None

def get_supabase_client() -> Client:
   
    global _supabase_client
    
    if _supabase_client is None:
        supabase_url = os.getenv("SUPABASE_URL")
        supabase_key = os.getenv("SUPABASE_KEY")
        
        if not supabase_url or not supabase_key:
            raise ValueError("SUPABASE_URL and SUPABASE_KEY environment variables must be set")
        
        _supabase_client = create_client(supabase_url, supabase_key)
    
    return _supabase_client


def format_candidate_data(resume_data: Dict[str, Any]) -> Dict[str, Any]:
    db_record = {
        "id": str(uuid.uuid4()),
        "created_at": datetime.now().isoformat(),
    }
    
    direct_fields = [
        "candidate_name", "gender", "email", "address", 
        "pan_number", "aadhar_number", "highest_education",
        "university_name", "designation", "skills", "region", 
        "relevant_experience", "experience_summary", "mobile_number"
    ]
    
    for field in direct_fields:
        db_record[field] = resume_data.get(field)
    
    dob = resume_data.get("date_of_birth")
    if dob:
        try:
            if '/' in dob:
                year, month, day = dob.split('/')
                db_record["date_of_birth"] = date(int(year), int(month), int(day)).isoformat()
            elif '-' in dob:
                db_record["date_of_birth"] = dob
            else:
                db_record["date_of_birth"] = None
        except (ValueError, TypeError):
            db_record["date_of_birth"] = None
    else:
        db_record["date_of_birth"] = None
    
    experience = resume_data.get("total_experience")
    if experience:
        try:
            db_record["total_experience"] = float(experience)
        except (ValueError, TypeError):
            db_record["total_experience"] = None
    else:
        db_record["total_experience"] = None
    
    file_date = resume_data.get("file_modified_date")
    if isinstance(file_date, datetime):
        db_record["file_modified_date"] = file_date.strftime("%Y-%m-%dT%H:%M:%S")
    else:
        db_record["file_modified_date"] = None
    
    return db_record


async def store_candidate_data(resume_data: Dict[str, Any]) -> Dict[str, Any]:
    try:
        supabase = get_supabase_client()
        
        db_record = format_candidate_data(resume_data)
        
        response = supabase.table("candidates").insert(db_record).execute()
        
        if hasattr(response, 'error') and response.error:
            raise Exception(f"Database error: {response.error}")
            
        return db_record
        
    except Exception as e:
        print(f"Error storing candidate data: {str(e)}")
        raise