import axios, { AxiosError } from 'axios'; import serversideConfig from '../../serversideConfig'; import https from 'https'; let solutions = []; let solution = {}; let selectedSolutionsByCustomer = []; let selectedSolutionsBySolution = []; let solTodosBySolutionId = []; let insertId = -1; let errorMsg = ''; export default defineEventHandler(async (event) => { const agent = new https.Agent({ rejectUnauthorized: false, }); const axiosInstance = axios.create({ headers: { 'Content-Type': 'application/json', Accept: "*", }, httpsAgent: agent }); if (event.path.startsWith("/api/getAllSolutions")) { // get all solutions object from backend try { let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/solutions`); solutions = res.data; } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/getSelectedSolutionsByCustomer")) { // get selected solutions object by customer from backend let filteredCustomer = null; const path = event._path; const pathSegments = path.split('/'); filteredCustomer = pathSegments[pathSegments.length - 1]; try { let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedSolutionsByCustomer/${filteredCustomer}`); selectedSolutionsByCustomer = res.data; } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/getSelectedSolutionsBySolution")) { // get selected solutions object by asset from backend let filteredSolution = null; const path = event._path; const pathSegments = path.split('/'); filteredSolution = pathSegments[pathSegments.length - 1]; try { let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedSolutionsBySolutionName/${filteredSolution}`); selectedSolutionsBySolution = res.data; } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/getSolution")) { // get solution object from backend let solutionId = null; const path = event._path; const pathSegments = path.split('/'); solutionId = pathSegments[pathSegments.length - 1]; try { let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/solutions/${solutionId}`); solution = res.data; } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/updateSolution")) { const body = await readBody(event) // update the solution in the backend try { let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/solutions`, body); } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { console.log(err) // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/addSol")) { const body = await readBody(event) // add the solution in the backend try { let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/solutions`, body); insertId = res.data.insertId } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { console.log(err) // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/deleteSolution")) { let solId = null; const path = event._path; const pathSegments = path.split('/'); solId = pathSegments[pathSegments.length - 1]; // delete the solution in the backend try { let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/solutions/${solId}`); } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { console.log(err) // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/addTodoSol")) { const body = await readBody(event) // add the solution todo in the backend try { let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/checklistSolutionsTodos`, body); } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { console.log(err) // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/updateSolTodo")) { const body = await readBody(event) // update the solution todo in the backend try { let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/checklistSolutionsTodos`, body); } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { console.log(err) // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/getTodosSolution")) { // get solution todos object from backend let itemId = null; const path = event._path; const pathSegments = path.split('/'); itemId = pathSegments[pathSegments.length - 1]; try { let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/checklistSolutionsTodos/${itemId}`); solTodosBySolutionId = res.data; } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/deleteTodosSol")) { let solId = null; const path = event._path; const pathSegments = path.split('/'); solId = pathSegments[pathSegments.length - 1]; // delete the mvt todos in the backend try { let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/checklistSolutionsTodosBySolutionID/${solId}`); } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { console.log(err) // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } if (event.path.startsWith("/api/deleteSolTodo")) { let solId = null; const path = event._path; const pathSegments = path.split('/'); solId = pathSegments[pathSegments.length - 1]; // delete the mvt in the backend try { let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/checklistSolutionsTodos/${solId}`); } catch (err) { if (axios.isAxiosError(err)) { const axiosError = err as AxiosError; if (axiosError.response) { // Axios error console.error(axiosError.response.data.message); errorMsg = axiosError.response.data.message; } else if (axiosError.request) { console.log(err) // If error was caused by the request console.error(axiosError.request); } else { // Other errors console.error('Error', axiosError.message); } } else { // No AxiosError console.error('Error', err); } } } }) export { solutions, solution, selectedSolutionsByCustomer, selectedSolutionsBySolution, solTodosBySolutionId, insertId, errorMsg };