refactor(core): Switch Tags queries from QueryBuilder to Repository API (no-changelog) (#5819)

Co-authored-by: Omar Ajoue <krynble@gmail.com>
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-03-30 16:25:51 +02:00
committed by GitHub
parent f8f584c136
commit 41cdee7bc7
9 changed files with 58 additions and 81 deletions

View File

@@ -4,7 +4,6 @@ import type { Config } from '@/config';
import { Delete, Get, Middleware, Patch, Post, RestController } from '@/decorators';
import type { IDatabaseCollections, IExternalHooksClass, ITagWithCountDb } from '@/Interfaces';
import { TagEntity } from '@db/entities/TagEntity';
import { getTagsWithCountDb } from '@/TagHelpers';
import { validateEntity } from '@/GenericHelpers';
import { BadRequestError, UnauthorizedError } from '@/ResponseHelper';
import { TagsRequest } from '@/requests';
@@ -44,8 +43,17 @@ export class TagsController {
async getAll(req: TagsRequest.GetAll): Promise<TagEntity[] | ITagWithCountDb[]> {
const { withUsageCount } = req.query;
if (withUsageCount === 'true') {
const tablePrefix = this.config.getEnv('database.tablePrefix');
return getTagsWithCountDb(tablePrefix);
return this.tagsRepository
.find({
select: ['id', 'name', 'createdAt', 'updatedAt'],
relations: ['workflowMappings'],
})
.then((tags) =>
tags.map(({ workflowMappings, ...rest }) => ({
...rest,
usageCount: workflowMappings.length,
})),
);
}
return this.tagsRepository.find({ select: ['id', 'name', 'createdAt', 'updatedAt'] });