refactor: update CNPJ handling and API integration in empresas page
- Replaced the ReceitaWS API with BrasilAPI for fetching CNPJ data, improving reliability. - Updated response handling to accommodate new data structure from BrasilAPI. - Enhanced form population logic for company details based on the new API response. - Adjusted table layout to correctly display CNPJ alongside company names.
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
import { Building2, Phone, Mail, Plus, Users, Pencil, X } from "lucide-svelte";
|
||||
import { resolve } from "$app/paths";
|
||||
import { maskCNPJ, maskCEP, maskPhone, maskUF, onlyDigits } from "$lib/utils/masks";
|
||||
import "$lib/svelte-compat";
|
||||
|
||||
const client = useConvexClient();
|
||||
const empresasQuery = useQuery(api.empresas.list, {});
|
||||
@@ -76,13 +75,12 @@
|
||||
let carregandoCnpj = $state(false);
|
||||
let erroCnpj = $state("");
|
||||
|
||||
type ReceitaWsResponse = {
|
||||
status?: string;
|
||||
message?: string;
|
||||
nome?: string;
|
||||
fantasia?: string;
|
||||
telefone?: string;
|
||||
type BrasilApiCnpjResponse = {
|
||||
razao_social?: string;
|
||||
nome_fantasia?: string;
|
||||
email?: string;
|
||||
ddd_telefone_1?: string;
|
||||
telefone?: string;
|
||||
cep?: string;
|
||||
logradouro?: string;
|
||||
numero?: string;
|
||||
@@ -90,6 +88,7 @@
|
||||
bairro?: string;
|
||||
municipio?: string;
|
||||
uf?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
function handleEmpresaCnpjInput(event: Event) {
|
||||
@@ -105,21 +104,23 @@
|
||||
carregandoCnpj = true;
|
||||
erroCnpj = "";
|
||||
try {
|
||||
const response = await fetch(`https://www.receitaws.com.br/v1/cnpj/${digits}`);
|
||||
const data: ReceitaWsResponse = await response.json();
|
||||
const response = await fetch(`https://brasilapi.com.br/api/cnpj/v1/${digits}`);
|
||||
const data: BrasilApiCnpjResponse = await response.json();
|
||||
|
||||
if (data.status === "ERROR") {
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || "CNPJ não encontrado.");
|
||||
}
|
||||
|
||||
if (data.nome && !empresaForm.razao_social) {
|
||||
empresaForm.razao_social = data.nome;
|
||||
if (data.razao_social && !empresaForm.razao_social) {
|
||||
empresaForm.razao_social = data.razao_social;
|
||||
}
|
||||
if (data.fantasia && !empresaForm.nome_fantasia) {
|
||||
empresaForm.nome_fantasia = data.fantasia;
|
||||
if (data.nome_fantasia && !empresaForm.nome_fantasia) {
|
||||
empresaForm.nome_fantasia = data.nome_fantasia;
|
||||
}
|
||||
if (data.telefone && !empresaForm.telefone) {
|
||||
empresaForm.telefone = maskPhone(data.telefone);
|
||||
|
||||
const telefoneFonte = data.ddd_telefone_1 ?? data.telefone;
|
||||
if (telefoneFonte && !empresaForm.telefone) {
|
||||
empresaForm.telefone = maskPhone(telefoneFonte);
|
||||
}
|
||||
if (data.email && !empresaForm.email) {
|
||||
empresaForm.email = data.email;
|
||||
@@ -463,8 +464,8 @@
|
||||
<table class="table table-zebra">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Razão social / Nome fantasia</th>
|
||||
<th>CNPJ</th>
|
||||
<th>Razão social / Nome fantasia</th>
|
||||
<th>Telefone</th>
|
||||
<th>E-mail</th>
|
||||
<th>Ações</th>
|
||||
@@ -473,6 +474,7 @@
|
||||
<tbody>
|
||||
{#each empresasQuery.data as empresa (empresa._id)}
|
||||
<tr>
|
||||
<td>{empresa.cnpj}</td>
|
||||
<td>
|
||||
<div class="flex flex-col">
|
||||
<span class="font-semibold">{empresa.razao_social}</span>
|
||||
@@ -481,7 +483,6 @@
|
||||
{/if}
|
||||
</div>
|
||||
</td>
|
||||
<td>{empresa.cnpj}</td>
|
||||
<td class="flex items-center gap-2">
|
||||
<Phone class="h-4 w-4 text-base-content/60" strokeWidth={2} />
|
||||
<span>{empresa.telefone}</span>
|
||||
|
||||
2
packages/backend/convex/_generated/api.d.ts
vendored
2
packages/backend/convex/_generated/api.d.ts
vendored
@@ -15,6 +15,7 @@ import type * as actions_smtp from "../actions/smtp.js";
|
||||
import type * as actions_utils_nodeCrypto from "../actions/utils/nodeCrypto.js";
|
||||
import type * as atestadosLicencas from "../atestadosLicencas.js";
|
||||
import type * as ausencias from "../ausencias.js";
|
||||
import type * as autenticacao from "../autenticacao.js";
|
||||
import type * as auth from "../auth.js";
|
||||
import type * as auth_utils from "../auth/utils.js";
|
||||
import type * as chamados from "../chamados.js";
|
||||
@@ -64,6 +65,7 @@ declare const fullApi: ApiFromModules<{
|
||||
"actions/utils/nodeCrypto": typeof actions_utils_nodeCrypto;
|
||||
atestadosLicencas: typeof atestadosLicencas;
|
||||
ausencias: typeof ausencias;
|
||||
autenticacao: typeof autenticacao;
|
||||
auth: typeof auth;
|
||||
"auth/utils": typeof auth_utils;
|
||||
chamados: typeof chamados;
|
||||
|
||||
Reference in New Issue
Block a user