| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { apiFetch, apiBaseUrl, buildAuthHeaders, refreshAccessToken, notifyAuthError } from "./client.js";
- export function sincronizarAtendimentos(params = {}) {
- return apiFetch("/api/atendimentos/sync", { method: "POST", body: params });
- }
- export function sincronizarTodosAtendimentos(params = {}) {
- return apiFetch("/api/atendimentos/sync-all", { method: "POST", body: params });
- }
- export function obterAtendimento(id) {
- return apiFetch(`/api/atendimentos/${id}`);
- }
- export function sincronizarAtendimento(id) {
- return apiFetch(`/api/atendimentos/${id}/sync`, { method: "POST", body: {} });
- }
- export function avaliarAtendimentosPendentes(params = {}) {
- return apiFetch("/api/atendimentos/avaliar", { method: "POST", body: params });
- }
- export function avaliarAtendimento(id) {
- return apiFetch(`/api/atendimentos/${id}/avaliar`, { method: "POST", body: {} });
- }
- export function listarAvaliacoes({ page = 1, pageSize = 20, setor, resolvido, sentimento, statusCancelamento, scoreMax, busca, ordenacao, dataInicio, dataFim } = {}) {
- const query = new URLSearchParams({ page: String(page), pageSize: String(pageSize) });
- if (setor) query.set("setor", setor);
- if (resolvido) query.set("resolvido", resolvido);
- if (sentimento) query.set("sentimento", sentimento);
- if (statusCancelamento) query.set("statusCancelamento", statusCancelamento);
- if (scoreMax !== undefined) query.set("scoreMax", String(scoreMax));
- if (busca) query.set("busca", busca);
- if (ordenacao === "asc") query.set("ordenacao", ordenacao);
- if (dataInicio) query.set("dataInicio", dataInicio);
- if (dataFim) query.set("dataFim", dataFim);
- return apiFetch(`/api/atendimentos/avaliacoes?${query.toString()}`);
- }
- export function estatisticasAvaliacoes({ setor, resolvido, sentimento, statusCancelamento, busca, dataInicio, dataFim } = {}) {
- const query = new URLSearchParams();
- if (setor) query.set("setor", setor);
- if (resolvido) query.set("resolvido", resolvido);
- if (sentimento) query.set("sentimento", sentimento);
- if (statusCancelamento) query.set("statusCancelamento", statusCancelamento);
- if (busca) query.set("busca", busca);
- if (dataInicio) query.set("dataInicio", dataInicio);
- if (dataFim) query.set("dataFim", dataFim);
- const qs = query.toString();
- return apiFetch(`/api/atendimentos/avaliacoes/estatisticas${qs ? `?${qs}` : ""}`);
- }
- export function listarSetoresAvaliacoes() {
- return apiFetch("/api/atendimentos/avaliacoes/setores");
- }
- export function listarGoldenSet({ status, setor } = {}) {
- const query = new URLSearchParams();
- if (status) query.set("status", status);
- if (setor) query.set("setor", setor);
- const qs = query.toString();
- return apiFetch(`/api/atendimentos/golden-set${qs ? `?${qs}` : ""}`);
- }
- export function salvarGoldenLabel(id, dados) {
- return apiFetch(`/api/atendimentos/${id}/golden-label`, { method: "PATCH", body: dados });
- }
- export function aceitarGoldenLabelComoIa(id) {
- return apiFetch(`/api/atendimentos/${id}/golden-label/aceitar`, { method: "POST" });
- }
- export async function baixarRelatorioNaoResolvidos({ setor, sentimento, busca, ordenacao, dataInicio, dataFim } = {}) {
- const query = new URLSearchParams();
- if (setor) query.set("setor", setor);
- if (sentimento) query.set("sentimento", sentimento);
- if (busca) query.set("busca", busca);
- if (ordenacao === "asc") query.set("ordenacao", ordenacao);
- if (dataInicio) query.set("dataInicio", dataInicio);
- if (dataFim) query.set("dataFim", dataFim);
- const reqUrl = `${apiBaseUrl}/api/atendimentos/avaliacoes/relatorio?${query.toString()}`;
- let res;
- try {
- res = await fetch(reqUrl, { headers: buildAuthHeaders() });
- } catch {
- throw new Error("network_error");
- }
- if (res.status === 401) {
- try {
- await refreshAccessToken();
- } catch {
- notifyAuthError();
- throw new Error("session_expired");
- }
- try {
- res = await fetch(reqUrl, { headers: buildAuthHeaders() });
- } catch {
- throw new Error("network_error");
- }
- }
- if (res.status === 401) {
- notifyAuthError();
- throw new Error("session_expired");
- }
- if (!res.ok) {
- const text = await res.text().catch(() => "");
- let payload = null;
- try { payload = JSON.parse(text); } catch {}
- if (payload?.error === "relatorio_muito_grande") {
- throw new Error(`Muitos atendimentos para gerar o relatório (limite: ${payload.limite}). ${payload.dica ?? "Estreite o período ou os filtros."}`);
- }
- throw new Error(payload?.error || text || `http_error:${res.status}`);
- }
- const blob = await res.blob();
- const url = URL.createObjectURL(blob);
- const a = document.createElement("a");
- a.href = url;
- a.download = `atendimentos-nao-resolvidos-${new Date().toISOString().slice(0, 10)}.pdf`;
- document.body.appendChild(a);
- a.click();
- a.remove();
- URL.revokeObjectURL(url);
- }
|