-
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.
Feat(Instructor): Edit instructor details use case
- Loading branch information
1 parent
ac062b2
commit 1771399
Showing
5 changed files
with
108 additions
and
2 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
45 changes: 45 additions & 0 deletions
45
src/domain/course-management/application/use-cases/edit-instructor-details.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,45 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { makeInstructor } from '../../../../../test/factories/make-instructor' | ||
import { InMemoryInstructorRepository } from '../../../../../test/repositories/in-memory-instructors-repository' | ||
import { EditInstructorDetailsUseCase } from './edit-instructor-details' | ||
|
||
let inMemoryInstructorsRepository: InMemoryInstructorRepository | ||
let sut: EditInstructorDetailsUseCase | ||
|
||
describe('Edit instructor details use case', () => { | ||
beforeEach(() => { | ||
inMemoryInstructorsRepository = new InMemoryInstructorRepository() | ||
sut = new EditInstructorDetailsUseCase(inMemoryInstructorsRepository) | ||
}) | ||
|
||
it('should be able to edit instructor details', async () => { | ||
const instructor = makeInstructor({ | ||
email: '[email protected]', | ||
name: 'John Doe' | ||
}) | ||
await inMemoryInstructorsRepository.create(instructor) | ||
|
||
const result = await sut.exec({ | ||
email: '[email protected]', | ||
instructorId: instructor.id.toString() | ||
}) | ||
|
||
expect(result.isRight()).toBe(true) | ||
expect(result.value).toMatchObject({ | ||
instructor: expect.objectContaining({ | ||
email: '[email protected]', | ||
name: 'John Doe' | ||
}) | ||
}) | ||
}) | ||
|
||
it('should not be able to edit a inexistent instructor', async () => { | ||
const result = await sut.exec({ | ||
email: '[email protected]', | ||
instructorId: 'inexistentInstructorId' | ||
}) | ||
|
||
expect(result.isLeft()).toBe(true) | ||
expect(result.value).toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
}) |
55 changes: 55 additions & 0 deletions
55
src/domain/course-management/application/use-cases/edit-instructor-details.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,55 @@ | ||
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 Instructor } from '../../enterprise/entities/instructor' | ||
import { type InstructorsRepository } from '../repositories/instructors-repository' | ||
|
||
interface EditInstructorDetailsUseCaseRequest { | ||
email?: string | ||
age?: number | ||
summary?: string | ||
profileImageKey?: string | null | ||
bannerImageKey?: string | null | ||
instructorId: string | ||
} | ||
|
||
type EditInstructorDetailsUseCaseResponse = Either< | ||
ResourceNotFoundError, | ||
{ | ||
instructor: Instructor | ||
} | ||
> | ||
|
||
export class EditInstructorDetailsUseCase implements UseCase<EditInstructorDetailsUseCaseRequest, EditInstructorDetailsUseCaseResponse> { | ||
constructor( | ||
private readonly instructorsRepository: InstructorsRepository | ||
) { } | ||
|
||
async exec({ | ||
email, | ||
age, | ||
summary, | ||
profileImageKey, | ||
bannerImageKey, | ||
instructorId | ||
}: EditInstructorDetailsUseCaseRequest): Promise<EditInstructorDetailsUseCaseResponse> { | ||
const instructor = await this.instructorsRepository.findById(instructorId) | ||
|
||
if (!instructor) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
instructor.name = instructor.name | ||
instructor.email = email ?? instructor.email | ||
instructor.age = age ?? instructor.age | ||
instructor.summary = summary ?? instructor.summary | ||
instructor.profileImageKey = profileImageKey ?? instructor.profileImageKey | ||
instructor.bannerImageKey = bannerImageKey ?? instructor.bannerImageKey | ||
|
||
await this.instructorsRepository.save(instructor) | ||
|
||
return right({ | ||
instructor | ||
}) | ||
} | ||
} |
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