File size: 2,992 Bytes
4ced332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
import os
import json
import uuid
from typing import List, Dict, Any, Optional
from datetime import datetime

DATA_DIR = os.path.join(os.getcwd(), "data")
JOBS_PATH = os.path.join(DATA_DIR, "jobs.json")
CVS_PATH  = os.path.join(DATA_DIR, "cvs.json")

def _ensure_data_dir():
    os.makedirs(DATA_DIR, exist_ok=True)

def _read_json(path: str) -> Any:
    _ensure_data_dir()
    if not os.path.exists(path):
        # inicializa vazios
        return [] if path.endswith(".json") else None
    with open(path, "r", encoding="utf-8") as f:
        try:
            return json.load(f)
        except Exception:
            return []

def _write_json(path: str, data: Any):
    _ensure_data_dir()
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, indent=2, ensure_ascii=False)

# ---------------- Jobs ----------------

def list_jobs() -> List[Dict[str, Any]]:
    data = _read_json(JOBS_PATH)
    if isinstance(data, dict):
        data = [data]
    return data or []

def create_job(title: str, description: str, details: str, requirements: Optional[list]=None) -> Dict[str, Any]:
    jobs = list_jobs()
    job_id = str(uuid.uuid4())
    rec = {
        "id": job_id,
        "title": title,
        "description": description,
        "details": details,
        "requirements": requirements or [],
        "created_at": datetime.utcnow().isoformat()
    }
    jobs.append(rec)
    _write_json(JOBS_PATH, jobs)
    return rec

def get_job(job_id: str) -> Optional[Dict[str, Any]]:
    for j in list_jobs():
        if j.get("id") == job_id:
            return j
    return None

# ---------------- CVs ----------------

def list_cvs() -> List[Dict[str, Any]]:
    data = _read_json(CVS_PATH)
    if isinstance(data, dict):
        data = [data]
    return data or []

def save_cv_result(result: Dict[str, Any], job: Optional[Dict[str, Any]]=None) -> Dict[str, Any]:
    cvs = list_cvs()
    rec_id = str(uuid.uuid4())

    rec = {
        "id": rec_id,
        "name": result.get("name") or "",
        "area": result.get("area") or "",
        "summary": result.get("summary") or "",
        "skills": result.get("skills") or [],
        "education": result.get("education") or "",
        "interview_questions": result.get("interview_questions") or [],
        "strengths": result.get("strengths") or [],
        "areas_for_development": result.get("areas_for_development") or [],
        "important_considerations": result.get("important_considerations") or [],
        "final_recommendations": result.get("final_recommendations") or "",
        "score": result.get("score") or 0.0,
        "created_at": datetime.utcnow().isoformat(),
        "job_id": job.get("id") if job else None,
        "job_title": job.get("title") if job else None
    }

    cvs.append(rec)
    _write_json(CVS_PATH, cvs)
    return rec

def get_cv(cv_id: str) -> Optional[Dict[str, Any]]:
    for c in list_cvs():
        if c.get("id") == cv_id:
            return c
    return None