2024-11-05 07:10:24 -08:00
|
|
|
import { Get, Post, Patch, RestController, Delete } from '@/decorators';
|
|
|
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
|
|
|
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
2024-11-04 09:51:27 -08:00
|
|
|
import { listQueryMiddleware } from '@/middlewares';
|
2024-11-05 07:10:24 -08:00
|
|
|
import { getSharedWorkflowIds } from '@/public-api/v1/handlers/workflows/workflows.service';
|
|
|
|
import { isPositiveInteger } from '@/utils';
|
2024-11-01 08:13:31 -07:00
|
|
|
|
2024-11-06 04:45:16 -08:00
|
|
|
import { TestDefinitionsService } from './test-definitions.service';
|
|
|
|
import { TestDefinitionsRequest } from './test-definitions.types';
|
2024-11-01 08:13:31 -07:00
|
|
|
|
2024-11-06 04:45:16 -08:00
|
|
|
@RestController('/evaluation/test-definitions')
|
|
|
|
export class TestDefinitionsController {
|
|
|
|
constructor(private readonly testsService: TestDefinitionsService) {}
|
2024-11-01 08:13:31 -07:00
|
|
|
|
2024-11-04 09:51:27 -08:00
|
|
|
@Get('/', { middlewares: listQueryMiddleware })
|
2024-11-06 04:45:16 -08:00
|
|
|
async getMany(req: TestDefinitionsRequest.GetMany) {
|
2024-11-05 07:10:24 -08:00
|
|
|
const workflowIds = await getSharedWorkflowIds(req.user, ['workflow:read']);
|
|
|
|
|
2024-11-06 03:28:23 -08:00
|
|
|
return await this.testsService.getMany(req.listQueryOptions, workflowIds);
|
2024-11-05 07:10:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Get('/:id')
|
2024-11-06 04:45:16 -08:00
|
|
|
async getOne(req: TestDefinitionsRequest.GetOne) {
|
2024-11-05 07:10:24 -08:00
|
|
|
if (!isPositiveInteger(req.params.id)) {
|
|
|
|
throw new BadRequestError('Test ID is not a number');
|
|
|
|
}
|
|
|
|
|
|
|
|
const workflowIds = await getSharedWorkflowIds(req.user, ['workflow:read']);
|
|
|
|
|
|
|
|
return await this.testsService.findOne(Number(req.params.id), workflowIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Post('/')
|
2024-11-06 04:45:16 -08:00
|
|
|
async create(req: TestDefinitionsRequest.Create) {
|
2024-11-05 07:10:24 -08:00
|
|
|
const workflowIds = await getSharedWorkflowIds(req.user, ['workflow:read']);
|
|
|
|
|
|
|
|
if (!workflowIds.includes(req.body.workflowId)) {
|
|
|
|
throw new BadRequestError('User does not have access to the workflow');
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.testsService.save(this.testsService.toEntity(req.body));
|
2024-11-01 08:13:31 -07:00
|
|
|
}
|
|
|
|
|
2024-11-05 07:10:24 -08:00
|
|
|
@Delete('/:id')
|
2024-11-06 04:45:16 -08:00
|
|
|
async delete(req: TestDefinitionsRequest.Delete) {
|
2024-11-05 07:10:24 -08:00
|
|
|
if (!isPositiveInteger(req.params.id)) {
|
|
|
|
throw new BadRequestError('Test ID is not a number');
|
|
|
|
}
|
|
|
|
|
|
|
|
const workflowIds = await getSharedWorkflowIds(req.user, ['workflow:read']);
|
|
|
|
|
|
|
|
if (workflowIds.length === 0) throw new NotFoundError('Test not found');
|
|
|
|
|
|
|
|
return await this.testsService.delete(Number(req.params.id), workflowIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Patch('/:id')
|
2024-11-06 04:45:16 -08:00
|
|
|
async update(req: TestDefinitionsRequest.Update) {
|
2024-11-05 07:10:24 -08:00
|
|
|
if (!isPositiveInteger(req.params.id)) {
|
|
|
|
throw new BadRequestError('Test ID is not a number');
|
|
|
|
}
|
|
|
|
|
|
|
|
const workflowIds = await getSharedWorkflowIds(req.user, ['workflow:read']);
|
|
|
|
|
|
|
|
// Fail fast if no workflows are accessible
|
|
|
|
if (workflowIds.length === 0) throw new NotFoundError('Workflow not found');
|
|
|
|
|
|
|
|
return await this.testsService.save(
|
|
|
|
this.testsService.toEntity({ ...req.body, id: Number(req.params.id) }),
|
|
|
|
);
|
|
|
|
}
|
2024-11-01 08:13:31 -07:00
|
|
|
}
|