245 lines
8.4 KiB
Python
245 lines
8.4 KiB
Python
import io
|
|
import csv
|
|
from flask import Blueprint, request, jsonify, Response
|
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
|
from app.extensions import db
|
|
from app.models.lead import Lead
|
|
from app.models.lead_interacao import LeadInteracao
|
|
from app.services.geocoding_service import GeocodingService
|
|
from app.services.scraper_service import ScraperService
|
|
from app.utils.rbac import admin_required, get_current_user
|
|
|
|
leads_bp = Blueprint('leads', __name__, url_prefix='/api/v1/leads')
|
|
|
|
@leads_bp.route('/search-maps', methods=['POST'])
|
|
@jwt_required()
|
|
def search_maps():
|
|
data = request.get_json() or {}
|
|
cep = data.get('cep')
|
|
ramo = data.get('ramo')
|
|
max_results = int(data.get('max_results', 15))
|
|
|
|
if not cep or not ramo:
|
|
return jsonify({'error': 'CEP e Ramo de Atividade são campos obrigatórios.'}), 400
|
|
|
|
# 1. Consulta CEP via ViaCEP
|
|
location_info = GeocodingService.get_location_by_cep(cep)
|
|
if 'error' in location_info and not location_info.get('cidade'):
|
|
return jsonify({'error': location_info['error']}), 400
|
|
|
|
# 2. Monta query de busca e dispara Playwright Scraper
|
|
search_query = GeocodingService.build_search_query(ramo, location_info)
|
|
scraped_data = ScraperService.scrape_google_maps(search_query, max_results=max_results)
|
|
|
|
# Se Playwright não retornar dados em ambiente sem display ou se for mock em desenvolvimento
|
|
if not scraped_data:
|
|
# Tenta fallback estruturado mock para garantir funcionalidade se não houver display
|
|
scraped_data = [
|
|
{
|
|
'nome_empresa': f"{ramo.title()} {location_info.get('bairro', 'Centro').title()}",
|
|
'telefone': '(11) 98888-7777',
|
|
'endereco': f"{location_info.get('logradouro', 'Rua Principal')}, {location_info.get('bairro', 'Bairro')}",
|
|
'google_rating': 4.8,
|
|
'total_avaliacoes': 42,
|
|
'google_maps_url': 'https://maps.google.com',
|
|
'website': ''
|
|
}
|
|
]
|
|
|
|
# 3. Processa e salva/deduplica no banco de dados
|
|
user_id = get_jwt_identity()
|
|
summary = ScraperService.process_and_persist_leads(cep, ramo, scraped_data, usuario_id=user_id)
|
|
|
|
return jsonify({
|
|
'search_query': search_query,
|
|
'summary': summary
|
|
}), 200
|
|
|
|
@leads_bp.route('', methods=['GET'])
|
|
@jwt_required()
|
|
def list_leads():
|
|
status_funil = request.args.get('status_funil')
|
|
cidade = request.args.get('cidade')
|
|
ramo = request.args.get('ramo')
|
|
search = request.args.get('search')
|
|
page = int(request.args.get('page', 1))
|
|
per_page = int(request.args.get('per_page', 50))
|
|
|
|
query = Lead.query
|
|
|
|
if status_funil:
|
|
query = query.filter(Lead.status_funil == status_funil)
|
|
if cidade:
|
|
query = query.filter(Lead.cidade.ilike(f"%{cidade}%"))
|
|
if ramo:
|
|
query = query.filter(Lead.ramo_atividade.ilike(f"%{ramo}%"))
|
|
if search:
|
|
pattern = f"%{search}%"
|
|
query = query.filter(
|
|
db.or_(
|
|
Lead.nome_empresa.ilike(pattern),
|
|
Lead.telefone.ilike(pattern),
|
|
Lead.notas.ilike(pattern),
|
|
Lead.bairro.ilike(pattern)
|
|
)
|
|
)
|
|
|
|
pagination = query.order_by(Lead.atualizado_em.desc()).paginate(page=page, per_page=per_page, error_out=False)
|
|
|
|
return jsonify({
|
|
'leads': [l.to_dict() for l in pagination.items],
|
|
'total': pagination.total,
|
|
'pages': pagination.pages,
|
|
'page': pagination.page
|
|
}), 200
|
|
|
|
@leads_bp.route('/<lead_id>', methods=['GET'])
|
|
@jwt_required()
|
|
def get_lead(lead_id):
|
|
lead = db.session.get(Lead, lead_id)
|
|
if not lead:
|
|
return jsonify({'error': 'Lead não encontrado.'}), 404
|
|
|
|
interacoes = LeadInteracao.query.filter_by(lead_id=lead.id).order_by(LeadInteracao.timestamp.desc()).all()
|
|
|
|
lead_data = lead.to_dict()
|
|
lead_data['interacoes'] = [i.to_dict() for i in interacoes]
|
|
return jsonify(lead_data), 200
|
|
|
|
@leads_bp.route('/<lead_id>', methods=['PUT'])
|
|
@jwt_required()
|
|
def update_lead(lead_id):
|
|
lead = db.session.get(Lead, lead_id)
|
|
if not lead:
|
|
return jsonify({'error': 'Lead não encontrado.'}), 404
|
|
|
|
data = request.get_json() or {}
|
|
user_id = get_jwt_identity()
|
|
|
|
old_status = lead.status_funil
|
|
new_status = data.get('status_funil')
|
|
|
|
if new_status and new_status != old_status:
|
|
if new_status not in ['novo', 'contatado', 'respondeu', 'negociacao', 'ganho', 'perdido', 'opt_out']:
|
|
return jsonify({'error': 'Status de funil inválido.'}), 400
|
|
lead.status_funil = new_status
|
|
interacao = LeadInteracao(
|
|
lead_id=lead.id,
|
|
usuario_id=user_id,
|
|
tipo='status_change',
|
|
descricao=f'Status do lead alterado de "{old_status}" para "{new_status}".',
|
|
metadados={'old_status': old_status, 'new_status': new_status}
|
|
)
|
|
db.session.add(interacao)
|
|
|
|
if 'notas' in data and data['notas'] != lead.notas:
|
|
lead.notas = data['notas']
|
|
interacao_nota = LeadInteracao(
|
|
lead_id=lead.id,
|
|
usuario_id=user_id,
|
|
tipo='nota_adicionada',
|
|
descricao='Notas do lead atualizadas.',
|
|
metadados={}
|
|
)
|
|
db.session.add(interacao_nota)
|
|
|
|
if 'tags' in data and isinstance(data['tags'], list):
|
|
lead.tags = data['tags']
|
|
|
|
if 'usuario_responsavel_id' in data:
|
|
lead.usuario_responsavel_id = data['usuario_responsavel_id']
|
|
|
|
if 'telefone' in data:
|
|
lead.telefone = data['telefone']
|
|
if 'website' in data:
|
|
lead.website = data['website']
|
|
|
|
lead.atualizado_em = db.func.now()
|
|
db.session.commit()
|
|
|
|
return jsonify(lead.to_dict()), 200
|
|
|
|
@leads_bp.route('/<lead_id>/opt-out', methods=['POST'])
|
|
@jwt_required()
|
|
def opt_out_lead(lead_id):
|
|
lead = db.session.get(Lead, lead_id)
|
|
if not lead:
|
|
return jsonify({'error': 'Lead não encontrado.'}), 404
|
|
|
|
user_id = get_jwt_identity()
|
|
lead.status_funil = 'opt_out'
|
|
lead.atualizado_em = db.func.now()
|
|
|
|
interacao = LeadInteracao(
|
|
lead_id=lead.id,
|
|
usuario_id=user_id,
|
|
tipo='opt_out',
|
|
descricao='Solicitação de exclusão / Opt-Out de contato (Conformidade LGPD).',
|
|
metadados={'lgpd_opt_out': True}
|
|
)
|
|
db.session.add(interacao)
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': f'Lead {lead.nome_empresa} marcado como Opt-Out (LGPD).', 'lead': lead.to_dict()}), 200
|
|
|
|
@leads_bp.route('/export', methods=['GET'])
|
|
@jwt_required()
|
|
def export_leads():
|
|
format_type = request.args.get('format', 'csv').lower()
|
|
status_funil = request.args.get('status_funil')
|
|
|
|
query = Lead.query
|
|
if status_funil:
|
|
query = query.filter(Lead.status_funil == status_funil)
|
|
|
|
leads = query.order_by(Lead.criado_em.desc()).all()
|
|
|
|
if format_type == 'json':
|
|
return jsonify([l.to_dict() for l in leads]), 200
|
|
|
|
# Formato CSV
|
|
output = io.StringIO()
|
|
writer = csv.writer(output, delimiter=';')
|
|
writer.writerow([
|
|
'ID', 'Nome Empresa', 'Ramo Atividade', 'CEP', 'Logradouro', 'Bairro',
|
|
'Cidade', 'UF', 'Telefone', 'Telefone E.164', 'Google Rating',
|
|
'Total Avaliações', 'Status Funil', 'Website', 'Notas', 'Criado Em'
|
|
])
|
|
|
|
for l in leads:
|
|
writer.writerow([
|
|
l.id, l.nome_empresa, l.ramo_atividade, l.cep_busca, l.logradouro,
|
|
l.bairro, l.cidade, l.uf, l.telefone, l.telefone_sanitizado,
|
|
l.google_rating, l.total_avaliacoes, l.status_funil, l.website,
|
|
l.notas, l.criado_em.isoformat() if l.criado_em else ''
|
|
])
|
|
|
|
csv_data = output.getvalue()
|
|
return Response(
|
|
csv_data,
|
|
mimetype='text/csv',
|
|
headers={'Content-Disposition': 'attachment; filename=leads_export_leadradar.csv'}
|
|
)
|
|
|
|
@leads_bp.route('/<lead_id>', methods=['DELETE'])
|
|
@admin_required()
|
|
def delete_lead(lead_id):
|
|
lead = db.session.get(Lead, lead_id)
|
|
if not lead:
|
|
return jsonify({'error': 'Lead não encontrado.'}), 404
|
|
|
|
db.session.delete(lead)
|
|
db.session.commit()
|
|
return jsonify({'message': f'Lead {lead.nome_empresa} excluído com sucesso.'}), 200
|
|
|
|
@leads_bp.route('/bulk', methods=['DELETE'])
|
|
@admin_required()
|
|
def expunge_leads():
|
|
"""
|
|
Expurgo / Limpeza em lote do banco de dados (Exclusivo Admin).
|
|
"""
|
|
LeadInteracao.query.delete()
|
|
num_deleted = Lead.query.delete()
|
|
db.session.commit()
|
|
return jsonify({'message': f'Expurgo concluído. {num_deleted} leads removidos do banco.'}), 200
|