494 lines
19 KiB
TypeScript
494 lines
19 KiB
TypeScript
import axios, { AxiosError } from 'axios';
|
|
import serversideConfig from '../../serversideConfig';
|
|
import https from 'https';
|
|
|
|
let productionOrderInstances = [];
|
|
let selectedPOIsByPOI = [];
|
|
let selectedPOIsByPOT = [];
|
|
let selectedPOIsByState = [];
|
|
let selectedPOIsByUser = [];
|
|
let selectedPOIsByCustomer = [];
|
|
let selectedPOIsByName = [];
|
|
let poiTodosByTemplateId = [];
|
|
let poi = [];
|
|
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/getAllProductionOrderInstances")) {
|
|
|
|
// get all maintenance visit instances object from backend
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrders`);
|
|
productionOrderInstances = 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/addTodoPOI")) {
|
|
const body = await readBody(event)
|
|
// add the poi in the backend
|
|
try {
|
|
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrderTodos`, 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/addPOI")) {
|
|
const body = await readBody(event)
|
|
// add the poi todo in the backend
|
|
try {
|
|
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrder`, 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/getSelectedPOIsByPOI")) {
|
|
// get selected production order instances object by poi from backend
|
|
let filteredPOI = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredPOI = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedProductionOrdersByChecklistname/${filteredPOI}`);
|
|
selectedPOIsByPOI = 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/getSelectedPOIsByState")) {
|
|
// get selected production order instances object by state from backend
|
|
let filteredPOI = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredPOI = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedProductionOrdersByState/${filteredPOI}`);
|
|
selectedPOIsByState = 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/getSelectedPOIsByPOT")) {
|
|
// get selected maintenance visit instances object by mvt from backend
|
|
let filteredMVT = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredMVT = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedProductionOrdersByTemplate/${filteredMVT}`);
|
|
selectedPOIsByPOT = 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/getSelectedPOIsByUser")) {
|
|
// get selected production order instances object by user from backend
|
|
let filteredUser = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredUser = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedProductionOrdersByUser/${filteredUser}`);
|
|
selectedPOIsByUser = 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/getSelectedPOIsByName")) {
|
|
// get selected production order instances object by name from backend
|
|
let filteredName = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredName = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedProductionOrdersByChecklistname/${filteredName}`);
|
|
selectedPOIsByName = 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/getSelectedPOIsByState")) {
|
|
// get selected production order instances object by state from backend
|
|
let filteredState = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredState = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedProductionOrdersByState/${filteredState}`);
|
|
selectedPOIsByState = 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/deleteProductionOrderInstance")) {
|
|
let poiId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
poiId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the mvt in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrder/${poiId}`);
|
|
} 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/getTodosProductionOrderInstance")) {
|
|
// get maintenance visit instance 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}/productionOrderTodosByTemplateID/${itemId}`);
|
|
poiTodosByTemplateId = 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/deleteTodosPOI")) {
|
|
let poiId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
poiId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the poi todos in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrderTodos/${poiId}`);
|
|
} 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/updateProductionOrderInstance")) {
|
|
const body = await readBody(event)
|
|
// update the maintenance visit instance in the backend
|
|
try {
|
|
let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrder`, 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/getProductionOrderInstance")) {
|
|
// get maintenance visit instance object from backend
|
|
let poiId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
poiId = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrder/${poiId}`);
|
|
poi = 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/updatePOITodo")) {
|
|
const body = await readBody(event)
|
|
// update the production order instance todo in the backend
|
|
try {
|
|
let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/productionOrderTodos`, 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);
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
export { productionOrderInstances, selectedPOIsByState, selectedPOIsByUser, selectedPOIsByPOT, selectedPOIsByPOI, selectedPOIsByName, selectedPOIsByCustomer, poiTodosByTemplateId, poi, insertId, errorMsg }; |