392 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			392 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import axios, { AxiosError } from 'axios';
 | |
| import serversideConfig from '../../serversideConfig';
 | |
| import https from 'https';
 | |
| 
 | |
| let productionOrderTemplates = [];
 | |
| let selectedPOTsByCustomer = [];
 | |
| let selectedPOTsByPOT = [];
 | |
| let potTodosByTemplateId = [];
 | |
| let pot = [];
 | |
| 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/getAllProductionOrderTemplates")) {
 | |
| 
 | |
|         // get all production order templates object from backend
 | |
|         try {
 | |
|             let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrders`);
 | |
|             productionOrderTemplates = 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/addPOT")) {
 | |
|         const body = await readBody(event)
 | |
|         // add the pot in the backend
 | |
|         try {
 | |
|             let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrders`, 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/getProductionOrderTemplate")) {
 | |
|         // get production order template 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}/masterProductionOrders/${itemId}`);
 | |
|             pot = 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/updateProductionOrderTemplate")) {
 | |
|         const body = await readBody(event)
 | |
|         // update the production order template in the backend
 | |
|         try {
 | |
|             let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrders`, 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/getSelectedPOTsByCustomer")) {
 | |
|         // get selected production order templates 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}/selectedMasterProductionOrdersByCustomer/${filteredCustomer}`);
 | |
|             selectedPOTsByCustomer = 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/getSelectedPOTsByPOT")) {
 | |
|         // get selected production order templates object by pot from backend
 | |
|         let filteredPOT = null;
 | |
|         const path = event._path;
 | |
|         const pathSegments = path.split('/');
 | |
|         filteredPOT = pathSegments[pathSegments.length - 1];
 | |
| 
 | |
|         try {
 | |
|             let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedMasterProductionOrdersByChecklistname/${filteredPOT}`);
 | |
|             selectedPOTsByPOT = 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/deleteProductionOrderTemplate")) {
 | |
|         let potId = null;
 | |
|         const path = event._path;
 | |
|         const pathSegments = path.split('/');
 | |
|         potId = pathSegments[pathSegments.length - 1];
 | |
| 
 | |
|         // delete the pot in the backend
 | |
|         try {
 | |
|             let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrders/${potId}`);
 | |
|         } 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/getTodosProductionOrderTemplate")) {
 | |
|         // get production order template 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}/masterProductionOrderTodos/${itemId}`);
 | |
|             potTodosByTemplateId = 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/addTodoPOT")) {
 | |
|         const body = await readBody(event)
 | |
|         // add the pot todo in the backend
 | |
|         try {
 | |
|             let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrderTodos`, 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/updatePOTTodo")) {
 | |
|         const body = await readBody(event)
 | |
|         // update the production order template todo in the backend
 | |
|         try {
 | |
|             let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrderTodos`, 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/deleteTodosPOT")) {
 | |
|         let potId = null;
 | |
|         const path = event._path;
 | |
|         const pathSegments = path.split('/');
 | |
|         potId = pathSegments[pathSegments.length - 1];
 | |
| 
 | |
|         // delete the pot in the backend
 | |
|         try {
 | |
|             let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrderTodosByTemplateid/${potId}`);
 | |
|         } 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/deletePOTTodo")) {
 | |
|         let potId = null;
 | |
|         const path = event._path;
 | |
|         const pathSegments = path.split('/');
 | |
|         potId = pathSegments[pathSegments.length - 1];
 | |
| 
 | |
|         // delete the pot in the backend
 | |
|         try {
 | |
|             let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/masterProductionOrderTodos/${potId}`);
 | |
|         } 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 { productionOrderTemplates, selectedPOTsByCustomer, selectedPOTsByPOT, pot, potTodosByTemplateId, insertId, errorMsg }; |