You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
from typing import Optional, List, Dict, Any
|
|
import re
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from app.services.mockaroo_client import MockarooClient
|
|
from app.core.settings import settings
|
|
|
|
|
|
def normalize_cpf(value: str) -> str:
|
|
return re.sub(r"\D", "", value or "")
|
|
|
|
|
|
async def consultar_estoque(preco_max: float, categoria: Optional[str] = None) -> List[Dict[str, Any]]:
|
|
client = MockarooClient()
|
|
registros = await client.fetch_schema_data("consultar_estoque", count=200)
|
|
return [
|
|
r for r in registros
|
|
if float(r.get("preco", 0)) <= preco_max and (categoria is None or r.get("categoria") == categoria)
|
|
]
|
|
|
|
|
|
async def validar_cliente_venda(cpf: str, valor_veiculo: float) -> Dict[str, Any]:
|
|
client = MockarooClient()
|
|
registros = await client.fetch_schema_data("clientes_credito", count=500)
|
|
cpf_norm = normalize_cpf(cpf)
|
|
cliente = next((r for r in registros if normalize_cpf(r.get("cpf", "")) == cpf_norm), None)
|
|
if not cliente:
|
|
return {
|
|
"aprovado": False,
|
|
"motivo": "Cliente não encontrado",
|
|
"cpf": cpf_norm,
|
|
"valor_veiculo": valor_veiculo,
|
|
}
|
|
limite = float(cliente.get("limite_credito", 0))
|
|
restricao = bool(cliente.get("possui_restricao", False))
|
|
aprovado = (not restricao) and (valor_veiculo <= limite)
|
|
return {
|
|
"aprovado": aprovado,
|
|
"cpf": cpf_norm,
|
|
"nome": cliente.get("nome"),
|
|
"score": cliente.get("score"),
|
|
"limite_credito": limite,
|
|
"possui_restricao": restricao,
|
|
"valor_veiculo": valor_veiculo,
|
|
}
|
|
|
|
|
|
async def avaliar_veiculo_troca(modelo: str, ano: int, km: int) -> Dict[str, Any]:
|
|
ano_atual = datetime.now().year
|
|
idade = max(0, ano_atual - ano)
|
|
base = 80000.0
|
|
valor = base * (0.85 ** idade) - (km * 0.03)
|
|
valor = max(5000.0, valor)
|
|
return {
|
|
"modelo": modelo,
|
|
"ano": ano,
|
|
"km": km,
|
|
"valor_estimado_troca": round(valor, 2),
|
|
}
|
|
|
|
|
|
async def agendar_revisao(placa: str, data_hora: str) -> Dict[str, Any]:
|
|
if settings.use_mockaroo_writes:
|
|
client = MockarooClient()
|
|
try:
|
|
return await client.post_json(
|
|
"agendamentos_revisao",
|
|
{"placa": placa, "data_hora": data_hora, "status": "Agendado"},
|
|
)
|
|
except Exception:
|
|
pass
|
|
agendamento_id = str(uuid.uuid4())
|
|
return {"id": agendamento_id, "placa": placa, "data_hora": data_hora, "status": "Agendado"}
|
|
|
|
|
|
async def cancelar_pedido(numero_pedido: str, motivo: str) -> Dict[str, Any]:
|
|
client = MockarooClient()
|
|
registros = await client.fetch_schema_data("pedidos", count=500)
|
|
pedido = next((r for r in registros if str(r.get("numero_pedido")) == str(numero_pedido)), None)
|
|
if not pedido:
|
|
return {
|
|
"status": "NaoEncontrado",
|
|
"numero_pedido": numero_pedido,
|
|
"motivo": motivo,
|
|
}
|
|
if settings.use_mockaroo_writes:
|
|
try:
|
|
await client.delete_json("pedidos", {"numero_pedido": numero_pedido, "motivo": motivo})
|
|
except Exception:
|
|
pass
|
|
return {"status": "Cancelado", "numero_pedido": numero_pedido, "motivo": motivo, "pedido": pedido}
|