Skip to content

Commit

Permalink
Feat(User): Delete user use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 12, 2024
1 parent 1771399 commit 4f2fce0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
- [ ] GET /users/:userId - Get user details
- [ ] POST /users - Register user
- [ ] PUT /users/:userId - Update user
- [ ] DELETE /users/:userId - Delete user - make use case
- [ ] DELETE /users/:userId - Delete user

### Sessions
- [ ] POST /sessions - User authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface UsersRepository {
findById: (id: string) => Promise<User<StudentProps | InstructorProps> | null>
findByEmail: (email: string) => Promise<User<StudentProps | InstructorProps> | null>
create: (user: User<StudentProps | InstructorProps>) => Promise<User<StudentProps | InstructorProps>>
delete: (user: User<StudentProps | InstructorProps>) => Promise<void>
}
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 src/domain/course-management/application/use-cases/delete-user.ts
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
})
}
}
5 changes: 5 additions & 0 deletions test/repositories/in-memory-users-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ export class InMemoryUsersRepository implements UsersRepository {
this.inMemoryInstructorsRepository.items.push(user)
return user
}

async delete(user: User<StudentProps | InstructorProps>): Promise<void> {
const userIndex = this.items.indexOf(user)
this.items.splice(userIndex, 1)
}
}

0 comments on commit 4f2fce0

Please sign in to comment.