-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
293 additions
and
55 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
import { ICommand } from 'src/Application/ICommand'; | ||
|
||
export class RemoveUserCommand implements ICommand { | ||
constructor( | ||
public readonly id: string, | ||
public readonly currentUserId: string | ||
) {} | ||
} |
75 changes: 75 additions & 0 deletions
75
api/src/Application/User/Command/RemoveUserCommandHandler.spec.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,75 @@ | ||
import { mock, instance, when, verify, anything } from 'ts-mockito'; | ||
import { UserRepository } from 'src/Infrastructure/User/Repository/UserRepository'; | ||
import { User } from 'src/Domain/User/User.entity'; | ||
import { RemoveUserCommandHandler } from './RemoveUserCommandHandler'; | ||
import { RemoveUserCommand } from './RemoveUserCommand'; | ||
import { UserNotFoundException } from 'src/Domain/User/Exception/UserNotFoundException'; | ||
import { CantRemoveYourselfException } from 'src/Domain/User/Exception/CantRemoveYourselfException'; | ||
|
||
describe('RemoveUserCommandHandler', () => { | ||
let userRepository: UserRepository; | ||
let removedUser: User; | ||
let handler: RemoveUserCommandHandler; | ||
|
||
const command = new RemoveUserCommand( | ||
'17efcbee-bd2f-410e-9e99-51684b592bad', | ||
'c47f70f1-101c-4d9b-84e1-f88bed74f957' | ||
); | ||
|
||
beforeEach(() => { | ||
userRepository = mock(UserRepository); | ||
removedUser = mock(User); | ||
|
||
handler = new RemoveUserCommandHandler( | ||
instance(userRepository) | ||
); | ||
}); | ||
|
||
it('testUserRemovedSuccessfully', async () => { | ||
when(userRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) | ||
.thenResolve(instance(removedUser)); | ||
when(removedUser.getId()).thenReturn( | ||
'17efcbee-bd2f-410e-9e99-51684b592bad' | ||
); | ||
when( | ||
userRepository.save(instance(removedUser)) | ||
).thenResolve(instance(removedUser)); | ||
|
||
await handler.execute(command); | ||
|
||
verify( | ||
userRepository.remove(instance(removedUser)) | ||
).once(); | ||
verify(userRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); | ||
}); | ||
|
||
it('testUserNotFound', async () => { | ||
when(userRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) | ||
.thenResolve(null); | ||
|
||
try { | ||
expect(await handler.execute(command)).toBeUndefined(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(UserNotFoundException); | ||
expect(e.message).toBe('users.errors.not_found'); | ||
verify(userRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); | ||
verify(userRepository.remove(anything())).never(); | ||
} | ||
}); | ||
|
||
it('testCantRemoveYourself', async () => { | ||
const command2 = new RemoveUserCommand( | ||
'17efcbee-bd2f-410e-9e99-51684b592bad', | ||
'17efcbee-bd2f-410e-9e99-51684b592bad' | ||
); | ||
|
||
try { | ||
expect(await handler.execute(command2)).toBeUndefined(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(CantRemoveYourselfException); | ||
expect(e.message).toBe('users.errors.cant_remove_yourself'); | ||
verify(userRepository.findOneById(anything())).never(); | ||
verify(userRepository.remove(anything())).never(); | ||
} | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
api/src/Application/User/Command/RemoveUserCommandHandler.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,28 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { CommandHandler } from '@nestjs/cqrs'; | ||
import { CantRemoveYourselfException } from 'src/Domain/User/Exception/CantRemoveYourselfException'; | ||
import { UserNotFoundException } from 'src/Domain/User/Exception/UserNotFoundException'; | ||
import { IUserRepository } from 'src/Domain/User/Repository/IUserRepository'; | ||
import { RemoveUserCommand } from './RemoveUserCommand'; | ||
|
||
@CommandHandler(RemoveUserCommand) | ||
export class RemoveUserCommandHandler { | ||
constructor( | ||
@Inject('IUserRepository') | ||
private readonly userRepository: IUserRepository, | ||
) {} | ||
|
||
public async execute({ id, currentUserId }: RemoveUserCommand): Promise<void> { | ||
if (id === currentUserId) { | ||
throw new CantRemoveYourselfException(); | ||
} | ||
|
||
const user = await this.userRepository.findOneById(id); | ||
|
||
if (!user) { | ||
throw new UserNotFoundException(); | ||
} | ||
|
||
await this.userRepository.remove(user); | ||
} | ||
} |
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,5 @@ | ||
export class CantRemoveYourselfException extends Error { | ||
constructor() { | ||
super('users.errors.cant_remove_yourself'); | ||
} | ||
} |
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
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
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
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,43 @@ | ||
import { | ||
Controller, | ||
Inject, | ||
BadRequestException, | ||
UseGuards, | ||
Param, | ||
Delete | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; | ||
import { ICommandBus } from 'src/Application/ICommandBus'; | ||
import { RemoveUserCommand } from 'src/Application/User/Command/RemoveUserCommand'; | ||
import { UserRole } from 'src/Domain/User/User.entity'; | ||
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; | ||
import { Roles } from 'src/Infrastructure/User/Decorator/Roles'; | ||
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard'; | ||
import { LoggedUser } from '../Decorator/LoggedUser'; | ||
import { UserAuthView } from '../Security/UserAuthView'; | ||
|
||
@Controller('users') | ||
@ApiTags('User') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer'), RolesGuard) | ||
export class RemoveUserAction { | ||
constructor( | ||
@Inject('ICommandBus') | ||
private readonly commandBus: ICommandBus | ||
) {} | ||
|
||
@Delete(':id') | ||
@Roles(UserRole.PHOTOGRAPHER) | ||
@ApiOperation({ summary: 'Remove user' }) | ||
public async index( | ||
@Param() { id }: IdDTO, | ||
@LoggedUser() user: UserAuthView | ||
) { | ||
try { | ||
await this.commandBus.execute(new RemoveUserCommand(id, user.id)); | ||
} catch (e) { | ||
throw new BadRequestException(e.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
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
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
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
Oops, something went wrong.