-
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
1 parent
1771399
commit 4f2fce0
Showing
5 changed files
with
97 additions
and
1 deletion.
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
50 changes: 50 additions & 0 deletions
50
src/domain/course-management/application/use-cases/delete-user.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,50 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { makeStudent } from '../../../../../test/factories/make-student' | ||
import { InMemoryStudentsRepository } from '../../../../../test/repositories/in-memory-students-repository' | ||
import { InMemoryInstructorRepository } from './../../../../../test/repositories/in-memory-instructors-repository' | ||
import { InMemoryUsersRepository } from './../../../../../test/repositories/in-memory-users-repository' | ||
import { DeleteUserUseCase } from './delete-user' | ||
|
||
let inMemoryInstructorsRepository: InMemoryInstructorRepository | ||
let inMemoryStudentsRepository: InMemoryStudentsRepository | ||
let inMemoryUsersRepository: InMemoryUsersRepository | ||
let sut: DeleteUserUseCase | ||
|
||
describe('Delete user use case', () => { | ||
beforeEach(() => { | ||
inMemoryInstructorsRepository = new InMemoryInstructorRepository() | ||
inMemoryStudentsRepository = new InMemoryStudentsRepository() | ||
|
||
inMemoryUsersRepository = new InMemoryUsersRepository(inMemoryInstructorsRepository, inMemoryStudentsRepository) | ||
|
||
sut = new DeleteUserUseCase(inMemoryUsersRepository) | ||
}) | ||
|
||
it('should be able to delete a user', async () => { | ||
const student = makeStudent({ | ||
email: '[email protected]' | ||
}) | ||
await inMemoryUsersRepository.create(student) | ||
|
||
const result = await sut.exec({ | ||
userId: student.id.toString() | ||
}) | ||
|
||
expect(result.isRight()).toBe(true) | ||
expect(result.value).toMatchObject({ | ||
user: { | ||
email: '[email protected]' | ||
} | ||
}) | ||
expect(inMemoryUsersRepository.items).toHaveLength(0) | ||
}) | ||
|
||
it('should not be able to delete a inexistent user', async () => { | ||
const result = await sut.exec({ | ||
userId: 'inexistentUserId' | ||
}) | ||
|
||
expect(result.isLeft()).toBe(true) | ||
expect(result.value).toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
}) |
40 changes: 40 additions & 0 deletions
40
src/domain/course-management/application/use-cases/delete-user.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,40 @@ | ||
import { left, right, type Either } from '@/core/either' | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { type UseCase } from '@/core/use-cases/use-case' | ||
import { type InstructorProps } from '../../enterprise/entities/instructor' | ||
import { type StudentProps } from '../../enterprise/entities/student' | ||
import { type User } from '../../enterprise/entities/user' | ||
import { type UsersRepository } from './../repositories/users-repository' | ||
|
||
interface DeleteUserUseCaseRequest { | ||
userId: string | ||
} | ||
|
||
type DeleteUserUseCaseResponse = Either< | ||
ResourceNotFoundError, | ||
{ | ||
user: User<StudentProps | InstructorProps> | ||
} | ||
> | ||
|
||
export class DeleteUserUseCase implements UseCase<DeleteUserUseCaseRequest, DeleteUserUseCaseResponse> { | ||
constructor( | ||
private readonly usersRepository: UsersRepository | ||
) { } | ||
|
||
async exec({ | ||
userId | ||
}: DeleteUserUseCaseRequest): Promise<DeleteUserUseCaseResponse> { | ||
const user = await this.usersRepository.findById(userId) | ||
|
||
if (!user) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
await this.usersRepository.delete(user) | ||
|
||
return right({ | ||
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