78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
from flask import Blueprint, request, jsonify, url_for
|
|
from typing import List
|
|
from modules.crm.search import filter_prospects
|
|
|
|
crm_api_bp = Blueprint("crm_api", __name__, url_prefix="/api/crm")
|
|
|
|
|
|
@crm_api_bp.get("/prospects/search")
|
|
def prospects_search():
|
|
"""
|
|
API de recherche/filtre prospects.
|
|
Paramètres (query string):
|
|
- q: recherche globale (nom, email, société, ville)
|
|
- name, email, company, status, city: filtres spécifiques (contient, sauf status: égal insensible à la casse)
|
|
- tags: liste séparée par des virgules (tous doivent être présents)
|
|
- date_from, date_to: période sur created_at (YYYY-MM-DD)
|
|
"""
|
|
q = request.args.get("q")
|
|
name = request.args.get("name")
|
|
email = request.args.get("email")
|
|
company = request.args.get("company")
|
|
status = request.args.get("status")
|
|
city = request.args.get("city")
|
|
tags_raw = request.args.get("tags") or ""
|
|
tags: List[str] = [t.strip() for t in tags_raw.split(",")] if tags_raw else []
|
|
date_from = request.args.get("date_from") or None
|
|
date_to = request.args.get("date_to") or None
|
|
|
|
results = filter_prospects(
|
|
q=q,
|
|
name=name,
|
|
email=email,
|
|
company=company,
|
|
status=status,
|
|
city=city,
|
|
tags=tags,
|
|
date_from=date_from,
|
|
date_to=date_to,
|
|
)
|
|
|
|
# Enrichit avec des URLs navigables
|
|
enriched = []
|
|
for it in results:
|
|
new_it = dict(it)
|
|
# URL principale (prospect)
|
|
try:
|
|
new_it["url"] = url_for("prospect_details", prospect_id=it.get("id"))
|
|
except Exception:
|
|
pass
|
|
|
|
related = []
|
|
cid = it.get("client_id")
|
|
if cid:
|
|
try:
|
|
related.append({"type": "client", "url": url_for("client_details", client_id=cid)})
|
|
except Exception:
|
|
pass
|
|
|
|
pids = it.get("project_ids") or []
|
|
for pid in pids:
|
|
try:
|
|
related.append({"type": "project", "url": url_for("project_details", project_id=pid)})
|
|
except Exception:
|
|
pass
|
|
|
|
if related:
|
|
new_it["related"] = related
|
|
|
|
# S'assure que le type est présent
|
|
new_it["entity_type"] = new_it.get("entity_type") or "prospect"
|
|
enriched.append(new_it)
|
|
|
|
return jsonify(
|
|
{
|
|
"count": len(enriched),
|
|
"results": enriched,
|
|
}
|
|
)
|