diff --git a/backend/src/app/tasks/task.controller.ts b/backend/src/app/tasks/task.controller.ts index a514f88..b1ba469 100644 --- a/backend/src/app/tasks/task.controller.ts +++ b/backend/src/app/tasks/task.controller.ts @@ -7,7 +7,7 @@ import { } from './task.service'; import { AutoGuard } from '../../auth/auto.guard'; import { NestRequest } from '../../types/request.type'; -import { Response } from 'express'; +import { Response, response } from 'express'; @Controller('task') export class TaskController { @@ -50,4 +50,20 @@ export class TaskController { public async stopTask(@Req() req: NestRequest, @Param('id') id: string) { return await this.taskService.stopTask(req.user.id, id); } + + @Get('/:id') + @UseGuards(AutoGuard) + public async getTask( + @Req() req: NestRequest, + @Param('id') id: string, + @Res() response: Response, + ) { + const task = await this.taskService.getTask(req.user.id, id); + + if (!task) { + return response.status(404).json({ error: 'Task not found' }); + } + + return response.status(200).json(task.getInfo()); + } } diff --git a/backend/src/app/tasks/task.service.ts b/backend/src/app/tasks/task.service.ts index 6120088..946d068 100644 --- a/backend/src/app/tasks/task.service.ts +++ b/backend/src/app/tasks/task.service.ts @@ -41,13 +41,28 @@ export class TaskService { const tasksWithLogs = Object.keys(this.availableTasks).map((t) => { return new this.availableTasks[t]( - this.getLogForTask(logs, t)?.status || 'unknown', + this.getLogForTask(logs, t)?.status || 'not started', ); }); return tasksWithLogs; } + public async getTask( + user: string, + taskId: string, + ): Promise { + const task = this.availableTasks[taskId]; + + if (!task) { + return undefined; + } + + const log = await this.taskRepository.getTaskLog(user, taskId); + + return new task(log?.status ?? 'not started'); + } + public async startTask(user: string, task: string): Promise { // Only one concurrent task should be allowed const startedTasks = await this.taskRepository.getStartedTasksForUser(user);