diff --git a/index.js b/index.js index 7a67abe..5b36205 100644 --- a/index.js +++ b/index.js @@ -481,6 +481,50 @@ app.get("/applications", checkAuthorization, async (req, res) => { }); }); +app.get("/myapplications", checkAuthorization, async (req, res) => { + const token = req.cookies.token; + + var options = { + method: "POST", + url: process.env.API_SERVER + "/forms/getmyapplications", + headers: { + Authorization: req.cookies.token, + }, + }; + + axios + .request(options) + .then(function (response) { + const authorizationHeader = response.headers["authorization"]; + + res.cookie("token", authorizationHeader, { + maxAge: twelveHoursInSeconds * 1000, + }); + + var templateData = { + API_SERVER: process.env.API_SERVER, + User: response.data.userData, + Role: response.data.role, + Applications: response.data.applications, + Total: response.data.totalCount, + }; + + const source = fs.readFileSync( + "static/templates/account/myapplications.html", + "utf8" + ); + const template = handlebars.compile(source); + const resultT = template(templateData); + res.send(resultT); + }) + .catch(function (error) { + if (error.response) { + console.error("Ошибка при отправке GET-запроса:", error); + res.status(500).send("Произошла ошибка при выполнении запроса."); + } + }); +}); + app.get("/passes", checkAuthorization, async (req, res) => { const token = req.cookies.token; diff --git a/static/scripts/applications-table.js b/static/scripts/applications-table.js index 59f1d3c..cc4cb81 100644 --- a/static/scripts/applications-table.js +++ b/static/scripts/applications-table.js @@ -20,7 +20,7 @@ const createTable = () => { type.textContent = application.type; row.appendChild(type); const worker = document.createElement("td"); - worker.textContent = application.worker; + worker.innerHTML = application.worker; row.appendChild(worker); const legal = document.createElement("td"); legal.innerHTML = application.legal; diff --git a/static/scripts/myapplications-table.js b/static/scripts/myapplications-table.js new file mode 100644 index 0000000..98bc424 --- /dev/null +++ b/static/scripts/myapplications-table.js @@ -0,0 +1,141 @@ +const createTable = () => { + const table = document.getElementById("applicationsTable"); + const tbody = table.querySelector("tbody"); + + // Очищаем таблицу + tbody.innerHTML = ""; + + applications.forEach((application) => { + const row = document.createElement("tr"); + row.setAttribute("onclick", `openApplication(${application.id});`); + + // Добавляем ячейки с данными + const id = document.createElement("td"); + id.textContent = application.id; + row.appendChild(id); + const status = document.createElement("td"); + status.textContent = application.status; + row.appendChild(status); + const type = document.createElement("td"); + type.textContent = application.type; + row.appendChild(type); + const worker = document.createElement("td"); + worker.innerHTML = application.worker; + row.appendChild(worker); + const legal = document.createElement("td"); + legal.innerHTML = application.legal; + row.appendChild(legal); + const date = document.createElement("td"); + const rawDate = new Date(application.date); + const formattedDate = `${rawDate.getDate().toString().padStart(2, "0")}/${( + rawDate.getMonth() + 1 + ) + .toString() + .padStart(2, "0")}/${rawDate.getFullYear()}`; + date.textContent = formattedDate; + row.appendChild(date); + + const finaldate = document.createElement("td"); + if (application.finaldate) { + const rawFinalDate = new Date(application.finaldate); + const formattedfinalDate = `${rawFinalDate + .getDate() + .toString() + .padStart(2, "0")}/${(rawFinalDate.getMonth() + 1) + .toString() + .padStart(2, "0")}/${rawFinalDate.getFullYear()}`; + finaldate.textContent = formattedfinalDate; + } else { + finaldate.textContent = ""; + } + row.appendChild(finaldate); + const car = document.createElement("td"); + if (application.car) { + let carText = application.car.replace(/"/g, '"'); + carText = JSON.parse(carText).join(", "); + car.textContent = carText; + } + row.appendChild(car); + + tbody.appendChild(row); + }); +}; + +document.addEventListener("DOMContentLoaded", function () { + createTable(); +}); + +function getCookie(name) { + var cookies = document.cookie.split(";"); + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i].trim(); + if (cookie.startsWith(name + "=")) { + return cookie.substring(name.length + 1); + } + } + return null; +} + +function requestUpdate() { + document.getElementById("applicationsTable").style.filter = + "brightness(0.85)"; + + const formData = new FormData(); + formData.append( + "page", + parseInt(document.getElementById("page-number").textContent) + ); + + const requestOptions = { + method: "POST", + headers: { + Authorization: getCookie("token"), + }, + body: formData, + }; + + fetch(API_SERVER + "/forms/getmyapplications", requestOptions) + .then((response) => { + document.getElementById("applicationsTable").style.filter = + "brightness(1)"; + if (response.ok) { + return response.json(); + } else { + console.error("Ошибка при отправке POST запроса."); + return "Ошибка при отправке запроса."; + } + }) + .then((data) => { + applications.splice(0, applications.length); + + applications.push( + ...data.applications.map((item) => ({ + id: item.id, + status: item.Статус, + type: item.Вид_заявки, + worker: item.Работник, + date: item.Дата_заявки, + finaldate: item.Дата_решения, + legal: item.Организация, + car: item.Авто_гос_номер, + })) + ); + + createTable(); + + totalMax = Math.ceil(data.totalCount / 15); + + if (totalMax === 0) { + totalMax = 1; + } + + var currentPage = parseInt(pageNumberInput.textContent, 10); + if (currentPage > totalMax) { + pageNumberInput.textContent = totalMax; + requestUpdate(); + } + }) + .catch((error) => { + console.error("Ошибка при отправке запроса:", error); + }); +} diff --git a/static/scripts/passes-table.js b/static/scripts/passes-table.js index 432462d..4df78f6 100644 --- a/static/scripts/passes-table.js +++ b/static/scripts/passes-table.js @@ -29,7 +29,7 @@ const createTable = () => { type.textContent = pass.type; row.appendChild(type); const worker = document.createElement("td"); - worker.textContent = pass.name; + worker.innerHTML = pass.name; row.appendChild(worker); const legal = document.createElement("td"); legal.innerHTML = pass.legal; diff --git a/static/scripts/tso-table.js b/static/scripts/tso-table.js index 4dacf30..d942d90 100644 --- a/static/scripts/tso-table.js +++ b/static/scripts/tso-table.js @@ -26,10 +26,10 @@ const createTable = () => { date.textContent = formattedDate; row.appendChild(date); const type = document.createElement("td"); - type.textContent = pass.type; + type.innerHTML = pass.type; row.appendChild(type); const object = document.createElement("td"); - object.textContent = pass.object; + object.innerHTML = pass.object; row.appendChild(object); const editdate = document.createElement("td"); if (pass.editdate) { @@ -46,7 +46,7 @@ const createTable = () => { } row.appendChild(editdate); const executor = document.createElement("td"); - executor.textContent = pass.executor; + executor.innerHTML = pass.executor; row.appendChild(executor); tbody.appendChild(row); diff --git a/static/scripts/users-table.js b/static/scripts/users-table.js index 1c4a1b8..3717b7a 100644 --- a/static/scripts/users-table.js +++ b/static/scripts/users-table.js @@ -20,13 +20,13 @@ const createTable = () => { id.textContent = user.id; row.appendChild(id); const surname = document.createElement("td"); - surname.textContent = user.surname; + surname.innerHTML = user.surname; row.appendChild(surname); const name = document.createElement("td"); - name.textContent = user.name; + name.innerHTML = user.name; row.appendChild(name); const secondname = document.createElement("td"); - secondname.textContent = user.secondname; + secondname.innerHTML = user.secondname; row.appendChild(secondname); const role = document.createElement("td"); if (user.role) { @@ -36,10 +36,10 @@ const createTable = () => { } row.appendChild(role); const phone = document.createElement("td"); - phone.textContent = user.phone; + phone.innerHTML = user.phone; row.appendChild(phone); const email = document.createElement("td"); - email.textContent = user.email; + email.innerHTML = user.email; row.appendChild(email); const sub = document.createElement("td"); sub.textContent = user.sub; diff --git a/static/templates/account/account.html b/static/templates/account/account.html index 79d65f4..f3ba234 100644 --- a/static/templates/account/account.html +++ b/static/templates/account/account.html @@ -77,6 +77,24 @@