feat: REST API v1 (Bearer JWT, CRUD entries + admin companies)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
const express = require('express');
|
||||
|
||||
// Отдельный requireAdmin для API — возвращает JSON вместо plain text
|
||||
function apiRequireAdmin(req, res, next) {
|
||||
if (!req.user || !req.user.isAdmin) {
|
||||
return res.status(403).json({ error: 'Admin role required' });
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
function createAdminRouter({ q }) {
|
||||
const router = express.Router();
|
||||
const json = express.json({ limit: '8kb' });
|
||||
|
||||
// GET /api/v1/companies — все компании (admin only)
|
||||
router.get('/companies', apiRequireAdmin, async (req, res) => {
|
||||
try {
|
||||
const companies = await q.getAllCompanies();
|
||||
res.json({ companies });
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// PATCH /api/v1/companies/:id/limit — установить лимит (admin only)
|
||||
router.patch('/companies/:id/limit', apiRequireAdmin, json, async (req, res) => {
|
||||
const limit = parseInt((req.body || {}).limit, 10);
|
||||
if (isNaN(limit) || limit < 0) {
|
||||
return res.status(400).json({ error: 'limit must be non-negative integer' });
|
||||
}
|
||||
try {
|
||||
await q.setLimit(req.params.id, limit);
|
||||
res.json({ ok: true, limit });
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
module.exports = { createAdminRouter };
|
||||
Reference in New Issue
Block a user