2024-05-15 14:22:55 +00:00
|
|
|
|
const cors = require("cors");
|
|
|
|
|
const bcrypt = require("bcrypt");
|
|
|
|
|
const path = require("path");
|
|
|
|
|
const jwt = require("jsonwebtoken");
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
const sql = require("mssql");
|
|
|
|
|
const multer = require("multer");
|
|
|
|
|
const { DateTime } = require("luxon");
|
|
|
|
|
|
|
|
|
|
const express = require("express");
|
|
|
|
|
const app = express();
|
|
|
|
|
require("dotenv").config();
|
|
|
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
|
res.header(
|
|
|
|
|
"Access-Control-Allow-Headers",
|
|
|
|
|
"Origin, X-Requested-With, Content-Type, Accept"
|
|
|
|
|
);
|
|
|
|
|
res.header("Access-Control-Allow-Headers", "Authorization");
|
|
|
|
|
res.header("Access-Control-Allow-Methods", "POST");
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
app.use(express.urlencoded({ extended: false }));
|
|
|
|
|
app.use(cors());
|
|
|
|
|
|
|
|
|
|
const dbConfig = {
|
|
|
|
|
user: process.env.DB_USER,
|
|
|
|
|
password: process.env.DB_PASSWORD,
|
|
|
|
|
server: process.env.DB_HOST,
|
|
|
|
|
database: process.env.DB_NAME,
|
|
|
|
|
port: parseInt(process.env.DB_PORT),
|
|
|
|
|
options: {
|
|
|
|
|
trustServerCertificate: true, // Разрешить доверять самоподписанным сертификатам
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const storage = multer.memoryStorage();
|
|
|
|
|
const upload = multer({ storage: storage });
|
|
|
|
|
|
|
|
|
|
async function executeQuery(query, params) {
|
|
|
|
|
console.log(params);
|
|
|
|
|
try {
|
|
|
|
|
// Create a connection pool
|
|
|
|
|
const pool = await new sql.ConnectionPool(dbConfig).connect();
|
|
|
|
|
// Create a new request
|
|
|
|
|
const request = pool.request();
|
|
|
|
|
|
|
|
|
|
// Add parameters to the request
|
|
|
|
|
for (const key in params) {
|
|
|
|
|
if (params.hasOwnProperty(key)) {
|
|
|
|
|
request.input(key, params[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute the query
|
|
|
|
|
const result = await request.query(query);
|
|
|
|
|
|
|
|
|
|
// Close the connection pool
|
|
|
|
|
pool.close();
|
|
|
|
|
|
|
|
|
|
return result.recordset;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const sqlQuery = await fs.readFileSync("create_tables.sql", "utf-8");
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(sqlQuery);
|
|
|
|
|
|
|
|
|
|
console.log("Tables created successfully");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Ошибка MSSQL", err);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
function generateToken(userId, role) {
|
|
|
|
|
return jwt.sign({ userId, role }, process.env.SECRET, {
|
|
|
|
|
expiresIn: "12h",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Функция middleware для проверки токена
|
|
|
|
|
async function checkToken(req, res, next) {
|
|
|
|
|
const token = req.headers.authorization;
|
|
|
|
|
|
|
|
|
|
// const admins = await pool.query("SELECT * FROM users WHERE isadmin = true");
|
|
|
|
|
// if (admins.rows.length === 0) {
|
|
|
|
|
// return res.status(400).json({ message: "Необходима установка" });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
|
console.log("Токен отсутствует");
|
|
|
|
|
return res.status(401).json({ message: "Токен отсутствует" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log(token, process.env.SECRET);
|
|
|
|
|
const decoded = jwt.verify(token, process.env.SECRET);
|
|
|
|
|
req.user = decoded;
|
|
|
|
|
res.set("Authorization", generateToken(decoded.userId, decoded.role));
|
|
|
|
|
next();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("Неверный токен");
|
|
|
|
|
return res.status(401).json({ message: "Неверный токен" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saltRounds = 10;
|
|
|
|
|
|
|
|
|
|
// Обработка POST на регистрацию
|
|
|
|
|
app.post("/signup", upload.single("file"), async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
firstname,
|
|
|
|
|
surname,
|
|
|
|
|
secondname,
|
|
|
|
|
born,
|
|
|
|
|
name,
|
|
|
|
|
inn,
|
|
|
|
|
ogrn,
|
|
|
|
|
legaladdress,
|
|
|
|
|
address,
|
|
|
|
|
phone,
|
|
|
|
|
email,
|
|
|
|
|
contact,
|
|
|
|
|
password,
|
|
|
|
|
formrole,
|
|
|
|
|
} = req.body;
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
|
|
|
|
|
|
|
|
|
const checkLegal = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE Email = @email",
|
|
|
|
|
{
|
|
|
|
|
email: email,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const checkPersons = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE Email = @email",
|
|
|
|
|
{
|
|
|
|
|
email: email,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (checkPersons.length > 0 || checkLegal.length > 0) {
|
|
|
|
|
res.status(400).json({
|
|
|
|
|
message: "Такой Email уже зарегистрирован",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (formrole === "legal") {
|
|
|
|
|
const addResult = await executeQuery(
|
|
|
|
|
`INSERT INTO [Юридические лица] (Наименование, ИНН, ОГРН, Юридический_адрес, Фактический_адрес, Телефон, Email, Контактное_лицо, Пароль)
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
VALUES (@name, @inn, @ogrn, @legaladdress, @address, @phone, @email, @contact, @hashedPassword)
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
name: name,
|
|
|
|
|
inn: inn,
|
|
|
|
|
ogrn: ogrn,
|
|
|
|
|
legaladdress: legaladdress,
|
|
|
|
|
address: address,
|
|
|
|
|
phone: phone,
|
|
|
|
|
email: email,
|
|
|
|
|
contact: contact,
|
|
|
|
|
hashedPassword: hashedPassword,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const userId = addResult[0].id;
|
|
|
|
|
const role = "legal";
|
|
|
|
|
|
|
|
|
|
const token = jwt.sign({ userId, role }, process.env.SECRET, {
|
|
|
|
|
expiresIn: "12h",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Юридическое лицо успешно добавлено",
|
|
|
|
|
token: token,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const addResult = await executeQuery(
|
|
|
|
|
`INSERT INTO [Физические лица] (Имя, Фамилия, Отчество, Дата_рождения, Телефон, Email, Пароль)
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
VALUES (@firstname, @surname, @secondname, @born, @phone, @email, @hashedPassword)
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
firstname: firstname,
|
|
|
|
|
surname: surname,
|
|
|
|
|
secondname: secondname,
|
|
|
|
|
born: born,
|
|
|
|
|
phone: phone,
|
|
|
|
|
email: email,
|
|
|
|
|
hashedPassword: hashedPassword,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const userId = addResult[0].id;
|
|
|
|
|
const role = "guest";
|
|
|
|
|
|
|
|
|
|
const token = jwt.sign({ userId, role }, process.env.SECRET, {
|
|
|
|
|
expiresIn: "12h",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Физическое лицо успешно добавлено",
|
|
|
|
|
token: token,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка DELETE запроса для удаления строки с определенным ID из таблицы "Юридические лица"
|
|
|
|
|
app.delete("/legal/delete", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(
|
|
|
|
|
"DELETE FROM [Юридические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(200).json({ message: "Строка успешно удалена" });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке DELETE запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка DELETE запроса для удаления строки с определенным ID из таблицы "Юридические лица"
|
|
|
|
|
app.delete("/user/delete", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(
|
|
|
|
|
"SELECT id FROM [Трудоустройство] WHERE Работник = @id",
|
|
|
|
|
{
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (result.length > 0) {
|
|
|
|
|
const secondDelete = await executeQuery(
|
|
|
|
|
"DELETE FROM [Трудоустройство] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: result[0].id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const firstDelete = await executeQuery(
|
|
|
|
|
"DELETE FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(200).json({ message: "Строка успешно удалена" });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке DELETE запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка запроса на авторизацию и генерация JWT токена при успешной аутентификации
|
|
|
|
|
app.post("/login", upload.single("file"), async (req, res) => {
|
|
|
|
|
const { email, password } = req.body;
|
|
|
|
|
|
|
|
|
|
const checkResult = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE Email = @email",
|
|
|
|
|
{
|
|
|
|
|
email: email,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (checkResult.length > 0) {
|
|
|
|
|
console.log(checkResult[0].Пароль);
|
|
|
|
|
|
|
|
|
|
const checkUser = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Трудоустройство] WHERE Работник = @id",
|
|
|
|
|
{
|
|
|
|
|
id: checkResult[0].id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const storedHashedPassword = checkResult[0].Пароль;
|
|
|
|
|
const userId = checkResult[0].id;
|
|
|
|
|
var role;
|
|
|
|
|
if (!checkUser[0]) {
|
|
|
|
|
role = "guest";
|
|
|
|
|
} else {
|
|
|
|
|
role = checkUser[0].Должность;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const passwordMatch = await bcrypt.compare(password, storedHashedPassword);
|
|
|
|
|
if (!passwordMatch) {
|
|
|
|
|
return res.status(401).json({ message: "Неверные учетные данные" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const token = jwt.sign({ userId, role }, process.env.SECRET, {
|
|
|
|
|
expiresIn: "12h",
|
|
|
|
|
});
|
|
|
|
|
console.log("LOGIN", token);
|
|
|
|
|
res.json({ token });
|
|
|
|
|
} else {
|
|
|
|
|
const checkLegals = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE Email = @email",
|
|
|
|
|
{
|
|
|
|
|
email: email,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (checkLegals.length > 0) {
|
|
|
|
|
const storedHashedPassword = checkLegals[0].Пароль;
|
|
|
|
|
const userId = checkLegals[0].id;
|
|
|
|
|
const role = "legal";
|
|
|
|
|
|
|
|
|
|
const passwordMatch = await bcrypt.compare(
|
|
|
|
|
password,
|
|
|
|
|
storedHashedPassword
|
|
|
|
|
);
|
|
|
|
|
if (!passwordMatch) {
|
|
|
|
|
return res.status(401).json({ message: "Неверные учетные данные" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const token = jwt.sign({ userId, role }, process.env.SECRET, {
|
|
|
|
|
expiresIn: "12h",
|
|
|
|
|
});
|
|
|
|
|
console.log("LOGIN", token);
|
|
|
|
|
res.set("Token", token).json({ token });
|
|
|
|
|
res.json({ token });
|
|
|
|
|
} else {
|
|
|
|
|
return res.status(401).json({ message: "Пользователь не найден" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка запроса на получение данных для личного кабинета
|
|
|
|
|
app.get("/account", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const userId = req.user.userId;
|
|
|
|
|
const userRole = req.user.role;
|
|
|
|
|
|
|
|
|
|
console.log(userId, userRole);
|
|
|
|
|
|
|
|
|
|
if (userRole === "legal") {
|
|
|
|
|
const result = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
data: result[0],
|
|
|
|
|
role: userRole,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const result = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
data: result[0],
|
|
|
|
|
role: userRole,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при получении данных пользователя:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка POST на новую заявку
|
|
|
|
|
app.post(
|
|
|
|
|
"/passes/newform",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
formrole,
|
|
|
|
|
passtext,
|
|
|
|
|
type,
|
|
|
|
|
fullnames,
|
|
|
|
|
legalname,
|
|
|
|
|
date,
|
|
|
|
|
carnumber,
|
|
|
|
|
carbrand,
|
|
|
|
|
carmodel,
|
|
|
|
|
carcolor,
|
|
|
|
|
tmcname,
|
|
|
|
|
tmcunit,
|
|
|
|
|
tmcquantity,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
var who;
|
|
|
|
|
|
|
|
|
|
if (req.user.role === "legal") {
|
|
|
|
|
const result = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
who = result[0].Наименование;
|
|
|
|
|
} else {
|
|
|
|
|
const result = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
who =
|
|
|
|
|
result[0].Фамилия + " " + result[0].Имя + " " + result[0].Отчество;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const formattedDate = currentDate.toISOString();
|
|
|
|
|
|
|
|
|
|
for (const name of fullnames) {
|
|
|
|
|
if (name && name.trim().length > 0) {
|
|
|
|
|
await executeQuery(
|
|
|
|
|
`INSERT INTO [Заявки] (Статус, Вид_заявки, Работник, Дата_заявки, Дополнение, Организация, Авто_модель, Авто_марка, Авто_цвет, Авто_гос_номер, Наименование, Единица_измерения, Количество, Автор)
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
VALUES (@status, @type, @fullname, @added, @passtext, @legalname, @carmodel, @carbrand, @carcolor, @carnumber, @tmcname, @tmcunit, @tmcquantity, @who)
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: "Новая",
|
|
|
|
|
type: type,
|
|
|
|
|
fullname: name,
|
|
|
|
|
added: formattedDate,
|
|
|
|
|
passtext: passtext,
|
|
|
|
|
legalname: legalname,
|
|
|
|
|
carmodel: JSON.stringify(carmodel),
|
|
|
|
|
carbrand: JSON.stringify(carbrand),
|
|
|
|
|
carcolor: JSON.stringify(carcolor),
|
|
|
|
|
carnumber: JSON.stringify(carnumber),
|
|
|
|
|
tmcname: JSON.stringify(tmcname),
|
|
|
|
|
tmcunit: JSON.stringify(tmcunit),
|
|
|
|
|
tmcquantity: JSON.stringify(tmcquantity),
|
|
|
|
|
who: who,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Заявка успешно отправлена",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка POST на регистрацию
|
|
|
|
|
app.post(
|
|
|
|
|
"/passes/newtso",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { type, object, fabula, fullname } = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role === "guest") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const id = req.user.id;
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const formattedDate = currentDate.toISOString();
|
|
|
|
|
|
|
|
|
|
const addResult = await executeQuery(
|
|
|
|
|
`INSERT INTO [Заявки_ТСО] (Вид_неисправности, Объект, Фабула, Дата_подачи, Состояние, Кто_подал, Дата_изменения)
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
VALUES (@type, @object, @fabula, @formattedDate, @status, @fullname, @formattedDate)
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: "Новая",
|
|
|
|
|
type: type,
|
|
|
|
|
object: object,
|
|
|
|
|
fabula: fabula,
|
|
|
|
|
formattedDate: formattedDate,
|
|
|
|
|
id: id,
|
|
|
|
|
fullname: fullname,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Заявка успешно отправлена",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка PUT запроса с новыми данными пользователя
|
|
|
|
|
app.put(
|
|
|
|
|
"/account/update",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const {
|
|
|
|
|
firstname,
|
|
|
|
|
surname,
|
|
|
|
|
secondname,
|
|
|
|
|
born,
|
|
|
|
|
name,
|
|
|
|
|
inn,
|
|
|
|
|
ogrn,
|
|
|
|
|
legaladdress,
|
|
|
|
|
address,
|
|
|
|
|
phone,
|
|
|
|
|
contactphone,
|
|
|
|
|
email,
|
|
|
|
|
contact,
|
|
|
|
|
password,
|
|
|
|
|
formrole,
|
|
|
|
|
bornplace,
|
|
|
|
|
passport,
|
|
|
|
|
citizenship,
|
|
|
|
|
regaddress,
|
|
|
|
|
realaddress,
|
|
|
|
|
dms,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
const id = req.user.userId;
|
|
|
|
|
const role = req.user.role;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (role === "legal") {
|
|
|
|
|
const params = {};
|
|
|
|
|
let query = `UPDATE [Юридические лица] SET`;
|
|
|
|
|
|
|
|
|
|
if (password) {
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
|
|
|
|
query += ` Пароль = @password,`;
|
|
|
|
|
params.password = hashedPassword;
|
|
|
|
|
}
|
|
|
|
|
if (inn) {
|
|
|
|
|
query += ` ИНН = @inn,`;
|
|
|
|
|
params.inn = inn;
|
|
|
|
|
}
|
|
|
|
|
if (ogrn) {
|
|
|
|
|
query += ` ОГРН = @ogrn,`;
|
|
|
|
|
params.ogrn = ogrn;
|
|
|
|
|
}
|
|
|
|
|
if (legaladdress) {
|
|
|
|
|
query += ` Юридический_адрес = @legaladdress,`;
|
|
|
|
|
params.legaladdress = legaladdress;
|
|
|
|
|
}
|
|
|
|
|
if (address) {
|
|
|
|
|
query += ` Фактический_адрес = @address,`;
|
|
|
|
|
params.address = address;
|
|
|
|
|
}
|
|
|
|
|
if (contact) {
|
|
|
|
|
query += ` Контактное_лицо = @contact,`;
|
|
|
|
|
params.contact = contact;
|
|
|
|
|
}
|
|
|
|
|
if (contactphone) {
|
|
|
|
|
query += ` Телефон = @contactphone,`;
|
|
|
|
|
params.contactphone = contactphone;
|
|
|
|
|
}
|
|
|
|
|
if (name) {
|
|
|
|
|
query += ` Наименование = @name`;
|
|
|
|
|
params.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query += ` OUTPUT inserted.*`;
|
|
|
|
|
|
|
|
|
|
query += ` WHERE id = @id`;
|
|
|
|
|
|
|
|
|
|
console.log(query);
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(query, {
|
|
|
|
|
...params,
|
|
|
|
|
id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Данные пользователя успешно обновлены",
|
|
|
|
|
data: result,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const params = {};
|
|
|
|
|
let query = `UPDATE [Физические лица] SET`;
|
|
|
|
|
|
|
|
|
|
if (password) {
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
|
|
|
|
query += ` Пароль = @password,`;
|
|
|
|
|
params.password = hashedPassword;
|
|
|
|
|
}
|
|
|
|
|
if (surname) {
|
|
|
|
|
query += ` Фамилия = @surname,`;
|
|
|
|
|
params.surname = surname;
|
|
|
|
|
}
|
|
|
|
|
if (secondname) {
|
|
|
|
|
query += ` Отчество = @secondname,`;
|
|
|
|
|
params.secondname = secondname;
|
|
|
|
|
}
|
|
|
|
|
if (born) {
|
|
|
|
|
query += ` Дата_рождения = @born,`;
|
|
|
|
|
params.born = born;
|
|
|
|
|
}
|
|
|
|
|
if (bornplace) {
|
|
|
|
|
query += ` Место_рождения = @bornplace,`;
|
|
|
|
|
params.bornplace = bornplace;
|
|
|
|
|
}
|
|
|
|
|
if (passport) {
|
|
|
|
|
query += ` Паспорт = @passport,`;
|
|
|
|
|
params.passport = passport;
|
|
|
|
|
}
|
|
|
|
|
if (citizenship) {
|
|
|
|
|
query += ` Гражданство = @citizenship,`;
|
|
|
|
|
params.citizenship = citizenship;
|
|
|
|
|
}
|
|
|
|
|
if (regaddress) {
|
|
|
|
|
query += ` Место_регистрации = @regaddress,`;
|
|
|
|
|
params.regaddress = regaddress;
|
|
|
|
|
}
|
|
|
|
|
if (realaddress) {
|
|
|
|
|
query += ` Место_жительства = @realaddress,`;
|
|
|
|
|
params.realaddress = realaddress;
|
|
|
|
|
}
|
|
|
|
|
if (dms) {
|
|
|
|
|
query += ` Полис_ДМС = @dms,`;
|
|
|
|
|
params.dms = dms;
|
|
|
|
|
}
|
|
|
|
|
if (phone) {
|
|
|
|
|
query += ` Телефон = @phone,`;
|
|
|
|
|
params.phone = phone;
|
|
|
|
|
}
|
|
|
|
|
if (firstname) {
|
|
|
|
|
query += ` Имя = @firstname`;
|
|
|
|
|
params.firstname = firstname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query += ` OUTPUT inserted.*`;
|
|
|
|
|
|
|
|
|
|
query += ` WHERE id = @id`;
|
|
|
|
|
|
|
|
|
|
console.log(query);
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(query, {
|
|
|
|
|
...params,
|
|
|
|
|
id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Данные пользователя успешно обновлены",
|
|
|
|
|
data: result,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке PUT запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка POST на добавление работника из аккаунта гостя
|
|
|
|
|
app.post("/users/makeworker", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const addResult = await executeQuery(
|
|
|
|
|
`INSERT INTO [Трудоустройство] (Работник, Должность)
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
VALUES (@id, @position)
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
id: id,
|
|
|
|
|
position: "Работник",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Работник успешно добавлен",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка PUT запроса с новыми данными другого пользователя
|
|
|
|
|
app.put(
|
|
|
|
|
"/users/update",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
firstname,
|
|
|
|
|
surname,
|
|
|
|
|
secondname,
|
|
|
|
|
oldsurname,
|
|
|
|
|
born,
|
|
|
|
|
name,
|
|
|
|
|
inn,
|
|
|
|
|
ogrn,
|
|
|
|
|
legaladdress,
|
|
|
|
|
address,
|
|
|
|
|
phone,
|
|
|
|
|
contactphone,
|
|
|
|
|
contact,
|
|
|
|
|
password,
|
|
|
|
|
formrole,
|
|
|
|
|
bornplace,
|
|
|
|
|
passport,
|
|
|
|
|
citizenship,
|
|
|
|
|
regaddress,
|
|
|
|
|
realaddress,
|
|
|
|
|
tabel,
|
|
|
|
|
dms,
|
|
|
|
|
block,
|
|
|
|
|
organization,
|
|
|
|
|
sub,
|
|
|
|
|
department,
|
|
|
|
|
migration,
|
|
|
|
|
permit,
|
|
|
|
|
contract,
|
|
|
|
|
contractdate,
|
|
|
|
|
lastjob,
|
|
|
|
|
fire,
|
|
|
|
|
reason,
|
|
|
|
|
role,
|
|
|
|
|
id,
|
|
|
|
|
additional,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (formrole === "legal") {
|
|
|
|
|
const params = {};
|
|
|
|
|
let query = `UPDATE [Юридические лица] SET`;
|
|
|
|
|
|
|
|
|
|
if (password) {
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
|
|
|
|
query += ` Пароль = @password,`;
|
|
|
|
|
params.password = hashedPassword;
|
|
|
|
|
}
|
|
|
|
|
if (inn) {
|
|
|
|
|
query += ` ИНН = @inn,`;
|
|
|
|
|
params.inn = inn;
|
|
|
|
|
}
|
|
|
|
|
if (ogrn) {
|
|
|
|
|
query += ` ОГРН = @ogrn,`;
|
|
|
|
|
params.ogrn = ogrn;
|
|
|
|
|
}
|
|
|
|
|
if (legaladdress) {
|
|
|
|
|
query += ` Юридический_адрес = @legaladdress,`;
|
|
|
|
|
params.legaladdress = legaladdress;
|
|
|
|
|
}
|
|
|
|
|
if (address) {
|
|
|
|
|
query += ` Фактический_адрес = @address,`;
|
|
|
|
|
params.address = address;
|
|
|
|
|
}
|
|
|
|
|
if (contact) {
|
|
|
|
|
query += ` Контактное_лицо = @contact,`;
|
|
|
|
|
params.contact = contact;
|
|
|
|
|
}
|
|
|
|
|
if (contactphone) {
|
|
|
|
|
query += ` Телефон = @contactphone,`;
|
|
|
|
|
params.contactphone = contactphone;
|
|
|
|
|
}
|
|
|
|
|
if (additional) {
|
|
|
|
|
query += ` Дополнительно = @additional,`;
|
|
|
|
|
params.additional = additional;
|
|
|
|
|
}
|
|
|
|
|
if (name) {
|
|
|
|
|
query += ` Наименование = @name`;
|
|
|
|
|
params.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query += ` OUTPUT inserted.*`;
|
|
|
|
|
|
|
|
|
|
query += ` WHERE id = @id`;
|
|
|
|
|
|
|
|
|
|
console.log(query);
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(query, {
|
|
|
|
|
...params,
|
|
|
|
|
id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Данные пользователя успешно обновлены",
|
|
|
|
|
data: result,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const params = {};
|
|
|
|
|
let query = `UPDATE [Физические лица] SET`;
|
|
|
|
|
|
|
|
|
|
if (password) {
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
|
|
|
|
query += ` Пароль = @password,`;
|
|
|
|
|
params.password = hashedPassword;
|
|
|
|
|
}
|
|
|
|
|
if (surname) {
|
|
|
|
|
query += ` Фамилия = @surname,`;
|
|
|
|
|
params.surname = surname;
|
|
|
|
|
}
|
|
|
|
|
if (secondname) {
|
|
|
|
|
query += ` Отчество = @secondname,`;
|
|
|
|
|
params.secondname = secondname;
|
|
|
|
|
}
|
|
|
|
|
if (oldsurname) {
|
|
|
|
|
query += ` Фамилия_старая = @oldsurname,`;
|
|
|
|
|
params.oldsurname = oldsurname;
|
|
|
|
|
}
|
|
|
|
|
if (born) {
|
|
|
|
|
query += ` Дата_рождения = @born,`;
|
|
|
|
|
params.born = born;
|
|
|
|
|
}
|
|
|
|
|
if (bornplace) {
|
|
|
|
|
query += ` Место_рождения = @bornplace,`;
|
|
|
|
|
params.bornplace = bornplace;
|
|
|
|
|
}
|
|
|
|
|
if (passport) {
|
|
|
|
|
query += ` Паспорт = @passport,`;
|
|
|
|
|
params.passport = passport;
|
|
|
|
|
}
|
|
|
|
|
if (citizenship) {
|
|
|
|
|
query += ` Гражданство = @citizenship,`;
|
|
|
|
|
params.citizenship = citizenship;
|
|
|
|
|
}
|
|
|
|
|
if (regaddress) {
|
|
|
|
|
query += ` Место_регистрации = @regaddress,`;
|
|
|
|
|
params.regaddress = regaddress;
|
|
|
|
|
}
|
|
|
|
|
if (realaddress) {
|
|
|
|
|
query += ` Место_жительства = @realaddress,`;
|
|
|
|
|
params.realaddress = realaddress;
|
|
|
|
|
}
|
|
|
|
|
if (tabel) {
|
|
|
|
|
query += ` Табельный_номер = @tabel,`;
|
|
|
|
|
params.tabel = tabel;
|
|
|
|
|
}
|
|
|
|
|
if (dms) {
|
|
|
|
|
query += ` Полис_ДМС = @dms,`;
|
|
|
|
|
params.dms = dms;
|
|
|
|
|
}
|
|
|
|
|
if (block) {
|
|
|
|
|
query += ` Черный_список = @block,`;
|
|
|
|
|
params.block = block;
|
|
|
|
|
}
|
|
|
|
|
if (phone) {
|
|
|
|
|
query += ` Телефон = @phone,`;
|
|
|
|
|
params.phone = phone;
|
|
|
|
|
}
|
|
|
|
|
if (firstname) {
|
|
|
|
|
query += ` Имя = @firstname`;
|
|
|
|
|
params.firstname = firstname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query += ` OUTPUT inserted.*`;
|
|
|
|
|
|
|
|
|
|
query += ` WHERE id = @id`;
|
|
|
|
|
|
|
|
|
|
var result = await executeQuery(query, {
|
|
|
|
|
...params,
|
|
|
|
|
id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var workresult;
|
|
|
|
|
|
|
|
|
|
if (role) {
|
|
|
|
|
const workparams = {};
|
|
|
|
|
let workquery = `UPDATE [Трудоустройство] SET`;
|
|
|
|
|
|
|
|
|
|
if (organization) {
|
|
|
|
|
workquery += ` Организация = @organization,`;
|
|
|
|
|
workparams.organization = organization;
|
|
|
|
|
}
|
|
|
|
|
if (sub) {
|
|
|
|
|
workquery += ` Субподряд = @sub,`;
|
|
|
|
|
workparams.sub = sub;
|
|
|
|
|
}
|
|
|
|
|
if (department) {
|
|
|
|
|
workquery += ` Цех = @department,`;
|
|
|
|
|
workparams.department = department;
|
|
|
|
|
}
|
|
|
|
|
if (migration) {
|
|
|
|
|
workquery += ` Миграционная_карта = @migration,`;
|
|
|
|
|
workparams.migration = migration;
|
|
|
|
|
}
|
|
|
|
|
if (permit) {
|
|
|
|
|
workquery += ` Разрешение_на_работу_до = @permit,`;
|
|
|
|
|
workparams.permit = permit;
|
|
|
|
|
}
|
|
|
|
|
if (contract) {
|
|
|
|
|
workquery += ` Трудовой_договор = @contract,`;
|
|
|
|
|
workparams.contract = contract;
|
|
|
|
|
}
|
|
|
|
|
if (contractdate) {
|
|
|
|
|
workquery += ` Дата_договора = @contractdate,`;
|
|
|
|
|
workparams.contractdate = contractdate;
|
|
|
|
|
}
|
|
|
|
|
if (lastjob) {
|
|
|
|
|
workquery += ` Последнее_место_работы = @lastjob,`;
|
|
|
|
|
workparams.lastjob = lastjob;
|
|
|
|
|
}
|
|
|
|
|
if (fire) {
|
|
|
|
|
workquery += ` Увольнение = @fire,`;
|
|
|
|
|
workparams.fire = fire;
|
|
|
|
|
}
|
|
|
|
|
if (reason) {
|
|
|
|
|
workquery += ` Причина_увольнения = @reason,`;
|
|
|
|
|
workparams.reason = reason;
|
|
|
|
|
}
|
|
|
|
|
if (role) {
|
|
|
|
|
workquery += ` Должность = @role`;
|
|
|
|
|
workparams.role = role;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workquery += ` OUTPUT inserted.*`;
|
|
|
|
|
|
|
|
|
|
workquery += ` WHERE Работник = @id`;
|
|
|
|
|
|
|
|
|
|
workresult = await executeQuery(workquery, {
|
|
|
|
|
...workparams,
|
|
|
|
|
id,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Данные пользователя успешно обновлены",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке PUT запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка POST на получение списка пользователей
|
|
|
|
|
app.post(
|
|
|
|
|
"/users/getusers",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
var { page, searchText } = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "КПП") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageSize = 15;
|
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
|
|
let physicalPersonsQuery = `
|
|
|
|
|
SELECT pp.*, emp.id AS emp_id, emp.[Организация], emp.[Субподряд], emp.[Цех], emp.[Должность], emp.[Миграционная_карта], emp.[Разрешение_на_работу_до], emp.[Трудовой_договор], emp.[Дата_договора], emp.[Последнее_место_работы], emp.[Увольнение], emp.[Причина_увольнения]
|
|
|
|
|
FROM [Физические лица] pp
|
|
|
|
|
LEFT JOIN [Трудоустройство] emp ON pp.id = emp.Работник
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
let legalPersonsQuery = `
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM [Юридические лица]
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
var totalCountQuery;
|
|
|
|
|
|
|
|
|
|
if (searchText) {
|
|
|
|
|
totalCountQuery = `
|
|
|
|
|
SELECT
|
|
|
|
|
(SELECT COUNT(*)
|
|
|
|
|
FROM [Физические лица] pp
|
|
|
|
|
LEFT JOIN [Трудоустройство] emp ON pp.id = emp.Работник
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
pp.[Фамилия] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Имя] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Отчество] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Место_рождения] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Паспорт], ' ',
|
|
|
|
|
pp.[Табельный_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Гражданство] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Место_регистрации] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Место_жительства] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Полис_ДМС] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Табельный_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Телефон], ' ',
|
|
|
|
|
pp.[Email] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) AS PhysicalCount,
|
|
|
|
|
(SELECT COUNT(*)
|
|
|
|
|
FROM [Юридические лица]
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[ИНН] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[ОГРН] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Юридический_адрес] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Фактический_адрес] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Телефон], ' ',
|
|
|
|
|
[Email] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Контактное_лицо] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) AS LegalCount
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
physicalPersonsQuery += `
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
pp.[Фамилия] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Имя] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Отчество] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Место_рождения] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Паспорт], ' ',
|
|
|
|
|
pp.[Табельный_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Гражданство] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Место_регистрации] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Место_жительства] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Полис_ДМС] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Табельный_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
pp.[Телефон], ' ',
|
|
|
|
|
pp.[Email] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
legalPersonsQuery += `
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[ИНН] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[ОГРН] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Юридический_адрес] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Фактический_адрес] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Телефон], ' ',
|
|
|
|
|
[Email] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Контактное_лицо] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
} else {
|
|
|
|
|
totalCountQuery = `
|
|
|
|
|
SELECT
|
|
|
|
|
(SELECT COUNT(*)
|
|
|
|
|
FROM [Физические лица] pp
|
|
|
|
|
LEFT JOIN [Трудоустройство] emp ON pp.id = emp.Работник
|
|
|
|
|
) AS PhysicalCount,
|
|
|
|
|
(SELECT COUNT(*)
|
|
|
|
|
FROM [Юридические лица]
|
|
|
|
|
) AS LegalCount
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
physicalPersonsQuery += `
|
|
|
|
|
ORDER BY pp.id DESC
|
|
|
|
|
OFFSET ${offset} ROWS
|
|
|
|
|
FETCH NEXT ${pageSize} ROWS ONLY
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
legalPersonsQuery += `
|
|
|
|
|
ORDER BY id DESC
|
|
|
|
|
OFFSET ${offset} ROWS
|
|
|
|
|
FETCH NEXT ${pageSize} ROWS ONLY
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const people = await executeQuery(physicalPersonsQuery);
|
|
|
|
|
const legals = await executeQuery(legalPersonsQuery);
|
|
|
|
|
const totalCounts = await executeQuery(totalCountQuery);
|
|
|
|
|
|
|
|
|
|
const totalCount =
|
|
|
|
|
totalCounts[0].PhysicalCount + totalCounts[0].LegalCount;
|
|
|
|
|
const totalCountPeople = totalCounts[0].PhysicalCount;
|
|
|
|
|
const totalCountLegal = totalCounts[0].LegalCount;
|
|
|
|
|
|
|
|
|
|
var userData;
|
|
|
|
|
|
|
|
|
|
if (req.user.role === "legal") {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
people: people,
|
|
|
|
|
legals: legals,
|
|
|
|
|
totalCountPeople: totalCountPeople,
|
|
|
|
|
totalCountLegal: totalCountLegal,
|
|
|
|
|
totalCount: totalCount,
|
|
|
|
|
userData: userData[0],
|
|
|
|
|
role: req.user.role,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка POST на получение списка Заявок ТСО
|
|
|
|
|
app.post(
|
|
|
|
|
"/passes/gettso",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
var { page } = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role === "guest") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageSize = 15;
|
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
|
|
let tsoQuery = `
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM [Заявки_ТСО]
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
tsoQuery += `
|
|
|
|
|
ORDER BY id DESC
|
|
|
|
|
OFFSET ${offset} ROWS
|
|
|
|
|
FETCH NEXT ${pageSize} ROWS ONLY
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
totalCountQuery = `
|
|
|
|
|
SELECT
|
|
|
|
|
(SELECT COUNT(*)
|
|
|
|
|
FROM [Заявки_ТСО]
|
|
|
|
|
) AS TotalCount
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const tso = await executeQuery(tsoQuery);
|
|
|
|
|
const totalCounts = await executeQuery(totalCountQuery);
|
|
|
|
|
|
|
|
|
|
console.log(tso);
|
|
|
|
|
|
|
|
|
|
const totalCount = totalCounts[0].TotalCount;
|
|
|
|
|
|
|
|
|
|
console.log(totalCount);
|
|
|
|
|
|
|
|
|
|
var userData;
|
|
|
|
|
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
passes: tso,
|
|
|
|
|
totalCount: totalCount,
|
|
|
|
|
userData: userData[0],
|
|
|
|
|
role: req.user.role,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка GET запроса на получение данных конкретного физ лица
|
|
|
|
|
app.get("/users/getuser", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "КПП") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let physicalPersonsQuery = `
|
|
|
|
|
SELECT pp.*, emp.id AS emp_id, emp.[Организация], emp.[Субподряд], emp.[Цех], emp.[Должность], emp.[Миграционная_карта], emp.[Разрешение_на_работу_до], emp.[Трудовой_договор], emp.[Дата_договора], emp.[Последнее_место_работы], emp.[Увольнение], emp.[Причина_увольнения]
|
|
|
|
|
FROM [Физические лица] pp
|
|
|
|
|
LEFT JOIN [Трудоустройство] emp ON pp.id = emp.Работник
|
|
|
|
|
WHERE pp.id = @id
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(physicalPersonsQuery, { id });
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
data: result[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка GET запроса на получение данных конкретного юр лица
|
|
|
|
|
app.get("/legals/getlegal", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "КПП") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let legalsQuery = `
|
|
|
|
|
SELECT * FROM [Юридические лица] WHERE id = @id
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(legalsQuery, { id });
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
data: result[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка POST на получение списка заявок
|
|
|
|
|
app.post(
|
|
|
|
|
"/forms/getapplications",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
var { page, searchText } = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageSize = 15;
|
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
|
|
let applicationsQuery = `
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM [Заявки]
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
var totalCountQuery;
|
|
|
|
|
|
|
|
|
|
if (searchText) {
|
|
|
|
|
totalCountQuery = `
|
|
|
|
|
SELECT COUNT(*) AS total
|
|
|
|
|
FROM [Заявки]
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
[Статус] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Работник] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Вид_заявки] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_заявки], ' ',
|
|
|
|
|
[Дата_принятия], ' ',
|
|
|
|
|
[Решение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_решения], ' ',
|
|
|
|
|
[Дополнение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_модель] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_марка] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_гос_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_цвет] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Организация] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
applicationsQuery += `
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
[Статус] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Работник] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Вид_заявки] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_заявки], ' ',
|
|
|
|
|
[Дата_принятия], ' ',
|
|
|
|
|
[Решение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_решения], ' ',
|
|
|
|
|
[Дополнение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_модель] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_марка] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_гос_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_цвет] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Организация] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
} else {
|
|
|
|
|
totalCountQuery = `
|
|
|
|
|
SELECT COUNT(*) AS total FROM [Заявки]
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applicationsQuery += `
|
|
|
|
|
ORDER BY id DESC
|
|
|
|
|
OFFSET ${offset} ROWS
|
|
|
|
|
FETCH NEXT ${pageSize} ROWS ONLY
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const applications = await executeQuery(applicationsQuery);
|
|
|
|
|
const totalCount = await executeQuery(totalCountQuery);
|
|
|
|
|
|
|
|
|
|
var userData;
|
|
|
|
|
|
|
|
|
|
if (req.user.role === "legal") {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
applications: applications,
|
|
|
|
|
totalCount: totalCount[0].total,
|
|
|
|
|
userData: userData[0],
|
|
|
|
|
role: req.user.role,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-06-09 23:50:43 +00:00
|
|
|
|
// Обработка POST на получение списка заявок
|
|
|
|
|
app.post(
|
|
|
|
|
"/forms/getmyapplications",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
var { page, searchText } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageSize = 15;
|
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
|
|
var userData;
|
|
|
|
|
var fullname;
|
|
|
|
|
|
|
|
|
|
if (req.user.role === "legal") {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
fullname = userData[0].Наименование;
|
|
|
|
|
} else {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
fullname = `${userData[0].Фамилия} ${userData[0].Имя} ${userData[0].Отчество}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let applicationsQuery = `
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM [Заявки]
|
|
|
|
|
WHERE [Автор] = N'${fullname}' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
let totalCountQuery = `
|
|
|
|
|
SELECT COUNT(*) AS total
|
|
|
|
|
FROM [Заявки]
|
|
|
|
|
WHERE [Автор] = N'${fullname}' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
if (searchText) {
|
|
|
|
|
totalCountQuery += ` AND CONCAT(
|
|
|
|
|
[Статус] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Работник] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Вид_заявки] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_заявки], ' ',
|
|
|
|
|
[Дата_принятия], ' ',
|
|
|
|
|
[Решение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_решения], ' ',
|
|
|
|
|
[Дополнение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_модель] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_марка] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_гос_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_цвет] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Организация] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS`;
|
|
|
|
|
|
|
|
|
|
applicationsQuery += ` AND CONCAT(
|
|
|
|
|
[Статус] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Работник] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Вид_заявки] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_заявки], ' ',
|
|
|
|
|
[Дата_принятия], ' ',
|
|
|
|
|
[Решение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_решения], ' ',
|
|
|
|
|
[Дополнение] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_модель] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_марка] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_гос_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_цвет] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Организация] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applicationsQuery += `
|
|
|
|
|
ORDER BY id DESC
|
|
|
|
|
OFFSET ${offset} ROWS
|
|
|
|
|
FETCH NEXT ${pageSize} ROWS ONLY
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const applications = await executeQuery(applicationsQuery);
|
|
|
|
|
const totalCount = await executeQuery(totalCountQuery);
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
applications: applications,
|
|
|
|
|
totalCount: totalCount[0].total,
|
|
|
|
|
userData: userData[0],
|
|
|
|
|
role: req.user.role,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-15 14:22:55 +00:00
|
|
|
|
// Обработка GET запроса на получение данных конкретной заявки
|
|
|
|
|
app.get("/forms/application", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "Пропуска") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let legalsQuery = `
|
|
|
|
|
SELECT * FROM [Заявки] WHERE id = @id
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(legalsQuery, { id });
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
data: result[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка DELETE запроса для удаления строки с определенным ID из таблицы "Заявки"
|
|
|
|
|
app.delete("/forms/delete", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "Пропуска") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery("DELETE FROM [Заявки] WHERE id = @id", {
|
|
|
|
|
id: id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({ message: "Строка успешно удалена" });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке DELETE запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-09 23:50:43 +00:00
|
|
|
|
// Обработка PUT запроса с новыми данными заявки
|
2024-05-15 14:22:55 +00:00
|
|
|
|
app.put(
|
|
|
|
|
"/forms/update",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
name,
|
|
|
|
|
legal,
|
|
|
|
|
status,
|
|
|
|
|
decision,
|
|
|
|
|
carnumber,
|
|
|
|
|
carbrand,
|
|
|
|
|
carmodel,
|
|
|
|
|
carcolor,
|
|
|
|
|
tmcname,
|
|
|
|
|
tmcunit,
|
|
|
|
|
tmcquantity,
|
|
|
|
|
date,
|
2024-06-09 23:50:43 +00:00
|
|
|
|
todate,
|
2024-05-15 14:22:55 +00:00
|
|
|
|
finaldate,
|
|
|
|
|
additional,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const updateResult = await executeQuery(
|
2024-06-09 23:50:43 +00:00
|
|
|
|
`UPDATE [Заявки] SET Статус = @status, Работник = @name, Дата_заявки = @date, Действие_до = @todate, Дата_решения = @finaldate, Организация = @legal, Решение = @decision, Дополнение = @additional, Авто_модель = @carmodel, Авто_марка = @carbrand, Авто_цвет = @carcolor, Авто_гос_номер = @carnumber, Наименование = @tmcname, Единица_измерения = @tmcunit, Количество = @tmcquantity
|
2024-05-15 14:22:55 +00:00
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
WHERE id = @id
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: status,
|
|
|
|
|
name: name,
|
|
|
|
|
date: date,
|
2024-06-09 23:50:43 +00:00
|
|
|
|
todate: todate,
|
2024-05-15 14:22:55 +00:00
|
|
|
|
finaldate: finaldate,
|
|
|
|
|
legal: legal,
|
|
|
|
|
decision: decision,
|
|
|
|
|
additional: additional,
|
|
|
|
|
carmodel: carmodel,
|
|
|
|
|
carbrand: carbrand,
|
|
|
|
|
carcolor: carcolor,
|
|
|
|
|
carnumber: carnumber,
|
|
|
|
|
tmcname: tmcname,
|
|
|
|
|
tmcunit: tmcunit,
|
|
|
|
|
tmcquantity: tmcquantity,
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
2024-06-09 23:50:43 +00:00
|
|
|
|
message: "Данные заявки успешно обновлены",
|
2024-05-15 14:22:55 +00:00
|
|
|
|
data: updateResult[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке PUT запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка POST на добавление работника из аккаунта гостя
|
|
|
|
|
app.post(
|
|
|
|
|
"/forms/makepass",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
name,
|
|
|
|
|
legal,
|
|
|
|
|
status,
|
|
|
|
|
decision,
|
|
|
|
|
carnumber,
|
|
|
|
|
carbrand,
|
|
|
|
|
carmodel,
|
|
|
|
|
carcolor,
|
|
|
|
|
tmcname,
|
|
|
|
|
tmcunit,
|
|
|
|
|
tmcquantity,
|
|
|
|
|
date,
|
|
|
|
|
todate,
|
|
|
|
|
finaldate,
|
|
|
|
|
additional,
|
|
|
|
|
type,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const updateResult = await executeQuery(
|
|
|
|
|
`UPDATE [Заявки] SET Статус = @status, Работник = @name, Дата_заявки = @date, Дата_решения = @finaldate, Организация = @legal, Решение = @decision, Дополнение = @additional, Авто_модель = @carmodel, Авто_марка = @carbrand, Авто_цвет = @carcolor, Авто_гос_номер = @carnumber, Наименование = @tmcname, Единица_измерения = @tmcunit, Количество = @tmcquantity
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
WHERE id = @id
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: "Утверждено",
|
|
|
|
|
name: name,
|
|
|
|
|
date: date,
|
|
|
|
|
finaldate: finaldate,
|
|
|
|
|
legal: legal,
|
|
|
|
|
decision: decision,
|
|
|
|
|
additional: additional,
|
|
|
|
|
carmodel: carmodel,
|
|
|
|
|
carbrand: carbrand,
|
|
|
|
|
carcolor: carcolor,
|
|
|
|
|
carnumber: carnumber,
|
|
|
|
|
tmcname: tmcname,
|
|
|
|
|
tmcunit: tmcunit,
|
|
|
|
|
tmcquantity: tmcquantity,
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const addResult = await executeQuery(
|
|
|
|
|
`INSERT INTO [Пропуска] (Состояние, Работник, Заявка, Дата_выдачи, Действие_до, Авто_модель, Авто_марка, Авто_цвет, Авто_гос_номер, Организация, Наименование, Единица_измерения, Количество, Дополнительно, Вид_пропуска)
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
VALUES (@status, @name, @id, @date, @todate, @carmodel, @carbrand, @carcolor, @carnumber, @legal, @tmcname, @tmcunit, @tmcquantity, @additional, @type)
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: "Заказан",
|
|
|
|
|
name: name,
|
|
|
|
|
date: date,
|
|
|
|
|
todate: todate,
|
|
|
|
|
finaldate: finaldate,
|
|
|
|
|
legal: legal,
|
|
|
|
|
decision: decision,
|
|
|
|
|
additional: additional,
|
|
|
|
|
carmodel: carmodel,
|
|
|
|
|
carbrand: carbrand,
|
|
|
|
|
carcolor: carcolor,
|
|
|
|
|
carnumber: carnumber,
|
|
|
|
|
tmcname: tmcname,
|
|
|
|
|
tmcunit: tmcunit,
|
|
|
|
|
tmcquantity: tmcquantity,
|
|
|
|
|
id: id,
|
|
|
|
|
type: type,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Данные пользователя успешно обновлены",
|
|
|
|
|
data: addResult[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке PUT запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка POST на получение списка заявок
|
|
|
|
|
app.post(
|
|
|
|
|
"/passes/getpasses",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
var { page, searchText } = req.body;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
req.user.role !== "Дирекция" &&
|
|
|
|
|
req.user.role !== "Пропуска" &&
|
|
|
|
|
req.user.role !== "КПП"
|
|
|
|
|
) {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageSize = 15;
|
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
|
|
let passesQuery = `
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM [Пропуска]
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
var totalCountQuery;
|
|
|
|
|
|
|
|
|
|
if (searchText) {
|
|
|
|
|
totalCountQuery = `
|
|
|
|
|
SELECT COUNT(*) AS total
|
|
|
|
|
FROM [Пропуска]
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
[Состояние] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Работник] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Организация] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_выдачи], ' ',
|
|
|
|
|
[Действие_до], ' ',
|
|
|
|
|
[Вид_пропуска] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_модель], ' ',
|
|
|
|
|
[Авто_марка] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_гос_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_цвет] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дополнительно] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Зона_доступа] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Согласование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Цель_выдачи] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
passesQuery += `
|
|
|
|
|
WHERE CONCAT(
|
|
|
|
|
[Состояние] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Работник] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Организация] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дата_выдачи], ' ',
|
|
|
|
|
[Действие_до], ' ',
|
|
|
|
|
[Вид_пропуска] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_модель], ' ',
|
|
|
|
|
[Авто_марка] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_гос_номер] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Авто_цвет] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Дополнительно] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Зона_доступа] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Наименование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Согласование] COLLATE Cyrillic_General_CI_AS, ' ',
|
|
|
|
|
[Цель_выдачи] COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
) LIKE N'%${searchText}%' COLLATE Cyrillic_General_CI_AS
|
|
|
|
|
`;
|
|
|
|
|
} else {
|
|
|
|
|
totalCountQuery = `
|
|
|
|
|
SELECT COUNT(*) AS total FROM [Пропуска]
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
passesQuery += `
|
|
|
|
|
ORDER BY id DESC
|
|
|
|
|
OFFSET ${offset} ROWS
|
|
|
|
|
FETCH NEXT ${pageSize} ROWS ONLY
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const passes = await executeQuery(passesQuery);
|
|
|
|
|
const totalCount = await executeQuery(totalCountQuery);
|
|
|
|
|
|
|
|
|
|
var userData;
|
|
|
|
|
|
|
|
|
|
if (req.user.role === "legal") {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Юридические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
userData = await executeQuery(
|
|
|
|
|
"SELECT * FROM [Физические лица] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: req.user.userId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
passes: passes,
|
|
|
|
|
totalCount: totalCount[0].total,
|
|
|
|
|
userData: userData[0],
|
|
|
|
|
role: req.user.role,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка POST на добавление нового пропуска
|
|
|
|
|
app.post(
|
|
|
|
|
"/passes/newpass",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const {
|
|
|
|
|
type,
|
|
|
|
|
status,
|
|
|
|
|
name,
|
|
|
|
|
date,
|
|
|
|
|
todate,
|
|
|
|
|
phone,
|
|
|
|
|
email,
|
|
|
|
|
legal,
|
|
|
|
|
purpose,
|
|
|
|
|
address,
|
|
|
|
|
additional,
|
|
|
|
|
carnumber,
|
|
|
|
|
carbrand,
|
|
|
|
|
carmodel,
|
|
|
|
|
carcolor,
|
|
|
|
|
tmcname,
|
|
|
|
|
tmcunit,
|
|
|
|
|
tmcquantity,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "Пропуска") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const addResult = await executeQuery(
|
|
|
|
|
`INSERT INTO [Пропуска] (Состояние, Работник, Дата_выдачи, Действие_до, Авто_модель, Авто_марка, Авто_цвет, Авто_гос_номер, Организация, Наименование, Единица_измерения, Количество, Дополнительно, Вид_пропуска, Email, Телефон, Цель_выдачи, Зона_доступа)
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
VALUES (@status, @name, @date, @todate, @carmodel, @carbrand, @carcolor, @carnumber, @legal, @tmcname, @tmcunit, @tmcquantity, @additional, @type, @email, @phone, @purpose, @address)
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: status,
|
|
|
|
|
name: name,
|
|
|
|
|
date: date,
|
|
|
|
|
todate: todate,
|
|
|
|
|
legal: legal,
|
|
|
|
|
additional: additional,
|
|
|
|
|
carmodel: JSON.stringify(carmodel),
|
|
|
|
|
carbrand: JSON.stringify(carbrand),
|
|
|
|
|
carcolor: JSON.stringify(carcolor),
|
|
|
|
|
carnumber: JSON.stringify(carnumber),
|
|
|
|
|
tmcname: JSON.stringify(tmcname),
|
|
|
|
|
tmcunit: JSON.stringify(tmcunit),
|
|
|
|
|
tmcquantity: JSON.stringify(tmcquantity),
|
|
|
|
|
type: type,
|
|
|
|
|
email: email,
|
|
|
|
|
phone: phone,
|
|
|
|
|
purpose: purpose,
|
|
|
|
|
address: address,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
message: "Данные на пропуск успешно добавлены",
|
|
|
|
|
data: addResult[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке PUT запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка GET запроса на получение данных конкретной заявки
|
|
|
|
|
app.get("/passes/pass", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "Пропуска") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let passQuery = `
|
|
|
|
|
SELECT * FROM [Пропуска] WHERE id = @id
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(passQuery, { id });
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
data: result[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка POST на обновление данных пропуска
|
|
|
|
|
app.put(
|
|
|
|
|
"/passes/update",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const {
|
|
|
|
|
type,
|
|
|
|
|
status,
|
|
|
|
|
name,
|
|
|
|
|
date,
|
|
|
|
|
todate,
|
|
|
|
|
phone,
|
|
|
|
|
email,
|
|
|
|
|
legal,
|
|
|
|
|
purpose,
|
|
|
|
|
address,
|
|
|
|
|
additional,
|
|
|
|
|
carnumber,
|
|
|
|
|
carbrand,
|
|
|
|
|
carmodel,
|
|
|
|
|
carcolor,
|
|
|
|
|
tmcname,
|
|
|
|
|
tmcunit,
|
|
|
|
|
tmcquantity,
|
|
|
|
|
factsValues,
|
|
|
|
|
id,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "Пропуска") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const updateResult = await executeQuery(
|
|
|
|
|
`UPDATE [Пропуска]
|
|
|
|
|
SET Состояние = @status,
|
|
|
|
|
Работник = @name,
|
|
|
|
|
Дата_выдачи = @date,
|
|
|
|
|
Действие_до = @todate,
|
|
|
|
|
Авто_модель = @carmodel,
|
|
|
|
|
Авто_марка = @carbrand,
|
|
|
|
|
Авто_цвет = @carcolor,
|
|
|
|
|
Авто_гос_номер = @carnumber,
|
|
|
|
|
Организация = @legal,
|
|
|
|
|
Наименование = @tmcname,
|
|
|
|
|
Единица_измерения = @tmcunit,
|
|
|
|
|
Количество = @tmcquantity,
|
|
|
|
|
Дополнительно = @additional,
|
|
|
|
|
Вид_пропуска = @type,
|
|
|
|
|
Email = @email,
|
|
|
|
|
Телефон = @phone,
|
|
|
|
|
Цель_выдачи = @purpose,
|
|
|
|
|
Отметка = @fact,
|
|
|
|
|
Зона_доступа = @address
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
WHERE id = @id
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: status,
|
|
|
|
|
name: name,
|
|
|
|
|
date: date,
|
|
|
|
|
todate: todate,
|
|
|
|
|
legal: legal,
|
|
|
|
|
additional: additional,
|
|
|
|
|
carmodel: carmodel,
|
|
|
|
|
carbrand: carbrand,
|
|
|
|
|
carcolor: carcolor,
|
|
|
|
|
carnumber: carnumber,
|
|
|
|
|
tmcname: tmcname,
|
|
|
|
|
tmcunit: tmcunit,
|
|
|
|
|
tmcquantity: tmcquantity,
|
|
|
|
|
type: type,
|
|
|
|
|
email: email,
|
|
|
|
|
phone: phone,
|
|
|
|
|
purpose: purpose,
|
|
|
|
|
fact: JSON.stringify(factsValues),
|
|
|
|
|
address: address,
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (updateResult.length === 0) {
|
|
|
|
|
res.status(404).json({ message: "Пропуск не найден" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
message: "Данные пропуска успешно обновлены",
|
|
|
|
|
data: updateResult[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обновлении данных пропуска:", error);
|
|
|
|
|
res
|
|
|
|
|
.status(500)
|
|
|
|
|
.json({ message: "Произошла ошибка при обновлении данных" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка DELETE запроса для удаления строки с определенным ID из таблицы "Заявки"
|
|
|
|
|
app.delete("/passes/delete", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция" && req.user.role !== "Пропуска") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery("DELETE FROM [Пропуска] WHERE id = @id", {
|
|
|
|
|
id: id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({ message: "Строка успешно удалена" });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке DELETE запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка GET запроса на получение данных конкретной заявки ТСО
|
|
|
|
|
app.get("/passes/tso", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let passQuery = `
|
|
|
|
|
SELECT * FROM [Заявки_ТСО] WHERE id = @id
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(passQuery, { id });
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
data: result[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке POST запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Обработка POST на обновление данных заявки ТСО
|
|
|
|
|
app.put(
|
|
|
|
|
"/passes/tso/update",
|
|
|
|
|
upload.single("file"),
|
|
|
|
|
checkToken,
|
|
|
|
|
async (req, res) => {
|
|
|
|
|
const { type, status, object, fabula, executor, events, id } = req.body;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const formattedDate = currentDate.toISOString();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const updateResult = await executeQuery(
|
|
|
|
|
`UPDATE [Заявки_ТСО]
|
|
|
|
|
SET Состояние = @status,
|
|
|
|
|
Вид_неисправности = @type,
|
|
|
|
|
Объект = @object,
|
|
|
|
|
Фабула = @fabula,
|
|
|
|
|
Дата_изменения = @editdate,
|
|
|
|
|
Исполнитель = @executor,
|
|
|
|
|
Мероприятия = @events
|
|
|
|
|
OUTPUT inserted.*
|
|
|
|
|
WHERE id = @id
|
|
|
|
|
`,
|
|
|
|
|
{
|
|
|
|
|
status: status,
|
|
|
|
|
type: type,
|
|
|
|
|
object: object,
|
|
|
|
|
fabula: fabula,
|
|
|
|
|
editdate: formattedDate,
|
|
|
|
|
executor: executor,
|
|
|
|
|
events: events,
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (updateResult.length === 0) {
|
|
|
|
|
res.status(404).json({ message: "Заявка не найдена" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
message: "Данные заявки успешно обновлены",
|
|
|
|
|
data: updateResult[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обновлении данных пропуска:", error);
|
|
|
|
|
res
|
|
|
|
|
.status(500)
|
|
|
|
|
.json({ message: "Произошла ошибка при обновлении данных" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Обработка DELETE запроса для удаления строки с определенным ID из таблицы "Заявки_ТСО"
|
|
|
|
|
app.delete("/passes/tso/delete", checkToken, async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.query.id;
|
|
|
|
|
|
|
|
|
|
if (req.user.role !== "Дирекция") {
|
|
|
|
|
res.status(403).json({ message: "Недостаточно прав" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await executeQuery(
|
|
|
|
|
"DELETE FROM [Заявки_ТСО] WHERE id = @id",
|
|
|
|
|
{
|
|
|
|
|
id: id,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.status(200).json({ message: "Строка успешно удалена" });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Ошибка при обработке DELETE запроса:", error);
|
|
|
|
|
res.status(500).json({ message: "Произошла ошибка" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get("/secure", upload.single("file"), checkToken, (req, res) => {
|
|
|
|
|
res.json({
|
|
|
|
|
message: "Защищенный ресурс",
|
|
|
|
|
user: req.user.userId,
|
|
|
|
|
role: req.user.role,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const port = 8080;
|
|
|
|
|
app.listen(port, () => {
|
|
|
|
|
console.log(`Server is running on port ${port}`);
|
|
|
|
|
});
|