-
Notifications
You must be signed in to change notification settings - Fork 1
feat!: added final job statuses middleware handler on crud operations (MAPCO-9859) #56
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
Changes from all commits
aac6807
d42e48f
bbcad74
6f95781
a75a878
58cb039
73d8a06
352a359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { Request, Response, NextFunction } from 'express'; | ||
| import { OperationStatus } from '@map-colonies/mc-priority-queue'; | ||
| import { ConflictError } from '@map-colonies/error-types'; | ||
| import { Logger } from '@map-colonies/js-logger'; | ||
| import { container } from 'tsyringe'; | ||
| import { SERVICES } from '../constants'; | ||
| import { JobManager } from '../../jobs/models/jobManager'; | ||
|
|
||
| export const validateJobStatusMiddleware = async (req: Request<{ jobId: string }>, res: Response, next: NextFunction): Promise<void> => { | ||
| const jobManager = container.resolve(JobManager); | ||
| const logger = container.resolve<Logger>(SERVICES.LOGGER); | ||
|
|
||
| const finalStateStatuses: OperationStatus[] = [OperationStatus.COMPLETED, OperationStatus.ABORTED, OperationStatus.EXPIRED]; | ||
| try { | ||
| const jobId = req.params.jobId; | ||
| const job = await jobManager.getJob({ jobId }, { shouldReturnTasks: false, shouldReturnAvailableActions: false }); | ||
|
|
||
| if (finalStateStatuses.includes(job.status)) { | ||
| const errorMessage = `Cannot perform the requested operation on job with final state status: ${job.status}`; | ||
| logger.error({ msg: errorMessage, jobId, status: job.status }); | ||
| throw new ConflictError(errorMessage); | ||
| } | ||
|
|
||
| next(); | ||
| } catch (error) { | ||
| next(error); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| /* eslint-disable @typescript-eslint/no-misused-promises */ | ||
| import { Router } from 'express'; | ||
| import { FactoryFunction } from 'tsyringe'; | ||
| import { JobController } from '../controllers/jobController'; | ||
| import { TaskController } from '../controllers/taskController'; | ||
| import { validateJobStatusMiddleware } from '../../common/middlewares/validateJobStatusMiddleware'; | ||
|
|
||
| const jobRouterFactory: FactoryFunction<Router> = (dependencyContainer) => { | ||
| const router = Router(); | ||
|
|
@@ -13,13 +15,13 @@ const jobRouterFactory: FactoryFunction<Router> = (dependencyContainer) => { | |
| router.post('/', jobsController.createResource); | ||
| router.get('/parameters', jobsController.getJobByJobsParameters); | ||
| router.get('/:jobId', jobsController.getResource); | ||
| router.put('/:jobId', jobsController.updateResource); | ||
| router.put('/:jobId', validateJobStatusMiddleware, jobsController.updateResource); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not exactly middleware pattern midlleware can come with routes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not accurate, middlewares are also can be defined
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| router.delete('/:jobId', jobsController.deleteResource); | ||
| router.post('/:jobId/resettable', jobsController.isResettable); | ||
| router.post('/:jobId/reset', jobsController.resetJob); | ||
| router.post('/:jobId/reset', validateJobStatusMiddleware, jobsController.resetJob); | ||
|
|
||
| router.get('/:jobId/tasks', tasksController.getResources); | ||
| router.post('/:jobId/tasks', tasksController.createResource); | ||
| router.post('/:jobId/tasks', validateJobStatusMiddleware, tasksController.createResource); | ||
| router.get('/:jobId/tasks/:taskId', tasksController.getResource); | ||
| router.put('/:jobId/tasks/:taskId', tasksController.updateResource); | ||
| router.delete('/:jobId/tasks/:taskId', tasksController.deleteResource); | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth to define it in more upper level, even more FINAL states def might be reused in other mapcolonies domain(APP for instance)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed, the change should be included in reusing this middle ware below comment on
abortlogic,finalStatuseswill remain in this level as it the middle ware logic that should define itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i reused it below comment