/*
* This file is part of Treeunfe DFe.
*
* Treeunfe DFe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Treeunfe DFe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Treeunfe DFe. If not, see .
*/
import { XmlBuilder, BaseNFe, Environment, Utility, logger } from '@treeunfe/shared';
import { GerarConsultaImpl, NFEConsultaGTINServiceImpl, SaveFilesImpl } from '@treeunfe/types/interfaces';
import { GenericObject } from '@treeunfe/types';
import { AxiosInstance, AxiosResponse } from 'axios';
import { Agent } from 'http';
class NFEConsultaGTINService extends BaseNFe implements NFEConsultaGTINServiceImpl {
constructor(environment: Environment, utility: Utility, xmlBuilder: XmlBuilder, axios: AxiosInstance, saveFiles: SaveFilesImpl, gerarConsulta: GerarConsultaImpl) {
super(environment, utility, xmlBuilder, 'NFEConsultaGTIN', axios, saveFiles, gerarConsulta);
}
protected gerarXml(gtin: string): string {
logger.info('Montando estrutura do XML em JSON', {
context: 'NFEConsultaGTINService',
});
const xmlObject = {
$: {
versao: '1.00',
xmlns: 'http://www.portalfiscal.inf.br/nfe'
},
GTIN: gtin,
};
return this.xmlBuilder.gerarXml(xmlObject, 'consGTIN', this.metodo);
}
protected async callWebService(xmlConsulta: string, webServiceUrl: string, _ContentType: string, action: string, agent: Agent): Promise> {
const response = await this.axios.post(webServiceUrl, xmlConsulta, {
headers: {
'Content-Type': `application/soap+xml; charset=utf-8; action="${action}"`,
},
httpsAgent: agent,
validateStatus: (status) => status < 600,
});
if (response.status >= 400) {
logger.error('Resposta HTTP do webservice', { context: 'NFEConsultaGTINService', status: response.status, body: response.data });
throw new Error(`Webservice retornou HTTP ${response.status}: ${typeof response.data === 'string' ? response.data.substring(0, 500) : JSON.stringify(response.data)}`);
}
return response;
}
async Exec(gtin: string): Promise {
let xmlConsulta: string = '';
let xmlConsultaSoap: string = '';
let responseInJson: GenericObject | undefined = undefined;
let xmlRetorno: AxiosResponse = {} as AxiosResponse;
const ContentType = this.setContentType();
try {
xmlConsulta = this.gerarXml(gtin);
const { xmlFormated, agent, webServiceUrl, action } = await this.gerarConsulta.gerarConsulta(
xmlConsulta,
this.metodo,
true,
'1.00',
'NFe',
true,
'ccgConsGTIN',
'nfeDadosMsg',
);
xmlConsultaSoap = xmlFormated;
xmlRetorno = await this.callWebService(xmlFormated, webServiceUrl, ContentType, action, agent);
responseInJson = this.utility.verificaRejeicao(xmlRetorno.data, this.metodo);
return responseInJson;
} finally {
this.utility.salvaConsulta(xmlConsulta, xmlConsultaSoap, this.metodo);
this.utility.salvaRetorno(xmlRetorno.data, responseInJson, this.metodo);
}
}
}
export default NFEConsultaGTINService;