mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 00:54:06 -08:00
wip: evaluation/tests endpoint
This commit is contained in:
parent
3dc9a6eb1d
commit
961d42fd51
|
@ -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 };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue