Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
add single task endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybrink committed Jun 11, 2024
1 parent fb5c6f6 commit c6ce1b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
18 changes: 17 additions & 1 deletion backend/src/app/tasks/task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 10 in backend/src/app/tasks/task.controller.ts

View workflow job for this annotation

GitHub Actions / verify

'response' is defined but never used

@Controller('task')
export class TaskController {
Expand Down Expand Up @@ -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());
}
}
17 changes: 16 additions & 1 deletion backend/src/app/tasks/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task | undefined> {
const task = this.availableTasks[taskId];

if (!task) {
return undefined;
}

const log = await this.taskRepository.getTaskLog(user, taskId);

return new task(log?.status ?? 'not started');

Check failure

Code scanning / CodeQL

Unvalidated dynamic method call High

Invocation of method with
user-controlled
name may dispatch to unexpected target and cause an exception.
Invocation of method with user-controlled name may dispatch to unexpected target and cause an exception.
}

public async startTask(user: string, task: string): Promise<TaskLog> {
// Only one concurrent task should be allowed
const startedTasks = await this.taskRepository.getStartedTasksForUser(user);
Expand Down

0 comments on commit c6ce1b3

Please sign in to comment.