From c3c02469f2ff44eb30bef80b54ab2345faa0084f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Mon, 18 Dec 2023 17:41:34 +0100 Subject: [PATCH] Move to `RoleService` and add tests --- packages/cli/src/services/role.service.ts | 18 +++++++ packages/cli/src/services/user.service.ts | 18 ------- packages/cli/src/telemetry/index.ts | 4 +- .../cli/test/integration/role.service.test.ts | 48 +++++++++++++++++++ 4 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 packages/cli/test/integration/role.service.test.ts diff --git a/packages/cli/src/services/role.service.ts b/packages/cli/src/services/role.service.ts index 5b81b8b99ffc3..3d2b68bfb8cc2 100644 --- a/packages/cli/src/services/role.service.ts +++ b/packages/cli/src/services/role.service.ts @@ -100,4 +100,22 @@ export class RoleService { }) .then((shared) => shared?.role); } + + /** + * Counts the number of users in each role, e.g. `{ admin: 2, member: 6, owner: 1 }` + */ + async countUsersByRole() { + const result: Array<{ role_name: string; count: number }> = await this.roleRepository + .createQueryBuilder('role') + .select('role.name') + .addSelect('COUNT(user.id)', 'count') + .innerJoin('user', 'user', 'role.id = user.globalRoleId') + .groupBy('role.name') + .getRawMany(); + + return result.reduce>((acc, item) => { + acc[item.role_name] = item.count; + return acc; + }, {}); + } } diff --git a/packages/cli/src/services/user.service.ts b/packages/cli/src/services/user.service.ts index 02fab23e4b9e5..9cbf4b9f7b7ed 100644 --- a/packages/cli/src/services/user.service.ts +++ b/packages/cli/src/services/user.service.ts @@ -308,22 +308,4 @@ export class UserService { return { usersInvited, usersCreated: toCreateUsers.map(({ email }) => email) }; } - - /** - * Counts the number of users in each role, e.g. `{ admin: 2, member: 6, owner: 1 }` - */ - async countUsersByRole() { - const result: Array<{ role_name: string; count: number }> = await Container.get(RoleRepository) - .createQueryBuilder('role') - .select('role.name') - .addSelect('COUNT(user.id)', 'count') - .innerJoin('user', 'user', 'role.id = user.globalRoleId') - .groupBy('role.name') - .getRawMany(); - - return result.reduce>((acc, item) => { - acc[item.role_name] = item.count; - return acc; - }, {}); - } } diff --git a/packages/cli/src/telemetry/index.ts b/packages/cli/src/telemetry/index.ts index 2836d119f6257..caa240d9054d7 100644 --- a/packages/cli/src/telemetry/index.ts +++ b/packages/cli/src/telemetry/index.ts @@ -10,7 +10,7 @@ import { LicenseService } from '@/license/License.service'; import { N8N_VERSION } from '@/constants'; import { SourceControlPreferencesService } from '../environments/sourceControl/sourceControlPreferences.service.ee'; import { InstanceSettings } from 'n8n-core'; -import { UserService } from '@/services/user.service'; +import { RoleService } from '@/services/role.service'; type ExecutionTrackDataKey = 'manual_error' | 'manual_success' | 'prod_error' | 'prod_success'; @@ -109,7 +109,7 @@ export class Telemetry { plan_name_current: this.license.getPlanName(), quota: this.license.getTriggerLimit(), usage: await LicenseService.getActiveTriggerCount(), - role_count: await Container.get(UserService).countUsersByRole(), + role_count: await Container.get(RoleService).countUsersByRole(), source_control_set_up: Container.get(SourceControlPreferencesService).isSourceControlSetup(), branchName: sourceControlPreferences.branchName, read_only_instance: sourceControlPreferences.branchReadOnly, diff --git a/packages/cli/test/integration/role.service.test.ts b/packages/cli/test/integration/role.service.test.ts new file mode 100644 index 0000000000000..e473a8fb6b1b1 --- /dev/null +++ b/packages/cli/test/integration/role.service.test.ts @@ -0,0 +1,48 @@ +import { RoleService } from '@/services/role.service'; +import { createAdmin, createMember, createOwner } from './shared/db/users'; +import * as testDb from './shared/testDb'; +import { mock } from 'jest-mock-extended'; +import { RoleRepository } from '@/databases/repositories/role.repository'; +import Container from 'typedi'; +import { SharedWorkflowRepository } from '@/databases/repositories/sharedWorkflow.repository'; + +describe('RoleService', () => { + let roleService: RoleService; + + beforeAll(async () => { + await testDb.init(); + + roleService = new RoleService( + Container.get(RoleRepository), + Container.get(SharedWorkflowRepository), + mock(), + ); + + testDb.truncate(['User']); + }); + + afterEach(async () => { + await testDb.truncate(['SharedWorkflow']); + }); + + afterAll(async () => { + await testDb.terminate(); + }); + + describe('countUsersByRole()', () => { + test('should return the number of users in each role', async () => { + await Promise.all([ + createOwner(), + createAdmin(), + createAdmin(), + createMember(), + createMember(), + createMember(), + ]); + + const usersByRole = await roleService.countUsersByRole(); + + expect(usersByRole).toStrictEqual({ admin: 2, member: 3, owner: 1 }); + }); + }); +});