@tyagoveras/node-mde
Version:
Biblioteca para consultar notas destinadas e enviar evento de manifestação do destinatário
62 lines (60 loc) • 2.72 kB
JavaScript
let {
CA,
DISTRIBUICAO_MDFE
} = require("../env"), DistribuicaoMdfeSchema = require("../schemas").DistribuicaoMdfeSchema, SefazService = require("../services/sefaz-service"), {
Gzip,
Xml
} = require("../util");
class DistribuicaoMdfeHelper {
static async enviarConsulta(e, s) {
var t = DISTRIBUICAO_MDFE[s.tpAmb], e = {
method: "POST",
data: e
}, a = {
...s.requestOptions || {},
headers: {
...s.requestOptions && s.requestOptions.headers,
"Content-Type": 'application/soap+xml; charset=utf-8; action="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeDistribuicaoDFe/mdfeDistDFeInteresse"'
}
};
return await new SefazService({
baseURL: t,
ca: CA,
cert: s.cert,
key: s.key,
tpAmb: s.tpAmb,
requestOptions: a,
httpsOptions: s.httpsOptions
}).request(e);
}
static montarRequest(e) {
var s = DistribuicaoMdfeSchema.montarSchema(e), t = Xml.jsonToXml(s), s = s.mdfeDadosMsg.distDFeInt["@_versao"] || "1.00";
return '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">' + `<soap:Header>${`<mdfeCabecMsg xmlns="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeDistribuicaoDFe"><cUF>${e.cUFAutor}</cUF><versaoDados>${s}</versaoDados></mdfeCabecMsg>`}</soap:Header>` + `<soap:Body>${t}</soap:Body>` + "</soap:Envelope>";
}
static async montarResponse(e) {
var s, t = {}, e = Xml.xmlToJson(e);
return e.error ? (t.error = e.error, t) : ({
"soap:Envelope": {
"soap:Body": {
mdfeDistDFeInteresseResult: {
retDistDFeInt: e = {}
} = {}
} = {}
} = {}
} = e, {
loteDistDFeInt: s = {}
} = e, s = (s.docZip ? Array.isArray(s.docZip) || (s.docZip = [ s.docZip ]) : s.docZip = [],
await Promise.all(s.docZip.map(async e => {
var s = await Gzip.unzip(e.value);
return {
xml: s,
json: Xml.xmlToJson(s),
nsu: e["@_NSU"],
schema: e["@_schema"]
};
}))), t.tpAmb = e.tpAmb || "", t.verAplic = e.verAplic || "", t.cStat = e.cStat || "",
t.xMotivo = e.xMotivo || "", t.dhResp = e.dhResp || "", t.ultNSU = e.ultNSU || "",
t.maxNSU = e.maxNSU || "", t.docZip = s), t;
}
}
module.exports = Object.freeze(DistribuicaoMdfeHelper);