455 lines
17 KiB
TypeScript
455 lines
17 KiB
TypeScript
import axios, { AxiosError } from 'axios';
|
|
import serversideConfig from '../../serversideConfig';
|
|
import https from 'https';
|
|
|
|
let issues = [];
|
|
let allIssueVariants = [];
|
|
let issue = {};
|
|
let selectedIssuesByIssueName = [];
|
|
let selectedIssuesByState = [];
|
|
let issueVariants = [];
|
|
let issueVariant = [];
|
|
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/getAllIssues")) {
|
|
|
|
// get all issues object from backend
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/issues`);
|
|
issues = 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/getAllIssueVariants")) {
|
|
|
|
// get all issue variants object from backend
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/issueVariants`);
|
|
allIssueVariants = 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/getSelectedIssuesByIssueName")) {
|
|
// get selected issues object by issue name from backend
|
|
let filteredIssue = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredIssue = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedIssuesByIssueName/${filteredIssue}`);
|
|
selectedIssuesByIssueName = 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/getSelectedIssuesByState")) {
|
|
// get selected issues object by state from backend
|
|
let filteredIssue = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
filteredIssue = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedIssuesByState/${filteredIssue}`);
|
|
selectedIssuesByState = 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/deleteIssueItem")) {
|
|
let issueId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
issueId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the issue in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/issues/${issueId}`);
|
|
} 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/updateIssueItem")) {
|
|
const body = await readBody(event)
|
|
// update the issue in the backend
|
|
try {
|
|
let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/issues`, 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/getIssueItem")) {
|
|
// get issue object from backend
|
|
let issueId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
issueId = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/issues/${issueId}`);
|
|
issue = 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/getIssueVariant")) {
|
|
// get issue variant object from backend
|
|
let issueVariantId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
issueVariantId = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/issueVariants/${issueVariantId}`);
|
|
issueVariant = 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/addII")) {
|
|
const body = await readBody(event)
|
|
// add the issue in the backend
|
|
try {
|
|
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/issues`, 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/addIssueVariant")) {
|
|
const body = await readBody(event)
|
|
// add the issue variant in the backend
|
|
try {
|
|
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/issueVariants`, 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/updateIssueVariant")) {
|
|
const body = await readBody(event)
|
|
// update the issue variant in the backend
|
|
try {
|
|
let res = await axiosInstance.put(`https://${serversideConfig.url}:${serversideConfig.port}/issueVariants`, 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/getIVs")) {
|
|
// get issue variants object from backend
|
|
let issueId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
issueId = pathSegments[pathSegments.length - 1];
|
|
|
|
try {
|
|
let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/selectedIssueVariantsByIssueId/${issueId}`);
|
|
issueVariants = 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/deleteIV")) {
|
|
let issueId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
issueId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the issue variant in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/issueVariantsByIssueSlipId/${issueId}`);
|
|
} 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/deleteIssueVariant")) {
|
|
let issueId = null;
|
|
const path = event._path;
|
|
const pathSegments = path.split('/');
|
|
issueId = pathSegments[pathSegments.length - 1];
|
|
|
|
// delete the issue variant in the backend
|
|
try {
|
|
let res = await axiosInstance.delete(`https://${serversideConfig.url}:${serversideConfig.port}/issueVariants/${issueId}`);
|
|
} 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 { issues, issueVariant, allIssueVariants, issueVariants, selectedIssuesByIssueName, selectedIssuesByState, issue, insertId, errorMsg }; |