Refactor API response structure to include success status and body for all endpoints, ensuring consistent response format across the application.
Some checks failed
platform/bro-js/challenge-pl/pipeline/head There was a failure building this commit

This commit is contained in:
Primakov Alexandr Alexandrovich 2025-11-04 11:18:13 +03:00
parent 67f29c103a
commit cde28b1dd8
9 changed files with 294 additions and 265 deletions

View File

@ -1,4 +1,7 @@
{ {
"success": true,
"body": {
"ok": true, "ok": true,
"userId": "user-frontend-001" "userId": "user-frontend-001"
} }
}

View File

@ -1,4 +1,6 @@
[ {
"success": true,
"body": [
{ {
"id": "chain-frontend", "id": "chain-frontend",
"_id": "chain-frontend", "_id": "chain-frontend",
@ -25,3 +27,4 @@
] ]
} }
] ]
}

View File

@ -1,4 +1,6 @@
{ {
"success": true,
"body": {
"queue-frontend-001": { "queue-frontend-001": {
"status": "completed", "status": "completed",
"position": 0, "position": 0,
@ -55,3 +57,4 @@
} }
} }
} }
}

View File

@ -1,4 +1,6 @@
[ {
"success": true,
"body": [
{ {
"_id": "submission-001", "_id": "submission-001",
"id": "submission-001", "id": "submission-001",
@ -57,3 +59,4 @@
"attemptNumber": 1 "attemptNumber": 1
} }
] ]
}

View File

@ -1,4 +1,7 @@
{ {
"success": true,
"body": {
"queueId": "queue-frontend-003", "queueId": "queue-frontend-003",
"submissionId": "submission-003" "submissionId": "submission-003"
} }
}

View File

@ -1,4 +1,6 @@
{ {
"success": true,
"body": {
"users": 128, "users": 128,
"tasks": 34, "tasks": 34,
"chains": 5, "chains": 5,
@ -18,3 +20,4 @@
"currentlyProcessing": 6 "currentlyProcessing": 6
} }
} }
}

View File

@ -1,4 +1,6 @@
{ {
"success": true,
"body": {
"user-frontend-001": { "user-frontend-001": {
"totalTasksAttempted": 2, "totalTasksAttempted": 2,
"completedTasks": 1, "completedTasks": 1,
@ -57,3 +59,4 @@
] ]
} }
} }
}

View File

@ -1,4 +1,6 @@
{ {
"success": true,
"body": {
"user-frontend-001": [ "user-frontend-001": [
{ {
"_id": "submission-001", "_id": "submission-001",
@ -41,3 +43,4 @@
} }
] ]
} }
}

View File

@ -32,16 +32,18 @@ router.get('/challenge/chains', (req, res) => {
}) })
router.get('/challenge/chain/:id', (req, res) => { router.get('/challenge/chain/:id', (req, res) => {
const chains = readJson('chains.json') const data = readJson('chains.json')
const chains = data.body || data
const chain = chains.find((item) => item.id === req.params.id || item._id === req.params.id) const chain = chains.find((item) => item.id === req.params.id || item._id === req.params.id)
if (!chain) { if (!chain) {
return sendNotFound(res, `Цепочка ${req.params.id} не найдена`) return sendNotFound(res, `Цепочка ${req.params.id} не найдена`)
} }
return res.json(chain) return res.json({ success: true, body: chain })
}) })
router.get('/challenge/task/:id', (req, res) => { router.get('/challenge/task/:id', (req, res) => {
const chains = readJson('chains.json') const data = readJson('chains.json')
const chains = data.body || data
const task = chains const task = chains
.flatMap((chain) => chain.tasks || []) .flatMap((chain) => chain.tasks || [])
.find((item) => item.id === req.params.id || item._id === req.params.id) .find((item) => item.id === req.params.id || item._id === req.params.id)
@ -50,7 +52,7 @@ router.get('/challenge/task/:id', (req, res) => {
return sendNotFound(res, `Задание ${req.params.id} не найдено`) return sendNotFound(res, `Задание ${req.params.id} не найдено`)
} }
return res.json(task) return res.json({ success: true, body: task })
}) })
router.post('/challenge/submit', (req, res) => { router.post('/challenge/submit', (req, res) => {
@ -59,31 +61,34 @@ router.post('/challenge/submit', (req, res) => {
}) })
router.get('/challenge/check-status/:queueId', (req, res) => { router.get('/challenge/check-status/:queueId', (req, res) => {
const statuses = readJson('queue-status.json') const data = readJson('queue-status.json')
const statuses = data.body || data
const status = statuses[req.params.queueId] const status = statuses[req.params.queueId]
if (!status) { if (!status) {
return sendNotFound(res, `Статус очереди ${req.params.queueId} не найден`) return sendNotFound(res, `Статус очереди ${req.params.queueId} не найден`)
} }
return res.json(status) return res.json({ success: true, body: status })
}) })
router.get('/challenge/user/:userId/stats', (req, res) => { router.get('/challenge/user/:userId/stats', (req, res) => {
const statsMap = readJson('user-stats.json') const data = readJson('user-stats.json')
const statsMap = data.body || data
const stats = statsMap[req.params.userId] const stats = statsMap[req.params.userId]
if (!stats) { if (!stats) {
return sendNotFound(res, `Статистика пользователя ${req.params.userId} не найдена`) return sendNotFound(res, `Статистика пользователя ${req.params.userId} не найдена`)
} }
return res.json(stats) return res.json({ success: true, body: stats })
}) })
router.get('/challenge/user/:userId/submissions', (req, res) => { router.get('/challenge/user/:userId/submissions', (req, res) => {
const submissionsMap = readJson('user-submissions.json') const data = readJson('user-submissions.json')
const submissionsMap = data.body || data
const submissions = submissionsMap[req.params.userId] || [] const submissions = submissionsMap[req.params.userId] || []
return res.json(submissions) return res.json({ success: true, body: submissions })
}) })
router.get('/challenge/stats', (req, res) => { router.get('/challenge/stats', (req, res) => {