Skip to content

Commit

Permalink
Add toggle test connections display mode functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Artuomka committed Sep 14, 2024
1 parent 00345df commit 887b38d
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 2 deletions.
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 @@ -29,6 +29,7 @@ export enum UseCaseType {
AUTHENTICATE_WITH_GITHUB = 'AUTHENTICATE_WITH_GITHUB',
SAVE_USER_SESSION_SETTINGS = 'SAVE_USER_SESSION_SETTINGS',
GET_USER_SESSION_SETTINGS = 'GET_USER_SESSION_SETTINGS',
TOGGLE_TEST_CONNECTIONS_DISPLAY_MODE = 'TOGGLE_TEST_CONNECTIONS_DISPLAY_MODE',

FIND_CONNECTIONS = 'FIND_CONNECTIONS',
FIND_USERS_IN_CONNECTION = 'FIND_USERS_IN_CONNECTION',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class ToggleConnectionDisplayModeDs {
userId: string;
displayMode: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Inject, Injectable, NotFoundException, Scope } from '@nestjs/common';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { SuccessResponse } from '../../../microservices/saas-microservice/data-structures/common-responce.ds.js';
import { IToggleTestConnectionsMode } from './user-use-cases.interfaces.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { ToggleConnectionDisplayModeDs } from '../application/data-structures/toggle-connection-display-mode.ds.js';

@Injectable({ scope: Scope.REQUEST })
export class ToggleTestConnectionsDisplayModeUseCase
extends AbstractUseCase<ToggleConnectionDisplayModeDs, SuccessResponse>
implements IToggleTestConnectionsMode
{
constructor(
@Inject(BaseType.GLOBAL_DB_CONTEXT)
protected _dbContext: IGlobalDatabaseContext,
) {
super();
}

public async implementation(inputData: ToggleConnectionDisplayModeDs): Promise<SuccessResponse> {
const { userId, displayMode } = inputData;
const user = await this._dbContext.userRepository.findOneUserById(userId);
if (!user) {
throw new NotFoundException(Messages.USER_NOT_FOUND);
}
user.showTestConnections = displayMode;
await this._dbContext.userRepository.save(user);
return { success: true };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { UsualLoginDs } from '../application/data-structures/usual-login.ds.js';
import { VerifyOtpDS } from '../application/data-structures/verify-otp.ds.js';
import { IToken } from '../utils/generate-gwt-token.js';
import { RequestRestUserPasswordDto } from '../dto/request-rest-user-password.dto.js';
import { ToggleConnectionDisplayModeDs } from '../application/data-structures/toggle-connection-display-mode.ds.js';
import { SuccessResponse } from '../../../microservices/saas-microservice/data-structures/common-responce.ds.js';

export interface ICreateUserUseCase {
execute(userData: CreateUserDs): Promise<CreatedUserDs>;
Expand Down Expand Up @@ -116,3 +118,7 @@ export interface ISaveUserSettings {
export interface IGetUserSettings {
execute(userId: string, inTransaction: InTransactionEnum): Promise<SaveUserSettingsDs>;
}

export interface IToggleTestConnectionsMode {
execute(inputData: ToggleConnectionDisplayModeDs, inTransaction: InTransactionEnum): Promise<SuccessResponse>;
}
30 changes: 29 additions & 1 deletion backend/src/entities/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
HttpStatus,
Inject,
Injectable,
Param,
Post,
Put,
Req,
Expand Down Expand Up @@ -43,6 +44,7 @@ import {
IRequestEmailVerification,
IRequestPasswordReset,
ISaveUserSettings,
IToggleTestConnectionsMode,
IUsualLogin,
IUsualPasswordChange,
IVerifyEmail,
Expand All @@ -54,7 +56,7 @@ import { ITokenExp, TokenExpDs } from './utils/generate-gwt-token.js';
import { OtpSecretDS } from './application/data-structures/otp-secret.ds.js';
import { OtpDisablingResultDS, OtpValidationResultDS } from './application/data-structures/otp-validation-result.ds.js';
import { getCookieDomainOptions } from './utils/get-cookie-domain-options.js';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { PasswordDto } from './dto/password.dto.js';
import { EmailDto } from './dto/email.dto.js';
import { DeleteUserAccountDTO } from './dto/delete-user-account-request.dto.js';
Expand All @@ -63,6 +65,7 @@ import { UserNameDto } from './dto/user-name.dto.js';
import { OtpTokenDto } from './dto/otp-token.dto.js';
import { UserSettingsDataRequestDto } from './dto/user-settings-data-request.dto.js';
import { RequestRestUserPasswordDto } from './dto/request-rest-user-password.dto.js';
import { SuccessResponse } from '../../microservices/saas-microservice/data-structures/common-responce.ds.js';

@UseInterceptors(SentryInterceptor)
@Controller()
Expand Down Expand Up @@ -107,6 +110,8 @@ export class UserController {
private readonly saveUserSessionSettingsUseCase: ISaveUserSettings,
@Inject(UseCaseType.GET_USER_SESSION_SETTINGS)
private readonly getUserSessionSettingsUseCase: IGetUserSettings,
@Inject(UseCaseType.TOGGLE_TEST_CONNECTIONS_DISPLAY_MODE)
private readonly toggleTestConnectionsDisplayModeUseCase: IToggleTestConnectionsMode,
) {}

@ApiOperation({ summary: 'Get user' })
Expand Down Expand Up @@ -460,4 +465,27 @@ export class UserController {
async getUserSessionSettings(@UserId() userId: string): Promise<UserSettingsDataRequestDto> {
return await this.getUserSessionSettingsUseCase.execute(userId, InTransactionEnum.OFF);
}

@ApiOperation({ summary: 'Toggle display mode of test connections' })
@ApiResponse({
status: 200,
description: 'Display mode of test connections changed.',
type: SuccessResponse,
})
@ApiParam({ name: 'displayMode', required: true, type: 'string', enum: ['on', 'off'] })
@Put('user/test/connections/display/')
async toggleTestConnectionsDisplayMode(
@UserId() userId: string,
@Param('displayMode') displayMode: string,
): Promise<SuccessResponse> {
const newDisplayMode = displayMode === 'on';
const toggleConnectionDisplayModeDs = {
userId: userId,
displayMode: newDisplayMode,
};
return await this.toggleTestConnectionsDisplayModeUseCase.execute(
toggleConnectionDisplayModeDs,
InTransactionEnum.OFF,
);
}
}
6 changes: 6 additions & 0 deletions backend/src/entities/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { GetUserSessionSettingsUseCase } from './use-cases/get-user-session-sett
import { SaveUserSettingsUseCase } from './use-cases/save-user-session-settings.use.case.js';
import { CompanyInfoEntity } from '../company-info/company-info.entity.js';
import { NonScopedAuthMiddleware } from '../../authorization/non-scoped-auth.middleware.js';
import { ToggleTestConnectionsDisplayModeUseCase } from './use-cases/toggle-test-connections-display-mode.use.case.js';

@Module({
imports: [
Expand Down Expand Up @@ -132,6 +133,10 @@ import { NonScopedAuthMiddleware } from '../../authorization/non-scoped-auth.mid
provide: UseCaseType.GET_USER_SESSION_SETTINGS,
useClass: GetUserSessionSettingsUseCase,
},
{
provide: UseCaseType.TOGGLE_TEST_CONNECTIONS_DISPLAY_MODE,
useClass: ToggleTestConnectionsDisplayModeUseCase,
},
UserHelperService,
],
controllers: [UserController],
Expand All @@ -152,6 +157,7 @@ export class UserModule implements NestModule {
{ path: 'user/email/change/request', method: RequestMethod.GET },
{ path: 'user/settings', method: RequestMethod.POST },
{ path: 'user/settings', method: RequestMethod.GET },
{path: 'user/test/connections/display/', method: RequestMethod.PUT},
)
.apply(TemporaryAuthMiddleware)
.forRoutes({ path: 'user/otp/login', method: RequestMethod.POST })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@ test.serial(`${currentTest} should return user settings when it was created`, as
throw err;
}
t.pass();
});
});
46 changes: 46 additions & 0 deletions backend/test/ava-tests/saas-tests/user-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,49 @@ test.serial(`${currentTest} should return user settings when it was created`, as
}
t.pass();
});

currentTest = 'PUT user/test/connections/display/';
test.serial(`${currentTest} should toggle test connections display mode`, async (t) => {
try {
const adminUserRegisterInfo = await registerUserAndReturnUserInfo(app);
const { token } = adminUserRegisterInfo;

//get user connections (test connections should be included)

const getUserConnectionsResult = await request(app.getHttpServer())
.get('/connections')
.set('Cookie', token)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json');

const getUserConnectionsRO = JSON.parse(getUserConnectionsResult.text);
t.is(getUserConnectionsResult.status, 200);
t.is(getUserConnectionsRO.hasOwnProperty('connections'), true);
t.is(getUserConnectionsRO.connections.length, 4);

const toggleTestConnectionsDisplayModeResult = await request(app.getHttpServer())
.put('/user/test/connections/display/?displayMode=off')
.set('Cookie', token)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json');
const toggleTestConnectionsDisplayModeRO = JSON.parse(toggleTestConnectionsDisplayModeResult.text);
t.is(toggleTestConnectionsDisplayModeResult.status, 200);
t.truthy(toggleTestConnectionsDisplayModeRO.success);

//get user connections (test connections shouldn't be included)

const getUserConnectionsResultAfterToggleMode = await request(app.getHttpServer())
.get('/connections')
.set('Cookie', token)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json');

const getUserConnectionsROAfterToggleMode = JSON.parse(getUserConnectionsResultAfterToggleMode.text);
t.is(getUserConnectionsResultAfterToggleMode.status, 200);
t.is(getUserConnectionsROAfterToggleMode.hasOwnProperty('connections'), true);
t.is(getUserConnectionsROAfterToggleMode.connections.length, 0);
} catch (err) {
throw err;
}
t.pass();
});

0 comments on commit 887b38d

Please sign in to comment.