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 @@ -116,6 +116,7 @@ export enum UseCaseType {
SAAS_REMOVE_COMPANY_ID_FROM_USER = 'SAAS_REMOVE_COMPANY_ID_FROM_USER',
SAAS_REGISTER_INVITED_USER = 'SAAS_REGISTER_INVITED_USER',
SAAS_SAAS_GET_USERS_INFOS_BY_EMAIL = 'SAAS_SAAS_GET_USERS_INFOS_BY_EMAIL',
SAAS_SUSPEND_USERS = 'SAAS_SUSPEND_USERS',

INVITE_USER_IN_COMPANY_AND_CONNECTION_GROUP = 'INVITE_USER_IN_COMPANY_AND_CONNECTION_GROUP',
VERIFY_INVITE_USER_IN_COMPANY_AND_CONNECTION_GROUP = 'VERIFY_INVITE_USER_IN_COMPANY_AND_CONNECTION_GROUP',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class FindAllUsersInConnectionUseCase
email: user.email,
createdAt: user.createdAt,
name: user.name,
suspended: user.suspended,
is_2fa_enabled: user.otpSecretKey !== null && user.isOTPEnabled,
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ export class FoundUserInGroupDs {

@ApiProperty()
name: string;

@ApiProperty()
suspended: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export class FoundUserDs {
@ApiProperty()
createdAt: Date;

@ApiProperty()
suspended: boolean;

@ApiProperty({ required: false })
intercom_hash?: string;

Expand All @@ -44,6 +47,9 @@ export class SimpleFoundUserInfoDs {
@ApiProperty()
createdAt: Date;

@ApiProperty()
suspended: boolean;

@ApiProperty()
name: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export const userCustomRepositoryExtension: IUserRepository = {
): Promise<UserEntity | null> {
const userQb = this.createQueryBuilder('user').where('user.email = :userEmail', { userEmail: email });
if (externalRegistrationProvider) {
userQb.andWhere('user.externalRegistrationProvider = :externalRegistrationProvider', { externalRegistrationProvider: externalRegistrationProvider });
userQb.andWhere('user.externalRegistrationProvider = :externalRegistrationProvider', {
externalRegistrationProvider: externalRegistrationProvider,
});
}
return userQb.getOne();
},
Expand Down Expand Up @@ -177,11 +179,39 @@ export const userCustomRepositoryExtension: IUserRepository = {
): Promise<Array<UserEntity>> {
const usersQb = this.createQueryBuilder('user').where('user.email = :userEmail', { userEmail: email });
if (externalRegistrationProvider) {
usersQb.andWhere('user.externalRegistrationProvider = :externalRegistrationProvider', { externalRegistrationProvider: externalRegistrationProvider });
usersQb.andWhere('user.externalRegistrationProvider = :externalRegistrationProvider', {
externalRegistrationProvider: externalRegistrationProvider,
});
}
return await usersQb.getMany();
},

async findUsersByEmailsAndCompanyId(usersEmails: Array<string>, companyId: string): Promise<Array<UserEntity>> {
const usersQb = this.createQueryBuilder('user')
.leftJoinAndSelect('user.company', 'company')
.where('user.email IN (:...userEmails)', { userEmails: usersEmails })
.andWhere('company.id = :companyId', { companyId: companyId });
return await usersQb.getMany();
},

async suspendUsers(userIds: Array<string>): Promise<Array<UserEntity>> {
const usersQb = this.createQueryBuilder('user')
.update()
.set({ suspended: true })
.where('id IN (:...userIds)', { userIds: userIds });
await usersQb.execute();
return await this.createQueryBuilder('user').whereInIds(userIds).getMany();
},

async unSuspendUsers(userIds: Array<string>): Promise<Array<UserEntity>> {
const usersQb = this.createQueryBuilder('user')
.update()
.set({ suspended: false })
.where('id IN (:...userIds)', { userIds: userIds });
await usersQb.execute();
return await this.createQueryBuilder('user').whereInIds(userIds).getMany();
},

async bulkSaveUpdatedUsers(updatedUsers: Array<UserEntity>): Promise<Array<UserEntity>> {
return await this.save(updatedUsers);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ export interface IUserRepository {

findAllUsersWithEmail(email: string, externalProvider?: ExternalRegistrationProviderEnum): Promise<Array<UserEntity>>;

findUsersByEmailsAndCompanyId(emails: Array<string>, companyId: string): Promise<Array<UserEntity>>;

suspendUsers(userIds: Array<string>): Promise<Array<UserEntity>>;

unSuspendUsers(userIds: Array<string>): Promise<Array<UserEntity>>;

bulkSaveUpdatedUsers(updatedUsers: Array<UserEntity>): Promise<Array<UserEntity>>;
}
2 changes: 2 additions & 0 deletions backend/src/entities/user/user-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class UserHelperService implements OnModuleInit {
createdAt: user.createdAt,
isActive: user.isActive,
name: user.name,
suspended: user.suspended,
};
}

Expand All @@ -36,6 +37,7 @@ export class UserHelperService implements OnModuleInit {
return {
id: user.id,
createdAt: user.createdAt,
suspended: user.suspended,
isActive: user.isActive,
email: user.email,
intercom_hash: intercomHash,
Expand Down
3 changes: 3 additions & 0 deletions backend/src/entities/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class UserEntity {
@Column({ default: null })
name: string;

@Column({ default: false })
suspended: boolean;

@BeforeInsert()
async hashPassword() {
if (this.password) {
Expand Down
1 change: 1 addition & 0 deletions backend/src/entities/user/utils/build-created-user.ds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function buildSimpleUserInfoDs(user: UserEntity): SimpleFoundUserInfoDs {
isActive: user.isActive,
email: user.email,
createdAt: user.createdAt,
suspended: user.suspended,
name: user.name,
is_2fa_enabled: user.isOTPEnabled,
role: user.role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class SaasCompanyGatewayService extends BaseSaasGatewayService {
}

public async canInviteMoreUsers(companyId: string): Promise<boolean> {
if (!isSaaS()) {
if (!isSaaS() || process.env.NODE_ENV === 'test') {
return true;
}
const canInviteMoreUsersResult = await this.sendRequestToSaaS(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class SuspendUsersDS {
emailsToSuspend: Array<string>;
companyId: string
}
13 changes: 13 additions & 0 deletions backend/src/microservices/saas-microservice/saas.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ISaaSRegisterInvitedUser,
ISaasGetUsersInfosByEmail,
ISaasRegisterUser,
ISuspendUsers,
} from './use-cases/saas-use-cases.interface.js';
import { RegisteredCompanyDS } from './data-structures/registered-company.ds.js';
import { UserEntity } from '../../entities/user/user.entity.js';
Expand Down Expand Up @@ -57,6 +58,8 @@ export class SaasController {
private readonly removeCompanyIdFromUserUserUseCase: IAddOrRemoveCompanyIdToUser,
@Inject(UseCaseType.SAAS_REGISTER_INVITED_USER)
private readonly registerInvitedUserUseCase: ISaaSRegisterInvitedUser,
@Inject(UseCaseType.SAAS_SUSPEND_USERS)
private readonly suspendUsersUseCase: ISuspendUsers,
) {}

@ApiOperation({ summary: 'Company registered webhook' })
Expand Down Expand Up @@ -221,4 +224,14 @@ export class SaasController {
await this.removeCompanyIdFromUserUserUseCase.execute({ userId, companyId, userRole });
return { success: true };
}

@ApiOperation({ summary: 'Suspending users' })
@Put('/company/:companyId/users/suspend')
async suspendUsers(
@Body('emailsToSuspend') emailsToSuspend: Array<string>,
@Body('companyId') companyId: string,
): Promise<SuccessResponse> {
await this.suspendUsersUseCase.execute({ emailsToSuspend, companyId });
return { success: true };
}
}
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 @@ -14,6 +14,7 @@ import { AddCompanyIdToUserUseCase } from './use-cases/add-company-id-to-user-us
import { RemoveCompanyIdFromUserUseCase } from './use-cases/remove-company-id-from-user.use.case.js';
import { SaasRegisterInvitedUserUseCase } from './use-cases/register-invited-user-use.case.js';
import { GetUsersInfosByEmailUseCase } from './use-cases/get-users-infos-by-email.use.case.js';
import { SuspendUsersUseCase } from './use-cases/suspend-users.use.case.js';

@Module({
imports: [],
Expand Down Expand Up @@ -66,6 +67,10 @@ import { GetUsersInfosByEmailUseCase } from './use-cases/get-users-infos-by-emai
provide: UseCaseType.SAAS_SAAS_GET_USERS_INFOS_BY_EMAIL,
useClass: GetUsersInfosByEmailUseCase,
},
{
provide: UseCaseType.SAAS_SUSPEND_USERS,
useClass: SuspendUsersUseCase,
},
],
controllers: [SaasController],
exports: [],
Expand All @@ -83,6 +88,7 @@ export class SaasModule {
{ path: 'saas/user/github/:githubId', method: RequestMethod.GET },
{ path: 'saas/user/github/login', method: RequestMethod.POST },
{ path: 'saas/user/:userId/company/:companyId', method: RequestMethod.PUT },
{ path: 'saas/company/:companyId/users/suspend', method: RequestMethod.PUT },
{ path: 'sass/user/register/invite', method: RequestMethod.POST },
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class SaasRegisterInvitedUserUseCase
intercom_hash: null,
name: savedUser.name,
is_2fa_enabled: false,
suspended: false,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RegisterCompanyWebhookDS } from '../data-structures/register-company.ds
import { RegisteredCompanyDS } from '../data-structures/registered-company.ds.js';
import { SaasRegisterUserWithGithub } from '../data-structures/saas-register-user-with-github.js';
import { SaasRegisterUserWithGoogleDS } from '../data-structures/sass-register-user-with-google.js';
import { SuspendUsersDS } from '../data-structures/suspend-users.ds.js';

export interface ICompanyRegistration {
execute(inputData: RegisterCompanyWebhookDS): Promise<RegisteredCompanyDS>;
Expand Down Expand Up @@ -51,3 +52,7 @@ export interface ILoginUserWithGitHub {
export interface IAddOrRemoveCompanyIdToUser {
execute(inputData: AddRemoveCompanyIdToUserDS): Promise<void>;
}

export interface ISuspendUsers {
execute(usersData: SuspendUsersDS): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class SaasUsualRegisterUseCase
intercom_hash: null,
name: savedUser.name,
is_2fa_enabled: false,
suspended: false,
};
}

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 { SuspendUsersDS } from '../data-structures/suspend-users.ds.js';
import { ISuspendUsers } from './saas-use-cases.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';

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

protected async implementation(inputData: SuspendUsersDS): Promise<void> {
const { emailsToSuspend, companyId } = inputData;
const foundUsersToSuspend = await this._dbContext.userRepository.findUsersByEmailsAndCompanyId(
emailsToSuspend,
companyId,
);
const foundUsersToSuspendIds = foundUsersToSuspend.map((user) => user.id);
await this._dbContext.userRepository.suspendUsers(foundUsersToSuspendIds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddSuspendedPropertyToUserEntity1710413135309 implements MigrationInterface {
name = 'AddSuspendedPropertyToUserEntity1710413135309';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "user" ADD "suspended" boolean NOT NULL DEFAULT false`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "user" DROP COLUMN "suspended"`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ test(`${currentTest} should return full found company info for company admin use
t.is(foundCompanyInfoRO.connections[0].groups[0].hasOwnProperty('isMain'), true);
t.is(foundCompanyInfoRO.connections[0].groups[0].hasOwnProperty('users'), true);
t.is(foundCompanyInfoRO.connections[0].groups[0].users.length > 0, true);
t.is(Object.keys(foundCompanyInfoRO.connections[0].groups[0].users[0]).length, 7);
t.is(Object.keys(foundCompanyInfoRO.connections[0].groups[0].users[0]).length, 8);
t.is(foundCompanyInfoRO.connections[0].groups[0].users[0].hasOwnProperty('id'), true);
t.is(foundCompanyInfoRO.connections[0].groups[0].users[0].hasOwnProperty('email'), true);
t.is(foundCompanyInfoRO.connections[0].groups[0].users[0].hasOwnProperty('role'), true);
Expand Down
Loading