-
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add endpoint POST /api/v1/generate-reports #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1a20f7c
chore: rename function to `generateStaticReports`
UlisesGascon e3648e0
feat: add option to clear previous static reports
UlisesGascon b370906
feat: add endpoint GET /api/v1/generate-reports
UlisesGascon d8ef118
test: add tests for endpoint GET /api/v1/generate-reports
UlisesGascon a626838
chore: relocate API v1 router
UlisesGascon 07dd888
feat: add a util to check database connection state
UlisesGascon 26b69cf
feat: improve server initialization
UlisesGascon db903b7
chore: convert `/api/v1/generate-reports` endpoint to POST method
UlisesGascon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,81 @@ | ||
| const request = require('supertest') | ||
| const { generateStaticReports } = require('../src/reports') | ||
| const serverModule = require('../src/httpServer') | ||
| const server = serverModule() | ||
| const app = request(server) | ||
| let server | ||
| let serverStop | ||
| let app | ||
|
|
||
| // Cleanup after all tests | ||
| afterAll(() => { | ||
| server?.close() | ||
| // Mocks | ||
| jest.mock('../src/reports', () => ({ | ||
| generateStaticReports: jest.fn() | ||
| })) | ||
|
|
||
| beforeAll(async () => { | ||
| // Initialize server asynchronously | ||
| const serverInstance = serverModule() | ||
| server = await serverInstance.start() | ||
| serverStop = serverInstance.stop | ||
| app = request(server) | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| // Cleanup after all tests | ||
| await serverStop?.() | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| describe('HTTP Server API', () => { | ||
| test('health check endpoint should return status ok', async () => { | ||
| const response = await app.get('/api/v1/__health') | ||
| describe('GET /api/v1/__health', () => { | ||
| test('should return status ok', async () => { | ||
| const response = await app.get('/api/v1/__health') | ||
|
|
||
| expect(response.status).toBe(200) | ||
| expect(response.body).toHaveProperty('status', 'ok') | ||
| expect(response.body).toHaveProperty('timestamp') | ||
| expect(response.status).toBe(200) | ||
| expect(response.body).toHaveProperty('status', 'ok') | ||
| expect(response.body).toHaveProperty('timestamp') | ||
|
|
||
| const timestamp = new Date(response.body.timestamp) | ||
| expect(timestamp.toISOString()).toBe(response.body.timestamp) | ||
| const timestamp = new Date(response.body.timestamp) | ||
| expect(timestamp.toISOString()).toBe(response.body.timestamp) | ||
| }) | ||
| }) | ||
|
|
||
| test('non-existent API endpoint should return 404', async () => { | ||
| const response = await app.get('/api/v1/non-existent-endpoint') | ||
| describe('POST /api/v1/generate-reports', () => { | ||
| test('should return status completed when report generation succeeds', async () => { | ||
| generateStaticReports.mockResolvedValueOnce() | ||
|
|
||
| const response = await app.post('/api/v1/generate-reports') | ||
|
|
||
| expect(generateStaticReports).toHaveBeenCalledWith(expect.anything(), { clearPreviousReports: true }) | ||
| expect(response.status).toBe(202) | ||
| expect(response.body).toHaveProperty('status', 'completed') | ||
| expect(response.body).toHaveProperty('startedAt') | ||
| expect(response.body).toHaveProperty('finishedAt') | ||
|
|
||
| const startedAt = new Date(response.body.startedAt) | ||
| const finishedAt = new Date(response.body.finishedAt) | ||
| expect(startedAt.toISOString()).toBe(response.body.startedAt) | ||
| expect(finishedAt.toISOString()).toBe(response.body.finishedAt) | ||
| expect(finishedAt >= startedAt).toBe(true) | ||
| }) | ||
|
|
||
| test('should return status failed when report generation fails', async () => { | ||
| generateStaticReports.mockRejectedValueOnce(new Error('Report generation failed')) | ||
|
|
||
| const response = await app.post('/api/v1/generate-reports') | ||
|
|
||
| expect(generateStaticReports).toHaveBeenCalledWith(expect.anything(), { clearPreviousReports: true }) | ||
| expect(response.status).toBe(500) | ||
| expect(response.body).toHaveProperty('status', 'failed') | ||
| expect(response.body).toHaveProperty('startedAt') | ||
| expect(response.body).toHaveProperty('finishedAt') | ||
|
|
||
| expect(response.status).toBe(404) | ||
| const startedAt = new Date(response.body.startedAt) | ||
| const finishedAt = new Date(response.body.finishedAt) | ||
| expect(startedAt.toISOString()).toBe(response.body.startedAt) | ||
| expect(finishedAt.toISOString()).toBe(response.body.finishedAt) | ||
| expect(finishedAt >= startedAt).toBe(true) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| const server = require('./src/httpServer') | ||
| const server = require('./src/httpServer'); | ||
|
|
||
| server() | ||
| (async () => { | ||
| const serverInstance = server() | ||
| await serverInstance.start() | ||
| })() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| const { generateStaticReports } = require('../reports') | ||
| const { logger } = require('../utils') | ||
|
|
||
| function createApiRouter (knex, express) { | ||
| const router = express.Router() | ||
|
|
||
| router.get('/__health', (req, res) => { | ||
| res.json({ status: 'ok', timestamp: new Date().toISOString() }) | ||
| }) | ||
|
|
||
| router.post('/generate-reports', async (req, res) => { | ||
| const startTs = new Date().toISOString() | ||
| try { | ||
| await generateStaticReports(knex, { clearPreviousReports: true }) | ||
| res.status(202).json({ | ||
| status: 'completed', | ||
| startedAt: startTs, | ||
| finishedAt: new Date().toISOString() | ||
| }) | ||
| } catch (error) { | ||
| logger.error(error) | ||
| res.status(500).json({ | ||
| status: 'failed', | ||
| startedAt: startTs, | ||
| finishedAt: new Date().toISOString() | ||
| }) | ||
| } | ||
| }) | ||
| return router | ||
| } | ||
|
|
||
| module.exports = { createApiRouter } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.