79 lines
2.0 KiB
JavaScript
79 lines
2.0 KiB
JavaScript
import fetch from 'node-fetch';
|
|
import { Op } from 'sequelize';
|
|
|
|
import config from '../services/config.json' assert {type: 'json'};
|
|
import { interval } from 'date-fns';
|
|
|
|
const dataUrbano = config.dataUrbano;
|
|
const dataInfracommerce = config.dataInfracommerce;
|
|
|
|
const sendData = async (url, token = null, payload) => {
|
|
|
|
try {
|
|
const headers = {
|
|
// 'Authorization': `Bearer ${token}`,
|
|
'user': dataUrbano.userHeader,
|
|
'pass': dataUrbano.passHeader,
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
};
|
|
|
|
const options = {
|
|
method: 'POST',
|
|
headers: headers,
|
|
body: `json=${JSON.stringify(payload)}`
|
|
};
|
|
|
|
const response = await fetch(url, options);
|
|
|
|
const result = await response.json();
|
|
return result;
|
|
|
|
} catch (error) {
|
|
console.error(error);
|
|
return error;
|
|
}
|
|
};
|
|
|
|
const sendRequestInfracommerce = async (payload, url) => {
|
|
// if (!omsId) {
|
|
// throw new Error('omsId no proporcionado');
|
|
// }
|
|
|
|
// const url = dataEnvioEstados.url.replace(':oms_id', omsId);
|
|
const { AppKey, AppToken } = dataInfracommerce;
|
|
|
|
// console.log(AppKey, AppToken)
|
|
|
|
try {
|
|
const headers = {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
'OMS-AppKey': AppKey,
|
|
'OMS-AppTok': AppToken
|
|
};
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
console.log(response)
|
|
if (!response.ok) {
|
|
const res = await response.json();
|
|
const data = JSON.stringify(res, null, 2);
|
|
throw new Error(`Error en la solicitud: ${response.status} ${data}`);
|
|
}
|
|
const result = await response.json();
|
|
return result;
|
|
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
}
|
|
}
|
|
|
|
|
|
export {
|
|
sendData,
|
|
sendRequestInfracommerce
|
|
} |