393 lines
15 KiB
TypeScript
393 lines
15 KiB
TypeScript
import axios, { AxiosError } from 'axios';
|
|
import serversideConfig from '../../serversideConfig';
|
|
import https from 'https';
|
|
|
|
let maintenanceVisitTemplates = [];
|
|
let selectedMVTsByMVT = [];
|
|
let selectedMVTsByCustomer = [];
|
|
let mvtTodosByTemplateId = [];
|
|
let mvt = [];
|
|
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/getAllMaintenanceVisitTemplates")) {
|
|
|
|
// get all maintenance visit templates object from backend
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisits`);
|
|
maintenanceVisitTemplates = 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/addMVT")) {
|
|
const body = await readBody(event)
|
|
// add the mvt in the backend
|
|
try {
|
|
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisits`, 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/addTodoMVT")) {
|
|
const body = await readBody(event)
|
|
// add the mvt todo in the backend
|
|
try {
|
|
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisitTodos`, 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/getSelectedMVTsByMVT")) {
|
|
// get selected maintenance visit templates 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}/selectedMasterMaintenanceVisitsByChecklistname/${filteredMVt}`);
|
|
selectedMVTsByMVT = 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/getSelectedMVTsByCustomer")) {
|
|
// get selected maintenance visit 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}/selectedMasterMaintenanceVisitsByCustomer/${filteredCustomer}`);
|
|
selectedMVTsByCustomer = 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/deleteMaintenanceVisitTemplate")) {
|
|
let mvtId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
mvtId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the mvt in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisits/${mvtId}`);
|
|
} 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/getTodosMaintenanceVisitTemplate")) {
|
|
// get maintenance visit 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}/masterMaintenanceVisitTodos/${itemId}`);
|
|
mvtTodosByTemplateId = 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/deleteTodosMVT")) {
|
|
let mvtId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
mvtId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the mvt todos in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisitTodosByTemplateid/${mvtId}`);
|
|
} 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/updateMaintenanceVisitTemplate")) {
|
|
const body = await readBody(event)
|
|
// update the maintenance visit template in the backend
|
|
try {
|
|
let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisits`, 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/getMaintenanceVisitTemplate")) {
|
|
// get maintenance visit template object from backend
|
|
let mvtId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
mvtId = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisit/${mvtId}`);
|
|
mvt = 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/updateMVTTodo")) {
|
|
const body = await readBody(event)
|
|
// update the maintenance visit template todo in the backend
|
|
try {
|
|
let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisitTodos`, 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/deleteMVTTodo")) {
|
|
let mvtId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
mvtId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the mvt in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/masterMaintenanceVisitTodos/${mvtId}`);
|
|
} 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 { maintenanceVisitTemplates, selectedMVTsByMVT, selectedMVTsByCustomer, mvtTodosByTemplateId, mvt, insertId, errorMsg }; |