Files
TueIT_App/server/middleware/customerEmployees.ts
2024-04-07 14:14:02 +02:00

277 lines
10 KiB
TypeScript

import axios, { AxiosError } from 'axios';
import serversideConfig from '../../serversideConfig';
import https from 'https';
let customerEmployee = {};
let errorMsg = '';
let selectedCustomerEmployeesByCustomer = [];
let selectedCustomerEmployeesByName = [];
let selectedCustomerEmployeesByDepartment = [];
let selectedCustomerEmployeesByDepartmentName = [];
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/getCustEmployee")) {
// get customer employee 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}/employee/${itemId}`);
customerEmployee = 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/getSelectedCustEmployeeByName")) {
// get all customer employees by name 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}/employeeByName/${itemId}`);
selectedCustomerEmployeesByName = 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/getSelectedCustEmployeeByDprtmntName")) {
// get all customer employees by department name 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}/employeeByDepartmentName/${itemId}`);
selectedCustomerEmployeesByDepartmentName = 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/getSelectedCustEmployeeByCustomer")) {
// get all customer employees from a customer 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}/employeeByCustomerID/${itemId}`);
selectedCustomerEmployeesByCustomer = 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/getSelectedCustEmployeeByDepartment")) {
// get all customer employees by department 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}/employeeByDeprtmentID/${itemId}`);
selectedCustomerEmployeesByDepartment = 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/updateCustEmployee")) {
const body = await readBody(event)
// update the customer employee in the backend
try {
let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/employee`, 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/addCustEmployee")) {
const body = await readBody(event)
// add the customer employee in the backend
try {
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/employees`, 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/deleteCustEmployee")) {
let itemId = null;
const path = event._path;
const pathSegments = path.split('/');
itemId = pathSegments[pathSegments.length - 1];
// delete the customer employee in the backend
try {
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/employees/${itemId}`);
} 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 { customerEmployee, selectedCustomerEmployeesByDepartmentName, selectedCustomerEmployeesByCustomer, selectedCustomerEmployeesByName, selectedCustomerEmployeesByDepartment, errorMsg };