Skip to content

Commit 3ab3fd1

Browse files
authored
Merge pull request #242 from OpenPathfinder/ulises/v1-gen-reports
2 parents 5012b92 + 2efd18c commit 3ab3fd1

File tree

3 files changed

+7
-119
lines changed

3 files changed

+7
-119
lines changed

__tests__/httpServer/apiV1.test.js

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ jest.mock('../../src/cli/workflows', () => ({
2020
}))
2121

2222
const request = require('supertest')
23-
const { generateStaticReports } = require('../../src/reports')
2423
const knexInit = require('knex')
2524
const { getConfig } = require('../../src/config')
2625
const { resetDatabase, initializeStore } = require('../../__utils__')
@@ -183,7 +182,8 @@ describe('HTTP Server API V1', () => {
183182
id: 'test-workflow',
184183
description: 'Test workflow'
185184
})
186-
expect(mockWorkflowFn).toHaveBeenCalledWith({ some: 'data' })
185+
// The first argument (...calls[0][0]) is Knex and we ignore it due framework limitations
186+
expect(mockWorkflowFn.mock.calls[0][1]).toEqual({ some: 'data' })
187187
})
188188

189189
test('should return 404 for invalid workflow ID', async () => {
@@ -216,47 +216,10 @@ describe('HTTP Server API V1', () => {
216216
description: 'Test workflow'
217217
})
218218
expect(response.body.errors[0].message).toMatch(/Failed to run workflow: Something went wrong/)
219-
expect(mockWorkflowFn).toHaveBeenCalledWith({ some: 'data' })
219+
// The first argument (...calls[0][0]) is Knex and we ignore it due framework limitations
220+
expect(mockWorkflowFn.mock.calls[0][1]).toEqual({ some: 'data' })
220221
})
221222

222223
test.todo('should return 500 when workflow execution times out')
223224
})
224-
225-
describe('POST /api/v1/generate-reports', () => {
226-
test('should return status completed when report generation succeeds', async () => {
227-
generateStaticReports.mockResolvedValueOnce()
228-
229-
const response = await app.post('/api/v1/generate-reports')
230-
231-
expect(generateStaticReports).toHaveBeenCalledWith(expect.anything(), { clearPreviousReports: true })
232-
expect(response.status).toBe(202)
233-
expect(response.body).toHaveProperty('status', 'completed')
234-
expect(response.body).toHaveProperty('startedAt')
235-
expect(response.body).toHaveProperty('finishedAt')
236-
237-
const startedAt = new Date(response.body.startedAt)
238-
const finishedAt = new Date(response.body.finishedAt)
239-
expect(startedAt.toISOString()).toBe(response.body.startedAt)
240-
expect(finishedAt.toISOString()).toBe(response.body.finishedAt)
241-
expect(finishedAt.getTime()).toBeGreaterThanOrEqual(startedAt.getTime())
242-
})
243-
244-
test('should return status failed when report generation fails', async () => {
245-
generateStaticReports.mockRejectedValueOnce(new Error('Report generation failed'))
246-
247-
const response = await app.post('/api/v1/generate-reports')
248-
249-
expect(generateStaticReports).toHaveBeenCalledWith(expect.anything(), { clearPreviousReports: true })
250-
expect(response.status).toBe(500)
251-
expect(response.body).toHaveProperty('status', 'failed')
252-
expect(response.body).toHaveProperty('startedAt')
253-
expect(response.body).toHaveProperty('finishedAt')
254-
255-
const startedAt = new Date(response.body.startedAt)
256-
const finishedAt = new Date(response.body.finishedAt)
257-
expect(startedAt.toISOString()).toBe(response.body.startedAt)
258-
expect(finishedAt.toISOString()).toBe(response.body.finishedAt)
259-
expect(finishedAt.getTime()).toBeGreaterThanOrEqual(startedAt.getTime())
260-
})
261-
})
262225
})

