Files
vue-pult/server/api/search-employees.js
sasha 3e90269b0b Initial commit: Vue.js тир управления система
- Клиентская часть Vue 3 + Vite
- Серверная часть Node.js + WebSocket
- Система авторизации и смен
- Управление игровыми портами
- Поддержка тем (светлая/темная)
- Адаптивный дизайн

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-19 12:24:22 +03:00

104 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
const fs = require('node:fs/promises');
const path = require('path');
exports.go = async (req, res, postData, urlParsed) => {
try {
console.log('[api/search-employees] Запрос поиска сотрудников:', {
query: postData.query,
limit: postData.limit || 10
});
// Читаем базу данных администраторов
let avt = {};
try {
const avtData = await fs.readFile(path.join(__dirname, '../data/avt.ini'), 'utf8');
avt = JSON.parse(avtData);
} catch (error) {
console.error('[api/search-employees] Ошибка чтения avt.ini:', error);
return res.end(JSON.stringify({
success: false,
error: "Ошибка загрузки данных сотрудников"
}));
}
const query = (postData.query || '').toLowerCase().trim();
const limit = Math.min(postData.limit || 10, 50); // Максимум 50 результатов
let results = [];
if (query.length > 0) {
// Поиск по базе данных
for (let key in avt) {
const employee = avt[key];
let matched = false;
// Поиск по ID
if (employee._id && employee._id.toString().includes(query)) {
matched = true;
}
// Поиск по ФИО
if (employee.fio && employee.fio.toLowerCase().includes(query)) {
matched = true;
}
// Поиск по телефону
if (employee.tel && employee.tel.toString().includes(query)) {
matched = true;
}
if (matched) {
results.push({
id: employee._id,
name: employee.fio || `Администратор ${employee._id}`,
phone: employee.tel,
group: employee.groupz ? employee.groupz.name : null
});
// Ограничиваем количество результатов
if (results.length >= limit) {
break;
}
}
}
} else {
// Если запрос пустой, возвращаем всех (с лимитом)
let count = 0;
for (let key in avt) {
const employee = avt[key];
results.push({
id: employee._id,
name: employee.fio || `Администратор ${employee._id}`,
phone: employee.tel,
group: employee.groupz ? employee.groupz.name : null
});
count++;
if (count >= limit) {
break;
}
}
}
// Сортируем по ФИО
results.sort((a, b) => (a.name || '').localeCompare(b.name || ''));
console.log('[api/search-employees] Найдено сотрудников:', results.length);
res.end(JSON.stringify({
success: true,
query: postData.query,
results: results,
total: results.length
}));
} catch (error) {
console.error('[api/search-employees] Критическая ошибка:', error);
res.end(JSON.stringify({
success: false,
error: "Внутренняя ошибка сервера"
}));
}
};