import axios from "axios"; import { prodEndpointCreation } from "./createUrl.js"; import { tryCatch } from "./tryCatch.js"; import { createLog } from "../services/logger/logger.js"; type bodyData = any; type Data = { endpoint: string; data: bodyData[]; }; export const runProdApi = async (data: Data) => { /** * Detachs a silo */ let url = await prodEndpointCreation(data.endpoint); const { data: d, error } = await tryCatch( axios.post(url, data.data[0], { headers: { "X-API-Key": process.env.TEC_API_KEY || "", "Content-Type": "application/json", }, }) ); let e = error as any; if (e) { //console.log(e.response); if (e.status === 401) { createLog( "error", "lst", "logistics", `Not autorized: ${JSON.stringify(e.response?.data)}` ); const data = { success: false, message: `Not autorized: ${JSON.stringify(e.response?.data)}`, data: { status: e.response?.status, statusText: e.response?.statusText, data: e.response?.data, }, }; return data; } else { createLog( "error", "lst", "logistics", `There was an error processing the endpoint: ${JSON.stringify( e.response?.data )}` ); return { success: false, message: `There was an error processing the endpoint: ${JSON.stringify( e.response?.data )}`, data: { status: e.response?.status, statusText: e.response?.statusText, data: e.response?.data, }, }; } } if (d?.status !== 200) { return { success: false, message: "Error processing endpoint", data: { status: d?.status, statusText: d?.statusText, data: d?.data, }, }; } else { return { success: true, message: "Endpoint was processed", data: { status: d.status, statusText: d.statusText, data: d.data, }, }; } };