generated from xgeekshq/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: users, auth and azure use cases (#1233)
- Loading branch information
1 parent
579a451
commit 486c344
Showing
81 changed files
with
938 additions
and
741 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
backend/src/libs/exceptions/deleteFailedBadRequestException.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BadRequestException, HttpStatus } from '@nestjs/common'; | ||
import { DELETE_FAILED } from './messages'; | ||
|
||
export class DeleteFailedException extends BadRequestException { | ||
private static defaultMessage = DELETE_FAILED; | ||
|
||
constructor(message = DeleteFailedException.defaultMessage) { | ||
super( | ||
{ | ||
statusCode: HttpStatus.BAD_REQUEST, | ||
message | ||
}, | ||
message | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/libs/exceptions/insertFailedBadRequestException.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BadRequestException, HttpStatus } from '@nestjs/common'; | ||
import { INSERT_FAILED } from './messages'; | ||
|
||
export class InsertFailedException extends BadRequestException { | ||
private static defaultMessage = INSERT_FAILED; | ||
|
||
constructor(message = InsertFailedException.defaultMessage) { | ||
super( | ||
{ | ||
statusCode: HttpStatus.BAD_REQUEST, | ||
message | ||
}, | ||
message | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/libs/exceptions/updateFailedBadRequestException.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BadRequestException, HttpStatus } from '@nestjs/common'; | ||
import { UPDATE_FAILED } from './messages'; | ||
|
||
export class UpdateFailedException extends BadRequestException { | ||
private static defaultMessage = UPDATE_FAILED; | ||
|
||
constructor(message = UpdateFailedException.defaultMessage) { | ||
super( | ||
{ | ||
statusCode: HttpStatus.BAD_REQUEST, | ||
message | ||
}, | ||
message | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NotFoundException } from '@nestjs/common'; | ||
import { USER_NOT_FOUND } from 'src/libs/exceptions/messages'; | ||
|
||
export class UserNotFoundException extends NotFoundException { | ||
private static defaultMessage = USER_NOT_FOUND; | ||
|
||
constructor(message = UserNotFoundException.defaultMessage) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface UseCase<T, P> { | ||
execute(props: T): Promise<P>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
backend/src/modules/auth/applications/create-reset-token.auth.application.ts
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
backend/src/modules/auth/applications/get-token.auth.application.ts
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
backend/src/modules/auth/applications/refresh-token.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { RefreshTokenUseCaseInterface } from '../interfaces/applications/refresh-token.use-case.interface'; | ||
import { TYPES } from '../interfaces/types'; | ||
import { GetTokenAuthServiceInterface } from '../interfaces/services/get-token.auth.service.interface'; | ||
|
||
@Injectable() | ||
export default class RefreshTokenUseCase implements RefreshTokenUseCaseInterface { | ||
constructor( | ||
@Inject(TYPES.services.GetTokenAuthService) | ||
private readonly getTokenService: GetTokenAuthServiceInterface | ||
) {} | ||
|
||
execute(userId: string) { | ||
return this.getTokenService.getAccessToken(userId); | ||
} | ||
} |
23 changes: 6 additions & 17 deletions
23
...es/auth/services/register.auth.service.ts → ...lications/register-guest-user.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
backend/src/modules/auth/applications/register-user.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { BadRequestException, Inject, Injectable } from '@nestjs/common'; | ||
import { encrypt } from 'src/libs/utils/bcrypt'; | ||
import CreateUserDto from 'src/modules/users/dto/create.user.dto'; | ||
import { CreateUserServiceInterface } from 'src/modules/users/interfaces/services/create.user.service.interface'; | ||
import { TYPES } from 'src/modules/users/interfaces/types'; | ||
import { RegisterUserUseCaseInterface } from '../interfaces/applications/register-user.use-case.interface'; | ||
import { uniqueViolation } from 'src/infrastructure/database/errors/unique.user'; | ||
import { EmailExistsException } from '../exceptions/emailExistsException'; | ||
|
||
@Injectable() | ||
export default class RegisterUserUseCase implements RegisterUserUseCaseInterface { | ||
constructor( | ||
@Inject(TYPES.services.CreateUserService) | ||
private createUserService: CreateUserServiceInterface | ||
) {} | ||
|
||
public async execute(registrationData: CreateUserDto) { | ||
const hashedPassword = await encrypt(registrationData.password); | ||
|
||
try { | ||
const { _id, firstName, lastName, email } = await this.createUserService.create({ | ||
...registrationData, | ||
password: hashedPassword | ||
}); | ||
|
||
return { _id, firstName, lastName, email }; | ||
} catch (error) { | ||
if (error.code === uniqueViolation) { | ||
throw new EmailExistsException(); | ||
} | ||
|
||
throw new BadRequestException(error.message); | ||
} | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
backend/src/modules/auth/applications/register.auth.application.ts
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
backend/src/modules/auth/applications/reset-password.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { TYPES } from 'src/modules/users/interfaces/types'; | ||
import { ResetPasswordUseCaseInterface } from '../interfaces/applications/reset-password.use-case.interface'; | ||
import { UpdateUserServiceInterface } from 'src/modules/users/interfaces/services/update.user.service.interface'; | ||
import { UPDATE_FAILED } from 'src/libs/exceptions/messages'; | ||
import { InvalidTokenException } from '../exceptions/invalidTokenException'; | ||
import { UpdateFailedException } from 'src/libs/exceptions/updateFailedBadRequestException'; | ||
|
||
@Injectable() | ||
export default class ResetPasswordUseCase implements ResetPasswordUseCaseInterface { | ||
constructor( | ||
@Inject(TYPES.services.UpdateUserService) | ||
private readonly updateUserService: UpdateUserServiceInterface | ||
) {} | ||
|
||
async execute(token: string, newPassword: string, newPasswordConf: string) { | ||
const email = await this.updateUserService.checkEmailOfToken(token); | ||
|
||
if (!email) { | ||
throw new InvalidTokenException(); | ||
} | ||
|
||
const result = await this.updateUserService.setPassword(email, newPassword, newPasswordConf); | ||
|
||
if (!result) { | ||
throw new UpdateFailedException(UPDATE_FAILED); | ||
} | ||
|
||
return { | ||
status: 'ok', | ||
message: 'Password updated successfully!' | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import UserDto from 'src/modules/users/dto/user.dto'; | ||
import { TYPES } from '../interfaces/types'; | ||
import { GetTokenAuthServiceInterface } from '../interfaces/services/get-token.auth.service.interface'; | ||
import { signIn } from '../shared/login.auth'; | ||
import { SignInUseCaseInterface } from '../interfaces/applications/signIn.use-case.interface'; | ||
import { UserNotFoundException } from 'src/libs/exceptions/userNotFoundException'; | ||
|
||
@Injectable() | ||
export default class SignInUseCase implements SignInUseCaseInterface { | ||
constructor( | ||
@Inject(TYPES.services.GetTokenAuthService) | ||
private getTokenAuthService: GetTokenAuthServiceInterface | ||
) {} | ||
|
||
public async execute(user: UserDto) { | ||
const loggedUser = await signIn(user, this.getTokenAuthService, 'local'); | ||
|
||
if (!loggedUser) { | ||
throw new UserNotFoundException(); | ||
} | ||
|
||
return loggedUser; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/src/modules/auth/applications/statistics.auth.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import * as User from 'src/modules/users/interfaces/types'; | ||
import * as Teams from 'src/modules/teams/interfaces/types'; | ||
import * as Boards from 'src/modules/boards/interfaces/types'; | ||
import { GetBoardApplicationInterface } from 'src/modules/boards/interfaces/applications/get.board.application.interface'; | ||
import { GetTeamApplicationInterface } from 'src/modules/teams/interfaces/applications/get.team.application.interface'; | ||
import { GetUserServiceInterface } from 'src/modules/users/interfaces/services/get.user.service.interface'; | ||
import { StatisticsAuthUserUseCaseInterface } from '../interfaces/applications/statistics.auth.use-case.interface'; | ||
|
||
@Injectable() | ||
export default class StatisticsAuthUserUseCase implements StatisticsAuthUserUseCaseInterface { | ||
constructor( | ||
@Inject(User.TYPES.services.GetUserService) | ||
private getUserService: GetUserServiceInterface, | ||
@Inject(Teams.TYPES.applications.GetTeamApplication) | ||
private getTeamsService: GetTeamApplicationInterface, | ||
@Inject(Boards.TYPES.applications.GetBoardApplication) | ||
private getBoardService: GetBoardApplicationInterface | ||
) {} | ||
|
||
public async execute(userId: string) { | ||
const [usersCount, teamsCount, boardsCount] = await Promise.all([ | ||
this.getUserService.countUsers(), | ||
this.getTeamsService.countTeams(userId), | ||
this.getBoardService.countBoards(userId) | ||
]); | ||
|
||
return { usersCount, teamsCount, boardsCount }; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/modules/auth/applications/validate-user-email.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { TYPES } from 'src/modules/users/interfaces/types'; | ||
import { ValidateUserEmailUseCaseInterface } from '../interfaces/applications/validate-email.use-case.interface'; | ||
import { GetUserServiceInterface } from 'src/modules/users/interfaces/services/get.user.service.interface'; | ||
|
||
@Injectable() | ||
export default class ValidateUserEmailUseCase implements ValidateUserEmailUseCaseInterface { | ||
constructor( | ||
@Inject(TYPES.services.GetUserService) | ||
private readonly getUserService: GetUserServiceInterface | ||
) {} | ||
|
||
async execute(email: string) { | ||
const user = await this.getUserService.getByEmail(email); | ||
|
||
return !!user; | ||
} | ||
} |
Oops, something went wrong.