Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/src/common/data-injection.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export enum UseCaseType {
SAAS_LOGIN_USER_WITH_GITHUB = 'SAAS_LOGIN_USER_WITH_GITHUB',
SAAS_SAAS_GET_USERS_INFOS_BY_EMAIL = 'SAAS_SAAS_GET_USERS_INFOS_BY_EMAIL',
SAAS_SUSPEND_USERS = 'SAAS_SUSPEND_USERS',
SAAS_SUSPEND_USERS_OVER_LIMIT = 'SAAS_SUSPEND_USERS_OVER_LIMIT',
SAAS_GET_COMPANY_INFO_BY_USER_ID = 'SAAS_GET_COMPANY_INFO_BY_USER_ID',
SAAS_GET_USERS_COUNT_IN_COMPANY = 'SAAS_GET_USERS_COUNT_IN_COMPANY',
FREEZE_CONNECTIONS_IN_COMPANY = 'FREEZE_CONNECTIONS_IN_COMPANY',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ export const userCustomRepositoryExtension: IUserRepository = {
return await usersQB.getCount();
},

async findUsersInCompany(companyId: string, orderByRole: boolean = false): Promise<Array<UserEntity>> {
const usersQB = this.createQueryBuilder('user')
.leftJoin('user.company', 'company')
.where('company.id = :companyId', { companyId: companyId })
.orderBy('user.createdAt', 'ASC');
if (orderByRole) {
usersQB.addOrderBy('user.role', 'ASC');
}
return await usersQB.getMany();
},

async getUserEmailOrReturnNull(userId: string): Promise<string> {
const userQB = this.createQueryBuilder('user').where('user.id = :userId', { userId: userId });
const user = await userQB.getOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ export interface IUserRepository {

countUsersInCompany(companyId: string): Promise<number>;

findUsersInCompany(companyId: string, orderByRole: boolean): Promise<Array<UserEntity>>;

suspendNewestUsersInCompany(companyId: string, unsuspendedUsersLeft: number): Promise<Array<UserEntity>>;
}
10 changes: 10 additions & 0 deletions backend/src/microservices/saas-microservice/saas.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
ISaasRegisterUser,
ISaasSAMLRegisterUser,
ISuspendUsers,
ISuspendUsersOverLimit,
} from './use-cases/saas-use-cases.interface.js';
import { SkipThrottle } from '@nestjs/throttler';
import { Messages } from '../../exceptions/text/messages.js';
Expand Down Expand Up @@ -69,6 +70,8 @@ export class SaasController {
private readonly registerUserWithSamlUseCase: ISaasSAMLRegisterUser,
@Inject(UseCaseType.SAAS_SUSPEND_USERS)
private readonly suspendUsersUseCase: ISuspendUsers,
@Inject(UseCaseType.SAAS_SUSPEND_USERS_OVER_LIMIT)
private readonly suspendUsersOverLimitUseCase: ISuspendUsersOverLimit,
@Inject(UseCaseType.SAAS_GET_COMPANY_INFO_BY_USER_ID)
private readonly getCompanyInfoByUserIdUseCase: ISaaSGetCompanyInfoByUserId,
@Inject(UseCaseType.SAAS_GET_USERS_COUNT_IN_COMPANY)
Expand Down Expand Up @@ -197,6 +200,13 @@ export class SaasController {
return { success: true };
}

@ApiOperation({ summary: 'Suspending users' })
@Put('/company/:companyId/users/suspend-above-limit')
async suspendUsersOverLimit(@Body('companyId') companyId: string): Promise<SuccessResponse> {
await this.suspendUsersOverLimitUseCase.execute(companyId);
return { success: true };
}

@ApiOperation({ summary: 'Get company info by user id' })
@ApiResponse({
status: 200,
Expand Down
6 changes: 6 additions & 0 deletions backend/src/microservices/saas-microservice/saas.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SuspendUsersUseCase } from './use-cases/suspend-users.use.case.js';
import { UnFreezeConnectionsInCompanyUseCase } from './use-cases/unfreeze-connections-in-company-use.case.js';
import { SaasRegisterDemoUserAccountUseCase } from './use-cases/register-demo-user-account.use.case.js';
import { SaaSRegisterUserWIthSamlUseCase } from './use-cases/register-user-with-saml-use.case.js';
import { SuspendUsersOverLimitUseCase } from './use-cases/suspend-users-over-limit.use.case.js';

@Module({
imports: [],
Expand Down Expand Up @@ -76,6 +77,10 @@ import { SaaSRegisterUserWIthSamlUseCase } from './use-cases/register-user-with-
provide: UseCaseType.SAAS_REGISTER_USER_WITH_SAML,
useClass: SaaSRegisterUserWIthSamlUseCase,
},
{
provide: UseCaseType.SAAS_SUSPEND_USERS_OVER_LIMIT,
useClass: SuspendUsersOverLimitUseCase,
},
],
controllers: [SaasController],
exports: [],
Expand All @@ -92,6 +97,7 @@ export class SaasModule {
{ path: 'saas/user/google/login', method: RequestMethod.POST },
{ path: 'saas/user/github/login', method: RequestMethod.POST },
{ path: 'saas/company/:companyId/users/suspend', method: RequestMethod.PUT },
{ path: 'saas/company/:companyId/users/suspend-above-limit', method: RequestMethod.PUT },
{ path: 'saas/user/:userId/company', method: RequestMethod.GET },
{ path: 'saas/company/:companyId/users/count', method: RequestMethod.GET },
{ path: 'saas/company/freeze-connections', method: RequestMethod.PUT },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface ISuspendUsers {
execute(usersData: SuspendUsersDS): Promise<void>;
}

export interface ISuspendUsersOverLimit {
execute(companyId: string): Promise<void>;
}

export interface ISaaSGetCompanyInfoByUserId {
execute(userId: string): Promise<CompanyInfoEntity>;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Inject, Injectable } from '@nestjs/common';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { ISuspendUsersOverLimit } from './saas-use-cases.interface.js';
import { Constants } from '../../../helpers/constants/constants.js';

@Injectable()
export class SuspendUsersOverLimitUseCase extends AbstractUseCase<string, void> implements ISuspendUsersOverLimit {
constructor(
@Inject(BaseType.GLOBAL_DB_CONTEXT)
protected _dbContext: IGlobalDatabaseContext,
) {
super();
}

protected async implementation(companyId: string): Promise<void> {
const foundUsersInCompany = await this._dbContext.userRepository.findUsersInCompany(companyId, true);
const usersToSuspend = foundUsersInCompany.slice(Constants.FREE_PLAN_USERS_COUNT);

if (usersToSuspend.length > 0) {
const userIdsToSuspend = usersToSuspend.map((user) => user.id);
await this._dbContext.userRepository.suspendUsers(userIdsToSuspend);
}
}
}