wip: evaluation/tests endpoint

This commit is contained in:
Eugene Molodkin 2024-11-04 18:51:27 +01:00 committed by Oleg Ivaniv
parent 3dc9a6eb1d
commit 961d42fd51
No known key found for this signature in database
3 changed files with 64 additions and 18 deletions

View file

@ -1,11 +1,59 @@
import { DataSource, Repository } from '@n8n/typeorm';
import type { FindManyOptions, FindOptionsSelect, FindOptionsWhere } from '@n8n/typeorm';
import { DataSource, In, Repository } from '@n8n/typeorm';
import { Service } from 'typedi';
import { TestEntity } from '@/databases/entities/test-entity';
import type { ListQuery } from '@/requests';
@Service()
export class TestRepository extends Repository<TestEntity> {
constructor(dataSource: DataSource) {
super(TestEntity, dataSource.manager);
}
async getMany(sharedWorkflowIds: string[], options?: ListQuery.Options) {
if (sharedWorkflowIds.length === 0) return { tests: [], count: 0 };
const where: FindOptionsWhere<TestEntity> = {
...options?.filter,
workflow: {
id: In(sharedWorkflowIds),
},
};
type Select = FindOptionsSelect<TestEntity>;
const select: Select = {
id: true,
name: true,
createdAt: true,
updatedAt: true,
};
const relations: string[] = ['workflow', 'evaluationWorkflow', 'annotationTag'];
const isDefaultSelect = options?.select === undefined;
const findManyOptions: FindManyOptions<TestEntity> = {
select: { ...select, id: true },
where,
};
if (isDefaultSelect || options?.select?.updatedAt === true) {
findManyOptions.order = { updatedAt: 'ASC' };
}
if (relations.length > 0) {
findManyOptions.relations = relations;
}
if (options?.take) {
findManyOptions.skip = options.skip;
findManyOptions.take = options.take;
}
const [tests, count] = await this.findAndCount(findManyOptions);
return { tests, count };
}
}

View file

@ -1,7 +1,5 @@
// import type { Scope } from '@n8n/permissions';
// import type { User } from '@/databases/entities/user';
import { Get, /*Patch, Post,*/ RestController } from '@/decorators';
import { listQueryMiddleware } from '@/middlewares';
import { TestsService } from './tests.service';
import { TestsRequest } from './tests.types';
@ -21,13 +19,9 @@ export class TestsController {
// }
// }
@Get('/')
async getMany(_req: TestsRequest.GetMany) {
return await this.testsService.getMany(
/*req.user, {
listQueryOptions: req.listQueryOptions,
}*/
);
@Get('/', { middlewares: listQueryMiddleware })
async getMany(req: TestsRequest.GetMany) {
return await this.testsService.getMany(req.user, req.listQueryOptions);
}
// @Get('/:id')

View file

@ -1,14 +1,18 @@
import { Service } from 'typedi';
import type { TestEntity } from '@/databases/entities/test-entity';
// import type { User } from '@/databases/entities/user';
import type { User } from '@/databases/entities/user';
import { TestRepository } from '@/databases/repositories/test.repository';
import { validateEntity } from '@/generic-helpers';
// import type { ListQuery } from '@/requests';
import type { ListQuery } from '@/requests';
import { WorkflowSharingService } from '@/workflows/workflow-sharing.service';
@Service()
export class TestsService {
constructor(private testRepository: TestRepository) {}
constructor(
private testRepository: TestRepository,
private readonly workflowSharingService: WorkflowSharingService,
) {}
// toEntity(attrs: { name: string; id?: string }) {
// attrs.name = attrs.name.trim();
@ -26,11 +30,11 @@ export class TestsService {
return await this.testRepository.delete(id);
}
async getMany(/*user: User, options: ListQuery.Options*/) {
const allTests = await this.testRepository.find({
select: ['id', 'name', 'createdAt', 'updatedAt'],
async getMany(user: User, options: ListQuery.Options) {
const sharedWorkflowIds = await this.workflowSharingService.getSharedWorkflowIds(user, {
scopes: ['workflow:read'],
});
return { allTests, count: allTests.length };
return await this.testRepository.getMany(sharedWorkflowIds, options);
}
}