-
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(Student, Course, Enrollment): Enroll student to course use case
- Loading branch information
1 parent
728cf59
commit 6aae7c3
Showing
9 changed files
with
216 additions
and
6 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
7 changes: 7 additions & 0 deletions
7
src/domain/course-management/application/repositories/enrollments.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,7 @@ | ||
import { type Enrollment } from '../../enterprise/entities/enrollment' | ||
|
||
export interface EnrollmentsRepository { | ||
findById: (id: string) => Promise<Enrollment | null> | ||
findByStudentIdAndCourseId: (studentId: string, courseId: string) => Promise<Enrollment | null> | ||
create: (enrollment: Enrollment) => Promise<Enrollment> | ||
} |
1 change: 1 addition & 0 deletions
1
src/domain/course-management/application/repositories/students-repository.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
87 changes: 87 additions & 0 deletions
87
src/domain/course-management/application/use-cases/enroll-to-course.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,87 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { makeCourse } from '../../../../../test/factories/make-course' | ||
import { makeStudent } from '../../../../../test/factories/make-student' | ||
import { InMemoryClassesRepository } from '../../../../../test/repositories/in-memory-classes-repository' | ||
import { InMemoryCoursesRepository } from '../../../../../test/repositories/in-memory-courses-repository' | ||
import { InMemoryEnrollmentsRepository } from '../../../../../test/repositories/in-memory-enrollments-repository' | ||
import { InMemoryStudentsRepository } from '../../../../../test/repositories/in-memory-students-repository' | ||
import { InMemoryInstructorRepository } from './../../../../../test/repositories/in-memory-instructors-repository' | ||
import { InMemoryModulesRepository } from './../../../../../test/repositories/in-memory-modules-repository' | ||
import { EnrollToCourseUseCase } from './enroll-to-course' | ||
import { AlreadyEnrolledInThisCourse } from './errors/already-enrolled-in-this-course' | ||
|
||
let inMemoryEnrollmentsRepository: InMemoryEnrollmentsRepository | ||
let inMemoryClassesRepository: InMemoryClassesRepository | ||
let inMemoryInstructorsRepository: InMemoryInstructorRepository | ||
let inMemoryStudentsRepository: InMemoryStudentsRepository | ||
let inMemoryModulesRepository: InMemoryModulesRepository | ||
let inMemoryCoursesRepository: InMemoryCoursesRepository | ||
let sut: EnrollToCourseUseCase | ||
|
||
describe('Enroll to course use case', () => { | ||
beforeEach(() => { | ||
inMemoryEnrollmentsRepository = new InMemoryEnrollmentsRepository() | ||
inMemoryClassesRepository = new InMemoryClassesRepository() | ||
inMemoryInstructorsRepository = new InMemoryInstructorRepository() | ||
inMemoryStudentsRepository = new InMemoryStudentsRepository() | ||
|
||
inMemoryModulesRepository = new InMemoryModulesRepository(inMemoryClassesRepository) | ||
inMemoryCoursesRepository = new InMemoryCoursesRepository(inMemoryModulesRepository, inMemoryInstructorsRepository) | ||
|
||
sut = new EnrollToCourseUseCase(inMemoryEnrollmentsRepository, inMemoryStudentsRepository, inMemoryCoursesRepository) | ||
}) | ||
|
||
it('should be able to enroll to a course', async () => { | ||
const student = makeStudent() | ||
await inMemoryStudentsRepository.create(student) | ||
|
||
const course = makeCourse() | ||
await inMemoryCoursesRepository.create(course) | ||
|
||
const result = await sut.exec({ | ||
studentId: student.id.toString(), | ||
courseId: course.id.toString() | ||
}) | ||
|
||
expect(result.isRight()).toBe(true) | ||
expect(result.value).toMatchObject({ | ||
enrollment: expect.objectContaining({ | ||
studentId: student.id | ||
}) | ||
}) | ||
}) | ||
|
||
it('should not be able to enroll to a inexistent course', async () => { | ||
const student = makeStudent() | ||
await inMemoryStudentsRepository.create(student) | ||
|
||
const result = await sut.exec({ | ||
studentId: student.id.toString(), | ||
courseId: 'inexistentCourseId' | ||
}) | ||
|
||
expect(result.isLeft()).toBe(true) | ||
expect(result.value).toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
|
||
it('an student should not be able to enroll to a course twice', async () => { | ||
const student = makeStudent() | ||
await inMemoryStudentsRepository.create(student) | ||
|
||
const course = makeCourse() | ||
await inMemoryCoursesRepository.create(course) | ||
|
||
await sut.exec({ | ||
studentId: student.id.toString(), | ||
courseId: course.id.toString() | ||
}) | ||
|
||
const result = await sut.exec({ | ||
studentId: student.id.toString(), | ||
courseId: course.id.toString() | ||
}) | ||
|
||
expect(result.isLeft()).toBe(true) | ||
expect(result.value).toBeInstanceOf(AlreadyEnrolledInThisCourse) | ||
}) | ||
}) |
59 changes: 59 additions & 0 deletions
59
src/domain/course-management/application/use-cases/enroll-to-course.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,59 @@ | ||
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 { Enrollment } from '../../enterprise/entities/enrollment' | ||
import { type CoursesRepository } from '../repositories/courses-repository' | ||
import { type EnrollmentsRepository } from '../repositories/enrollments' | ||
import { type StudentsRepository } from '../repositories/students-repository' | ||
import { AlreadyEnrolledInThisCourse } from './errors/already-enrolled-in-this-course' | ||
|
||
interface EnrollToCourseUseCaseRequest { | ||
studentId: string | ||
courseId: string | ||
} | ||
|
||
type EnrollToCourseUseCaseResponse = Either< | ||
ResourceNotFoundError, | ||
{ | ||
enrollment: Enrollment | ||
} | ||
> | ||
|
||
export class EnrollToCourseUseCase implements UseCase<EnrollToCourseUseCaseRequest, EnrollToCourseUseCaseResponse> { | ||
constructor( | ||
private readonly enrollmentsRepository: EnrollmentsRepository, | ||
private readonly studentsRepository: StudentsRepository, | ||
private readonly coursesRepository: CoursesRepository | ||
) { } | ||
|
||
async exec({ | ||
studentId, | ||
courseId | ||
}: EnrollToCourseUseCaseRequest): Promise<EnrollToCourseUseCaseResponse> { | ||
const [student, course] = await Promise.all([ | ||
this.studentsRepository.findById(studentId), | ||
this.coursesRepository.findById(courseId) | ||
]) | ||
|
||
if (!student || !course) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
const enrollmentAlreadyExists = await this.enrollmentsRepository.findByStudentIdAndCourseId(studentId, courseId) | ||
|
||
if (enrollmentAlreadyExists) { | ||
return left(new AlreadyEnrolledInThisCourse(course.name)) | ||
} | ||
|
||
const enrollment = Enrollment.create({ | ||
studentId: student.id, | ||
courseId: course.id | ||
}) | ||
|
||
await this.enrollmentsRepository.create(enrollment) | ||
|
||
return right({ | ||
enrollment | ||
}) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/domain/course-management/application/use-cases/errors/already-enrolled-in-this-course.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,7 @@ | ||
import { type UseCaseError } from '@/core/errors/use-case-error' | ||
|
||
export class AlreadyEnrolledInThisCourse extends Error implements UseCaseError { | ||
constructor(courseName: string) { | ||
super(`Already Enrolled in this course: ${courseName}.`) | ||
} | ||
} |
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,37 @@ | ||
import { type EnrollmentsRepository } from '@/domain/course-management/application/repositories/enrollments' | ||
import { type Enrollment } from '@/domain/course-management/enterprise/entities/enrollment' | ||
|
||
export class InMemoryEnrollmentsRepository implements EnrollmentsRepository { | ||
public items: Enrollment[] = [] | ||
|
||
async findById(id: string): Promise<Enrollment | null> { | ||
const enrollment = this.items.find(enrollmentToCompare => enrollmentToCompare.id.toString() === id) | ||
|
||
if (!enrollment) { | ||
return null | ||
} | ||
|
||
return enrollment | ||
} | ||
|
||
async findByStudentIdAndCourseId(studentId: string, courseId: string): Promise<Enrollment | null> { | ||
const enrollment = this.items.find(enrollmentToCompare => { | ||
if (enrollmentToCompare.studentId.toString() === studentId && enrollmentToCompare.courseId.toString() === courseId) { | ||
return enrollmentToCompare | ||
} | ||
|
||
return undefined | ||
}) | ||
|
||
if (!enrollment) { | ||
return null | ||
} | ||
|
||
return enrollment | ||
} | ||
|
||
async create(enrollment: Enrollment): Promise<Enrollment> { | ||
this.items.push(enrollment) | ||
return enrollment | ||
} | ||
} |
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