ntp&modercontent_front

This commit is contained in:
2026-08-10 23:29:34 +03:00
parent e52bfc8f84
commit 204e6a37ed
13 changed files with 628 additions and 192 deletions
+12 -6
View File
@@ -21,7 +21,7 @@ async function readJson(filePath) {
/**
* Получение mock-данных сотрудника из config.json
*/
async function getMockEmployee(searchParam, type = 'card') {
async function getMockEmployee(searchParam) {
try {
const configPath = path.join(__dirname, '../../config.json');
const config = await readJson(configPath);
@@ -31,14 +31,20 @@ async function getMockEmployee(searchParam, type = 'card') {
}
const mockEmployees = config.mockData.employees || [];
const paramStr = String(searchParam);
if (type === 'card') {
return mockEmployees.find(e => String(e.cardNumber) === String(searchParam)) || null;
} else if (type === 'id') {
return mockEmployees.find(e => e.id === parseInt(searchParam)) || null;
// Сначала пробуем найти по cardNumber (как строка, без приведения)
let employee = mockEmployees.find(e => String(e.cardNumber) === paramStr);
// Если не нашли — пробуем по id (как число)
if (!employee) {
const paramNum = parseInt(paramStr);
if (!isNaN(paramNum)) {
employee = mockEmployees.find(e => e.id === paramNum);
}
}
return null;
return employee || null;
} catch (err) {
logger.error('[MOCK ERROR]', { error: err.message });
return null;
+34 -3
View File
@@ -6,7 +6,13 @@ const { connectDB } = require('./config/db');
const { logger } = require('./config/logger');
const handbookRoutes = require('./routes/handbook');
const employeeRoutes = require('./routes/employee');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
require('dotenv').config({ path: path.join(__dirname, '../config.env') });
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
});
async function readJson(filePath) {
const data = await fs.readFile(filePath, 'utf8');
@@ -58,11 +64,20 @@ async function connectToNetworkShare(config) {
const app = express();
app.use(cors());
app.use(express.json());
app.use((req, res, next) => {
if (req.method === 'POST' || req.method === 'PUT') {
if (!req.is('application/json')) {
return res.status(415).json({ message: 'Only JSON allowed' });
}
}
next();
});
// ============================================
// 📍 Маршруты API
// ============================================
app.use(helmet());
app.use('/api/', limiter);
app.use('/api/handbook', handbookRoutes);
app.use('/api/employee', employeeRoutes);
@@ -320,7 +335,12 @@ app.get('/api/db-status', async (req, res) => {
res.status(500).json({ status: 'error', message: 'DB connection failed' });
}
});
app.get('/api/time', (req, res) => {
res.json({
time: new Date().toISOString(),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
});
});
async function startServer() {
try {
const configPath = path.join(__dirname, '../config.json');
@@ -328,6 +348,14 @@ async function startServer() {
const { host, port } = config.server;
await connectDB();
app.get('/api/time', (req, res) => {
const now = new Date();
logger.info(`[TIME] Запрос времени от ${req.ip || req.connection.remoteAddress}`);
res.json({
time: now.toISOString(),
timestamp: now.getTime()
});
});
app.listen(port, host, () => {
logger.info(`Server is running on http://${host}:${port}`);
});
@@ -339,5 +367,8 @@ async function startServer() {
}
}
}
app.use((err, req, res, next) => {
logger.error('Unhandled error', { error: err.message, stack: err.stack });
res.status(500).json({ message: 'Внутренняя ошибка сервера' });
});
startServer();