src/httpServer/routers/apiV1.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const { generateStaticReports } = require('../../reports')
21
const pkg = require('../../../package.json')
32
const { logger } = require('../../utils')
43
const { initializeStore } = require('../../store')
@@ -8,7 +7,7 @@ const { getWorkflowsDetails } = require('../../cli/workflows')
87

98
const HTTP_DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
109

11-
const runWorkflow = (workflowName, data) => new Promise((resolve, reject) => {
10+
const runWorkflow = ({ workflowName, knex, data } = {}) => new Promise((resolve, reject) => {
1211
const { workflows } = getWorkflowsDetails()
1312
const workflow = workflows[workflowName]
1413
if (!workflow || typeof workflow.workflow !== 'function') {
@@ -21,7 +20,7 @@ const runWorkflow = (workflowName, data) => new Promise((resolve, reject) => {
2120
}, HTTP_DEFAULT_TIMEOUT)
2221

2322
Promise.resolve()
24-
.then(() => workflow.workflow(data))
23+
.then(() => workflow.workflow(knex, data))
2524
.then(() => resolve(workflow))
2625
.catch(err => reject(new Error(`Failed to run workflow: ${err.message}`)))
2726
.finally(() => clearTimeout(timeout))
@@ -91,7 +90,7 @@ function createApiRouter (knex, express) {
9190
// @TODO: We need to delegate the workflow execution to a worker and provide and endpoint to check the status
9291
// This is a temporary solution to run the workflow within the HTTP timeout
9392
// data validation is done in the workflow itself
94-
const wf = await runWorkflow(id, data)
93+
const wf = await runWorkflow({ workflowName: id, knex, data })
9594
res.status(202).json({ status: 'completed', workflow: { id, description: wf.description } })
9695
} catch (error) {
9796
logger.error(error)
@@ -105,24 +104,6 @@ function createApiRouter (knex, express) {
105104
}
106105
})
107106

108-
router.post('/generate-reports', async (req, res) => {
109-
const startTs = new Date().toISOString()
110-
try {
111-
await generateStaticReports(knex, { clearPreviousReports: true })
112-
res.status(202).json({
113-
status: 'completed',
114-
startedAt: startTs,
115-
finishedAt: new Date().toISOString()
116-
})
117-
} catch (error) {
118-
logger.error(error)
119-
res.status(500).json({
120-
status: 'failed',
121-
startedAt: startTs,
122-
finishedAt: new Date().toISOString()
123-
})
124-
}
125-
})
126107
return router
127108
}
128109

src/httpServer/swagger/api-v1.yml

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -197,62 +197,6 @@ paths:
197197
application/json:
198198
schema:
199199
$ref: '#/components/schemas/ErrorResponse'
200-
/api/v1/generate-reports:
201-
post:
202-
summary: Generate static reports
203-
description: Triggers the generation of static reports
204-
operationId: generateReports
205-
tags:
206-
- Reports
207-
responses:
208-
'202':
209-
description: Report generation accepted
210-
content:
211-
application/json:
212-
schema:
213-
type: object
214-
additionalProperties: false
215-
properties:
216-
status:
217-
type: string
218-
enum: [completed, failed]
219-
example: completed
220-
startedAt:
221-
type: string
222-
format: date-time
223-
example: '2025-05-03T07:20:16.000Z'
224-
finishedAt:
225-
type: string
226-
format: date-time
227-
example: '2025-05-03T07:20:20.000Z'
228-
required:
229-
- status
230-
- startedAt
231-
- finishedAt
232-
'500':
233-
description: Report generation failed
234-
content:
235-
application/json:
236-
schema:
237-
type: object
238-
additionalProperties: false
239-
properties:
240-
status:
241-
type: string
242-
enum: [failed]
243-
example: failed
244-
startedAt:
245-
type: string
246-
format: date-time
247-
example: '2025-05-03T07:20:16.000Z'
248-
finishedAt:
249-
type: string
250-
format: date-time
251-
example: '2025-05-03T07:20:20.000Z'
252-
required:
253-
- status
254-
- startedAt
255-
- finishedAt
256200

257201
components:
258202
schemas:

0 commit comments

Comments
 (0)