From 0e582594ea889f86a2c29021ec30e25af0b050b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 28 Dec 2023 09:27:47 +0100 Subject: [PATCH] refactor(core): Move more `typeorm` operators to `UserRepository` (no-changelog) (#8165) Follow-up to: #8163 --- packages/cli/src/controllers/auth.controller.ts | 9 ++++----- packages/cli/src/controllers/invitation.controller.ts | 8 +++----- .../cli/src/databases/repositories/user.repository.ts | 9 ++++++++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/controllers/auth.controller.ts b/packages/cli/src/controllers/auth.controller.ts index b139013d38..235177023c 100644 --- a/packages/cli/src/controllers/auth.controller.ts +++ b/packages/cli/src/controllers/auth.controller.ts @@ -1,5 +1,4 @@ import validator from 'validator'; -import { In } from 'typeorm'; import { Authorized, Get, Post, RestController } from '@/decorators'; import { issueCookie, resolveJwt } from '@/auth/jwt'; import { AUTH_COOKIE_NAME, RESPONSE_ERROR_MESSAGES } from '@/constants'; @@ -25,6 +24,7 @@ import { InternalServerError } from '@/errors/response-errors/internal-server.er import { BadRequestError } from '@/errors/response-errors/bad-request.error'; import { UnauthorizedError } from '@/errors/response-errors/unauthorized.error'; import { ApplicationError } from 'n8n-workflow'; +import { UserRepository } from '@/databases/repositories/user.repository'; @RestController() export class AuthController { @@ -34,6 +34,7 @@ export class AuthController { private readonly mfaService: MfaService, private readonly userService: UserService, private readonly license: License, + private readonly userRepository: UserRepository, private readonly postHog?: PostHogClient, ) {} @@ -186,10 +187,8 @@ export class AuthController { } } - const users = await this.userService.findMany({ - where: { id: In([inviterId, inviteeId]) }, - relations: ['globalRole'], - }); + const users = await this.userRepository.findManybyIds([inviterId, inviteeId]); + if (users.length !== 2) { this.logger.debug( 'Request to resolve signup token failed because the ID of the inviter and/or the ID of the invitee were not found in database', diff --git a/packages/cli/src/controllers/invitation.controller.ts b/packages/cli/src/controllers/invitation.controller.ts index 4760a9f26e..6d24d53965 100644 --- a/packages/cli/src/controllers/invitation.controller.ts +++ b/packages/cli/src/controllers/invitation.controller.ts @@ -1,4 +1,3 @@ -import { In } from 'typeorm'; import { Response } from 'express'; import config from '@/config'; @@ -18,6 +17,7 @@ import { BadRequestError } from '@/errors/response-errors/bad-request.error'; import { UnauthorizedError } from '@/errors/response-errors/unauthorized.error'; import { InternalHooks } from '@/InternalHooks'; import { ExternalHooks } from '@/ExternalHooks'; +import { UserRepository } from '@/databases/repositories/user.repository'; @Authorized() @RestController('/invitations') @@ -29,6 +29,7 @@ export class InvitationController { private readonly userService: UserService, private readonly license: License, private readonly passwordUtility: PasswordUtility, + private readonly userRepository: UserRepository, private readonly postHog: PostHogClient, ) {} @@ -135,10 +136,7 @@ export class InvitationController { const validPassword = this.passwordUtility.validate(password); - const users = await this.userService.findMany({ - where: { id: In([inviterId, inviteeId]) }, - relations: ['globalRole'], - }); + const users = await this.userRepository.findManybyIds([inviterId, inviteeId]); if (users.length !== 2) { this.logger.debug( diff --git a/packages/cli/src/databases/repositories/user.repository.ts b/packages/cli/src/databases/repositories/user.repository.ts index 863a797a45..1d47f31ea5 100644 --- a/packages/cli/src/databases/repositories/user.repository.ts +++ b/packages/cli/src/databases/repositories/user.repository.ts @@ -1,5 +1,5 @@ import { Service } from 'typedi'; -import { DataSource, Repository } from 'typeorm'; +import { DataSource, In, Repository } from 'typeorm'; import { User } from '../entities/User'; @Service() @@ -7,4 +7,11 @@ export class UserRepository extends Repository { constructor(dataSource: DataSource) { super(User, dataSource.manager); } + + async findManybyIds(userIds: string[]) { + return this.find({ + where: { id: In(userIds) }, + relations: ['globalRole'], + }); + } }