RecrAI-backend / storage.py
ViniciusKhan's picture
Add application file
4ced332
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