-
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(course): adding relations at the enrollment
- Loading branch information
1 parent
ac6ace4
commit 3731589
Showing
9 changed files
with
207 additions
and
160 deletions.
There are no files selected for viewing
44 changes: 30 additions & 14 deletions
44
src/modules/course/domain/entities/enrollment/enrollment.entity.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 |
---|---|---|
@@ -1,45 +1,61 @@ | ||
import { UserEntity } from '@modules/user/domain/entities/user/user.entity'; | ||
import { BaseEntity, BaseEntityProps } from '@shared/domain/base.entity'; | ||
import { Replace } from '@shared/helpers/replace'; | ||
import { Either, Right } from '@shared/helpers/either'; | ||
import { UserEntity } from '@modules/user/domain/entities/user/user.entity'; | ||
import { Replace } from '@shared/helpers/replace'; | ||
import { UUID } from 'crypto'; | ||
import { CourseEntity } from '../course/course.entity'; | ||
|
||
export interface EnrollmentEntityProps { | ||
student: UserEntity; | ||
course: CourseEntity; | ||
studentId: UUID; | ||
courseId: UUID; | ||
student?: UserEntity; | ||
course?: CourseEntity; | ||
} | ||
|
||
export type EnrollmentEntityCreateProps = Replace<EnrollmentEntityProps, {}>; | ||
export type EnrollmentEntityCreateProps = Replace< | ||
EnrollmentEntityProps, | ||
unknown | ||
>; | ||
|
||
export class EnrollmentEntity extends BaseEntity<EnrollmentEntityProps> { | ||
private constructor( | ||
props: EnrollmentEntityProps, | ||
baseEntityProps?: BaseEntityProps | ||
baseEntityProps?: BaseEntityProps, | ||
) { | ||
super(props, baseEntityProps); | ||
Object.freeze(this); | ||
} | ||
|
||
static create( | ||
{ student, course }: EnrollmentEntityCreateProps, | ||
baseEntityProps?: BaseEntityProps | ||
{ studentId, courseId, student, course }: EnrollmentEntityCreateProps, | ||
baseEntityProps?: BaseEntityProps, | ||
): Either<Error, EnrollmentEntity> { | ||
return new Right( | ||
new EnrollmentEntity( | ||
{ | ||
studentId, | ||
courseId, | ||
student, | ||
course, | ||
}, | ||
baseEntityProps | ||
) | ||
baseEntityProps, | ||
), | ||
); | ||
} | ||
|
||
public get student(): UserEntity { | ||
return this.props.student; | ||
public get studentId(): UUID { | ||
return this.props.studentId; | ||
} | ||
|
||
public get courseId(): UUID { | ||
return this.props.courseId; | ||
} | ||
|
||
public get student(): UserEntity | null { | ||
return this.props.student || null; | ||
} | ||
|
||
public get course(): CourseEntity { | ||
return this.props.course; | ||
public get course(): CourseEntity | null { | ||
return this.props.course || null; | ||
} | ||
} |
128 changes: 64 additions & 64 deletions
128
src/modules/course/domain/usecases/enroll-student-in-course.usecase.test.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 |
---|---|---|
@@ -1,117 +1,117 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { EnrollStudentInCourseUseCase } from './enroll-student-in-course.usecase' | ||
import { UUID } from 'crypto' | ||
import { InMemoryRepository } from 'test/repositories/in-memory-repository' | ||
import { EnrollmentEntity } from '../entities/enrollment/enrollment.entity' | ||
import { EnrollmentRepository } from '../repositories/enrollment.repository' | ||
import { InMemoryEnrollmentRepository } from 'test/repositories/in-memory-enrollment-repository' | ||
import { StudentNotFoundError } from '../errors/student-not-found.error' | ||
import { UserRepository } from '@modules/user/domain/repositories/user.repository' | ||
import { UserEntity } from '@modules/user/domain/entities/user/user.entity' | ||
import { InMemoryUserRepository } from 'test/repositories/in-memory-user-repository' | ||
import { MockUser } from 'test/factories/mock-user' | ||
import { CourseRepository } from '../repositories/course.repository' | ||
import { CourseEntity } from '../entities/course/course.entity' | ||
import { InMemoryCourseRepository } from 'test/repositories/in-memory-course-repository' | ||
import { MockCourse } from 'test/factories/mock-course' | ||
import { CourseNotFoundError } from '../errors/course-not-found.error' | ||
import { StudentAlreadyEnrolledError } from '../errors/student-already-enrolled.error' | ||
import { faker } from '@faker-js/faker'; | ||
import { UserEntity } from '@modules/user/domain/entities/user/user.entity'; | ||
import { UserRepository } from '@modules/user/domain/repositories/user.repository'; | ||
import { UUID } from 'crypto'; | ||
import { MockCourse } from 'test/factories/mock-course'; | ||
import { MockUser } from 'test/factories/mock-user'; | ||
import { InMemoryCourseRepository } from 'test/repositories/in-memory-course-repository'; | ||
import { InMemoryEnrollmentRepository } from 'test/repositories/in-memory-enrollment-repository'; | ||
import { InMemoryRepository } from 'test/repositories/in-memory-repository'; | ||
import { InMemoryUserRepository } from 'test/repositories/in-memory-user-repository'; | ||
import { CourseEntity } from '../entities/course/course.entity'; | ||
import { EnrollmentEntity } from '../entities/enrollment/enrollment.entity'; | ||
import { CourseNotFoundError } from '../errors/course-not-found.error'; | ||
import { StudentAlreadyEnrolledError } from '../errors/student-already-enrolled.error'; | ||
import { StudentNotFoundError } from '../errors/student-not-found.error'; | ||
import { CourseRepository } from '../repositories/course.repository'; | ||
import { EnrollmentRepository } from '../repositories/enrollment.repository'; | ||
import { EnrollStudentInCourseUseCase } from './enroll-student-in-course.usecase'; | ||
|
||
describe('EnrollStudentInCourseUseCase', () => { | ||
let usecase: EnrollStudentInCourseUseCase | ||
let usecase: EnrollStudentInCourseUseCase; | ||
let enrollmentRespository: InMemoryRepository< | ||
EnrollmentRepository, | ||
EnrollmentEntity | ||
> | ||
let userRepository: InMemoryRepository<UserRepository, UserEntity> | ||
let courseRepository: InMemoryRepository<CourseRepository, CourseEntity> | ||
>; | ||
let userRepository: InMemoryRepository<UserRepository, UserEntity>; | ||
let courseRepository: InMemoryRepository<CourseRepository, CourseEntity>; | ||
|
||
beforeEach(() => { | ||
enrollmentRespository = new InMemoryEnrollmentRepository() | ||
userRepository = new InMemoryUserRepository() | ||
courseRepository = new InMemoryCourseRepository() | ||
enrollmentRespository = new InMemoryEnrollmentRepository(); | ||
userRepository = new InMemoryUserRepository(); | ||
courseRepository = new InMemoryCourseRepository(); | ||
usecase = new EnrollStudentInCourseUseCase( | ||
enrollmentRespository, | ||
userRepository, | ||
courseRepository, | ||
) | ||
}) | ||
); | ||
}); | ||
|
||
const createStudentWithId = (id: UUID) => { | ||
userRepository.save(MockUser.createEntity({ basePropsOverride: { id } })) | ||
} | ||
userRepository.save(MockUser.createEntity({ basePropsOverride: { id } })); | ||
}; | ||
|
||
const createCourseWithId = (id: UUID) => { | ||
courseRepository.save( | ||
MockCourse.createEntity({ basePropsOverride: { id } }), | ||
) | ||
} | ||
); | ||
}; | ||
|
||
it('should persist the enrollment with the correct student and course', async () => { | ||
const studentId = faker.string.uuid() as UUID | ||
const courseId = faker.string.uuid() as UUID | ||
const studentId = faker.string.uuid() as UUID; | ||
const courseId = faker.string.uuid() as UUID; | ||
|
||
createStudentWithId(studentId) | ||
createCourseWithId(courseId) | ||
createStudentWithId(studentId); | ||
createCourseWithId(courseId); | ||
|
||
await usecase.exec({ | ||
studentId, | ||
courseId, | ||
}) | ||
}); | ||
|
||
expect(enrollmentRespository.items).toHaveLength(1) | ||
expect(enrollmentRespository.items[0].student.id).toEqual(studentId) | ||
expect(enrollmentRespository.items[0].course.id).toEqual(courseId) | ||
}) | ||
expect(enrollmentRespository.items).toHaveLength(1); | ||
expect(enrollmentRespository.items[0].studentId).toEqual(studentId); | ||
expect(enrollmentRespository.items[0].courseId).toEqual(courseId); | ||
}); | ||
|
||
it('should return an error if the student does not exists', async () => { | ||
const studentId = faker.string.uuid() as UUID | ||
const courseId = faker.string.uuid() as UUID | ||
const studentId = faker.string.uuid() as UUID; | ||
const courseId = faker.string.uuid() as UUID; | ||
|
||
createCourseWithId(courseId) | ||
createCourseWithId(courseId); | ||
|
||
const result = await usecase.exec({ | ||
studentId, | ||
courseId, | ||
}) | ||
}); | ||
|
||
expect(result.isLeft()).toBeTruthy() | ||
expect(result.value).toBeInstanceOf(StudentNotFoundError) | ||
}) | ||
expect(result.isLeft()).toBeTruthy(); | ||
expect(result.value).toBeInstanceOf(StudentNotFoundError); | ||
}); | ||
|
||
it('should return an error if the course does not exists', async () => { | ||
const studentId = faker.string.uuid() as UUID | ||
const courseId = faker.string.uuid() as UUID | ||
const studentId = faker.string.uuid() as UUID; | ||
const courseId = faker.string.uuid() as UUID; | ||
|
||
createStudentWithId(studentId) | ||
createStudentWithId(studentId); | ||
|
||
const result = await usecase.exec({ | ||
studentId, | ||
courseId, | ||
}) | ||
}); | ||
|
||
expect(result.isLeft()).toBeTruthy() | ||
expect(result.value).toBeInstanceOf(CourseNotFoundError) | ||
}) | ||
expect(result.isLeft()).toBeTruthy(); | ||
expect(result.value).toBeInstanceOf(CourseNotFoundError); | ||
}); | ||
|
||
it('should return an error if the the user is already enrolled at the course', async () => { | ||
const studentId = faker.string.uuid() as UUID | ||
const courseId = faker.string.uuid() as UUID | ||
const studentId = faker.string.uuid() as UUID; | ||
const courseId = faker.string.uuid() as UUID; | ||
|
||
createStudentWithId(studentId) | ||
createCourseWithId(courseId) | ||
createStudentWithId(studentId); | ||
createCourseWithId(courseId); | ||
|
||
await usecase.exec({ | ||
studentId, | ||
courseId, | ||
}) | ||
}); | ||
|
||
const result = await usecase.exec({ | ||
studentId, | ||
courseId, | ||
}) | ||
}); | ||
|
||
expect(result.isLeft()).toBeTruthy() | ||
expect(result.value).toBeInstanceOf(StudentAlreadyEnrolledError) | ||
}) | ||
}) | ||
expect(result.isLeft()).toBeTruthy(); | ||
expect(result.value).toBeInstanceOf(StudentAlreadyEnrolledError); | ||
}); | ||
}); |
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.