Skip to content

Commit

Permalink
fix: fix private request method for distribuicaoDfe
Browse files Browse the repository at this point in the history
  • Loading branch information
CayoPOliveira committed Jan 26, 2025
1 parent 1f6dcbe commit 9f2f9c1
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 91 deletions.
26 changes: 14 additions & 12 deletions src/dfe/nfe/webServices/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,18 +424,20 @@ describe("NfeWebServices", async () => {
method: "POST",
response: {
data: buildMockResponse({
nfeResultMsg: {
retDistDFeInt: {
tpAmb: "2",
cStat: "138",
xMotivo: "Documento localizado",
dhResp: "2025-01-24T08:00:00-03:00",
ultNSU: "0",
maxNSU: "0",
loteDistDFeInt: {
"@_NSU": "0",
"@_schema": "resNFe_v1.00.xsd",
docZip: "NFe compactada no padrão gZip",
nfeDistDFeInteresseResponse: {
nfeDistDFeInteresseResult: {
retDistDFeInt: {
tpAmb: "2",
cStat: "138",
xMotivo: "Documento localizado",
dhResp: "2025-01-24T08:00:00-03:00",
ultNSU: "0",
maxNSU: "0",
loteDistDFeInt: {
"@_NSU": "0",
"@_schema": "resNFe_v1.00.xsd",
docZip: "NFe compactada no padrão gZip",
},
},
},
},
Expand Down
196 changes: 120 additions & 76 deletions src/dfe/nfe/webServices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import type {
NfeDistribuicaoDfeResponseRaw,
NfeDistribuicaoDfeStatus,
} from "./requests/distribuicaoDfe";
import { updateLanguageServiceSourceFile } from "typescript";

export class NfeWebServices {
private uf: UF;
Expand Down Expand Up @@ -125,12 +126,10 @@ export class NfeWebServices {
{ body, timeout, arrayTags }: NfeRequestOptions<Body>,
): Promise<NfeRequestResponse> {
const { cert, key } = this.certificate.asPem();
const soapBody = buildSoap({ nfeDadosMsg: body });

const response = await fetchWithTls(url, {
method: "POST",
headers: { "Content-Type": "application/soap+xml; charset=utf-8" },
body: soapBody,
body: buildSoap(body),
tls: { cert, key, ca: await this.getCa() },
signal: AbortSignal.timeout(timeout),
});
Expand All @@ -142,15 +141,15 @@ export class NfeWebServices {
}

const responseBody = await response.text();
const parsedResponse = parseSoap<{ nfeResultMsg?: NfeRequestResponse }>(
responseBody,
{ arrayTags },
);
if (!parsedResponse?.nfeResultMsg) {
const parsedResponse = parseSoap<NfeRequestResponse>(responseBody, {
arrayTags,
});

if (!parsedResponse) {
throw new NfeServiceRequestError(`URL: ${url}\n${responseBody}`);
}

return parsedResponse.nfeResultMsg;
return parsedResponse;
}

/**
Expand All @@ -162,19 +161,24 @@ export class NfeWebServices {
* @throws {NfeServiceRequestError} Se ocorrer um erro durante a requisição.
*/
async statusServico(): Promise<NfeStatusServicoResponse> {
const { retConsStatServ } = await this.request<
NfeStatusServicoRequest,
{ retConsStatServ: NfeStatusServicoResponseRaw }
const {
nfeResultMsg: { retConsStatServ },
} = await this.request<
{ nfeDadosMsg: NfeStatusServicoRequest },
{ nfeResultMsg: { retConsStatServ: NfeStatusServicoResponseRaw } }
>(this.getUrl("NfeStatusServico"), {
timeout: this.timeout,
body: {
"@_xmlns": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4",
consStatServ: {
...this.xmlNamespace,
"@_versao": "4.00",
tpAmb: this.tpAmb,
cUF: this.cUF,
xServ: "STATUS",
nfeDadosMsg: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4",
consStatServ: {
...this.xmlNamespace,
"@_versao": "4.00",
tpAmb: this.tpAmb,
cUF: this.cUF,
xServ: "STATUS",
},
},
},
});
Expand Down Expand Up @@ -207,18 +211,22 @@ export class NfeWebServices {
): Promise<NfeConsultaCadastroResponse> {
schemaNfeConsultaCadastroOptions.parse(options);

const { retConsCad } = await this.request<
NfeConsultaCadastroRequest,
{ retConsCad: NfeConsultaCadastroResponseRaw }
const {
nfeResultMsg: { retConsCad },
} = await this.request<
{ nfeDadosMsg: NfeConsultaCadastroRequest },
{ nfeResultMsg: { retConsCad: NfeConsultaCadastroResponseRaw } }
>(this.getUrl("NfeConsultaCadastro"), {
timeout: this.timeout,
body: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/CadConsultaCadastro4",
ConsCad: {
...this.xmlNamespace,
"@_versao": "2.00",
infCons: { xServ: "CONS-CAD", UF: this.uf, ...options },
nfeDadosMsg: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/CadConsultaCadastro4",
ConsCad: {
...this.xmlNamespace,
"@_versao": "2.00",
infCons: { xServ: "CONS-CAD", UF: this.uf, ...options },
},
},
},
arrayTags: ["infCad"],
Expand Down Expand Up @@ -257,19 +265,23 @@ export class NfeWebServices {
certificate: this.certificate,
signId: nfe.NFe.infNFe["@_Id"],
}) as NfeLayoutWithSignature;
const { retEnviNFe } = await this.request<
NfeAutorizacaoRequest,
{ retEnviNFe: NfeAutorizacaoResponseRaw }
const {
nfeResultMsg: { retEnviNFe },
} = await this.request<
{ nfeDadosMsg: NfeAutorizacaoRequest },
{ nfeResultMsg: { retEnviNFe: NfeAutorizacaoResponseRaw } }
>(this.getUrl("NFeAutorizacao"), {
timeout: this.timeout,
body: {
"@_xmlns": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeAutorizacao4",
enviNFe: {
...this.xmlNamespace,
"@_versao": "4.00",
idLote,
indSinc: "1",
...signedNfe,
nfeDadosMsg: {
"@_xmlns": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeAutorizacao4",
enviNFe: {
...this.xmlNamespace,
"@_versao": "4.00",
idLote,
indSinc: "1",
...signedNfe,
},
},
},
});
Expand Down Expand Up @@ -354,14 +366,18 @@ export class NfeWebServices {
certificate: this.certificate,
signId: id,
}) as NfeInutilizacaoInutNfeWithSignature;
const { retInutNFe } = await this.request<
NfeInutilizacaoRequest,
{ retInutNFe: NfeInutilizacaoResponseRaw }
const {
nfeResultMsg: { retInutNFe },
} = await this.request<
{ nfeDadosMsg: NfeInutilizacaoRequest },
{ nfeResultMsg: { retInutNFe: NfeInutilizacaoResponseRaw } }
>(this.getUrl("NfeInutilizacao"), {
timeout: this.timeout,
body: {
"@_xmlns": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeInutilizacao4",
...signedInutNfe,
nfeDadosMsg: {
"@_xmlns": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeInutilizacao4",
...signedInutNfe,
},
},
});
const statusMap: Record<string, NfeInutilizacaoStatus> = {
Expand Down Expand Up @@ -420,18 +436,23 @@ export class NfeWebServices {
certificate: this.certificate,
signId: recepcaoEvento.evento.infEvento["@_Id"],
}) as NfeRecepcaoEventoEventoWithSignature;
const { retEnvEvento } = await this.request<
NfeRecepcaoEventoRequest,
{ retEnvEvento: NfeRecepcaoEventoResponseRaw }
const {
nfeResultMsg: { retEnvEvento },
} = await this.request<
{ nfeDadosMsg: NfeRecepcaoEventoRequest },
{ nfeResultMsg: { retEnvEvento: NfeRecepcaoEventoResponseRaw } }
>(this.getUrl("RecepcaoEvento"), {
timeout: this.timeout,
body: {
"@_xmlns": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeRecepcaoEvento4",
envEvento: {
...this.xmlNamespace,
"@_versao": "1.00",
idLote,
...signedRecepcaoEvento,
nfeDadosMsg: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/NFeRecepcaoEvento4",
envEvento: {
...this.xmlNamespace,
"@_versao": "1.00",
idLote,
...signedRecepcaoEvento,
},
},
},
});
Expand Down Expand Up @@ -476,20 +497,24 @@ export class NfeWebServices {
async consultaProtocolo({
chNFe,
}: NfeConsultaProtocoloOptions): Promise<NfeConsultaProtocoloResponse> {
const { retConsSitNFe } = await this.request<
NfeConsultaProtocoloRequest,
{ retConsSitNFe: NfeConsultaProtocoloResponseRaw }
const {
nfeResultMsg: { retConsSitNFe },
} = await this.request<
{ nfeDadosMsg: NfeConsultaProtocoloRequest },
{ nfeResultMsg: { retConsSitNFe: NfeConsultaProtocoloResponseRaw } }
>(this.getUrl("NfeConsultaProtocolo"), {
timeout: this.timeout,
body: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/NFeConsultaProtocolo4",
consSitNFe: {
...this.xmlNamespace,
"@_versao": "4.00",
tpAmb: this.tpAmb,
xServ: "CONSULTAR",
chNFe,
nfeDadosMsg: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/NFeConsultaProtocolo4",
consSitNFe: {
...this.xmlNamespace,
"@_versao": "4.00",
tpAmb: this.tpAmb,
xServ: "CONSULTAR",
chNFe,
},
},
},
arrayTags: ["procEventoNFe"],
Expand Down Expand Up @@ -526,28 +551,47 @@ export class NfeWebServices {

let operation: NfeDistribuicaoDfeOperation;
if (distNSU) {
operation = { distNSU };
operation = { distNSU: { ultNSU: distNSU.ultNSU.padStart(15, "0") } };
} else if (consNSU) {
operation = { consNSU };
} else {
operation = { consChNFe };
}

const { retDistDFeInt } = await this.request<
NfeDistribuicaoDfeRequest,
{ retDistDFeInt: NfeDistribuicaoDfeResponseRaw }
const {
nfeDistDFeInteresseResponse: {
nfeDistDFeInteresseResult: { retDistDFeInt },
},
} = await this.request<
{
nfeDistDFeInteresse: {
"@_xmlns": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe";
nfeDadosMsg: NfeDistribuicaoDfeRequest;
};
},
{
nfeDistDFeInteresseResponse: {
nfeDistDFeInteresseResult: {
retDistDFeInt: NfeDistribuicaoDfeResponseRaw;
};
};
}
>(this.getUrl("NFeDistribuicaoDFe"), {
timeout: this.timeout,
body: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/NFeConsultaProtocolo4",
distDFeInt: {
...this.xmlNamespace,
"@_versao": "1.00",
tpAmb: this.tpAmb,
cUFAutor: this.cUF,
...cpfOrCnpj,
...operation,
nfeDistDFeInteresse: {
"@_xmlns":
"http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe",
nfeDadosMsg: {
distDFeInt: {
"@_versao": "1.00",
...this.xmlNamespace,
tpAmb: this.tpAmb,
cUFAutor: this.cUF,
...cpfOrCnpj,
...operation,
},
},
},
},
arrayTags: ["docZip"],
Expand Down
6 changes: 3 additions & 3 deletions src/dfe/nfe/webServices/requests/distribuicaoDfe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export type NfeDistribuicaoDfeOperation =
*/
export type NfeDistribuicaoDfeOptions = CpfOrCnpj & NfeDistribuicaoDfeOperation;

export type NfeDistribuicaoDfeRequest = WithXmlns<{
export type NfeDistribuicaoDfeRequest = {
distDFeInt: WithXmlnsVersao<
{
tpAmb: "1" | "2";
cUFAutor: UFCode;
} & CpfOrCnpj &
NfeDistribuicaoDfeOperation
>;
}>;
};

/**
* @description Resposta completa da consulta de status serviço.
Expand All @@ -58,7 +58,7 @@ export type NfeDistribuicaoDfeRequest = WithXmlns<{
* @property [ultNSU] - Último NSU processado.
* @property [maxNSU] - Maior NSU encontrado.
* @property [loteDistDFeInt] - Lote de distribuição de DF-e.
* @property loteDistDFeInt.docZip - Documentos zipados.
* @property loteDistDFeInt.docZip - Array de documentos zipados.
* @property loteDistDFeInt["@_NSU"] - NSU do lote.
* @property loteDistDFeInt["@_schema"] - Schema do lote.
*/
Expand Down

0 comments on commit 9f2f9c1

Please sign in to comment.