|
|
@@ -0,0 +1,87 @@
|
|
|
+import { atualizaTokenIxcIntegradorERetorna } from "../functions/atualizaTokenIxcIntegradorERetorna.js";
|
|
|
+
|
|
|
+function parseOutros(outros) {
|
|
|
+ if (!outros) return {};
|
|
|
+ return typeof outros === "string" ? JSON.parse(outros) : outros;
|
|
|
+}
|
|
|
+
|
|
|
+function baseUrlComBarra(url) {
|
|
|
+ return url.endsWith("/") ? url : `${url}/`;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+async function fetchJson(path, { method = "GET", body } = {}) {
|
|
|
+ const integracao = await atualizaTokenIxcIntegradorERetorna();
|
|
|
+ const { token } = parseOutros(integracao.Outros);
|
|
|
+ const baseUrl = baseUrlComBarra(integracao.Url);
|
|
|
+
|
|
|
+ const res = await fetch(`${baseUrl}${path}`, {
|
|
|
+ method,
|
|
|
+ headers: {
|
|
|
+ Authorization: `Bearer ${token}`,
|
|
|
+ ...(body ? { "Content-Type": "application/json" } : {})
|
|
|
+ },
|
|
|
+ body: body ? JSON.stringify(body) : undefined,
|
|
|
+ signal: AbortSignal.timeout(15_000)
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!res.ok) {
|
|
|
+ const err = new Error(`ixc_integrador_fetch_error:${res.status}`);
|
|
|
+ err.statusCode = 502;
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return await res.json();
|
|
|
+ } catch {
|
|
|
+ const err = new Error("ixc_integrador_invalid_json");
|
|
|
+ err.statusCode = 502;
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarClientes(busca) {
|
|
|
+ const json = await fetchJson("cliente/", { method: "POST", body: { busca } });
|
|
|
+ return json.clientes ?? [];
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarClienteCompleto(idClienteIxc) {
|
|
|
+ const json = await fetchJson(`cliente/dados/${idClienteIxc}`, { method: "POST" });
|
|
|
+ return json.cliente ?? null;
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarClienteBase(idClienteIxc) {
|
|
|
+ const json = await fetchJson(`cliente/dados/${idClienteIxc}/base`, { method: "POST" });
|
|
|
+ return json.cliente ?? null;
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarContratosCliente(idClienteIxc) {
|
|
|
+ return fetchJson(`contrato/${idClienteIxc}`);
|
|
|
+}
|
|
|
+
|
|
|
+export async function possuiContratoTelefonia(idClienteIxc) {
|
|
|
+ return fetchJson(`contrato/possui-contrato-telefonia/${idClienteIxc}`);
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarDetalhesContrato(idContratoIxc, idClienteIxc) {
|
|
|
+ return fetchJson(`contrato/detalhes/${idContratoIxc}?cliente_id=${idClienteIxc}`);
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarOsAvancado(filtros) {
|
|
|
+ return fetchJson("os/busca-avancada", { method: "POST", body: filtros });
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarOsPorId(id) {
|
|
|
+ const json = await fetchJson(`os/${id}`);
|
|
|
+ return json.os ?? null;
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarAtendimentoOsPorId(id) {
|
|
|
+ const json = await fetchJson(`os/atendimento/${id}`);
|
|
|
+ return json.os ?? null;
|
|
|
+}
|
|
|
+
|
|
|
+export async function buscarAssuntosOs() {
|
|
|
+ const json = await fetchJson("os/assuntos");
|
|
|
+ return json.assuntos ?? [];
|
|
|
+}
|