-
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.
Merge pull request #1 from leonardodimarchi/feat/password-reset
Feat/password-reset
- Loading branch information
Showing
50 changed files
with
977 additions
and
68 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
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
5 changes: 4 additions & 1 deletion
5
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
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,12 +1,12 @@ | ||
import { DomainError } from '@shared/domain/domain.error' | ||
import { DomainError } from '@shared/domain/errors/domain.error'; | ||
|
||
export class CourseNotFoundError extends Error implements DomainError { | ||
public courseId: string | ||
public courseId: string; | ||
|
||
constructor(courseId: string) { | ||
super(`Course not found: ${courseId}`) | ||
super(`Course not found: ${courseId}`); | ||
|
||
this.courseId = courseId | ||
this.name = 'CourseNotFoundError' | ||
this.courseId = courseId; | ||
this.name = 'CourseNotFoundError'; | ||
} | ||
} |
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
14 changes: 7 additions & 7 deletions
14
src/modules/course/domain/errors/student-already-enrolled.error.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,14 +1,14 @@ | ||
import { DomainError } from '@shared/domain/domain.error' | ||
import { DomainError } from '@shared/domain/errors/domain.error'; | ||
|
||
export class StudentAlreadyEnrolledError extends Error implements DomainError { | ||
public studentId: string | ||
public courseId: string | ||
public studentId: string; | ||
public courseId: string; | ||
|
||
constructor(studentId: string, courseId: string) { | ||
super(`Enrollment already exists: ${studentId} at ${courseId}`) | ||
super(`Enrollment already exists: ${studentId} at ${courseId}`); | ||
|
||
this.studentId = studentId | ||
this.courseId = courseId | ||
this.name = 'StudentAlreadyEnrolledError' | ||
this.studentId = studentId; | ||
this.courseId = courseId; | ||
this.name = 'StudentAlreadyEnrolledError'; | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
src/modules/course/domain/errors/student-not-found.error.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,12 +1,12 @@ | ||
import { DomainError } from '@shared/domain/domain.error' | ||
import { DomainError } from '@shared/domain/errors/domain.error'; | ||
|
||
export class StudentNotFoundError extends Error implements DomainError { | ||
public studentId: string | ||
public studentId: string; | ||
|
||
constructor(studentId: string) { | ||
super(`Student not found: ${studentId}`) | ||
super(`Student not found: ${studentId}`); | ||
|
||
this.studentId = studentId | ||
this.name = 'StudentNotFoundError' | ||
this.studentId = studentId; | ||
this.name = 'StudentNotFoundError'; | ||
} | ||
} |
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
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
92 changes: 92 additions & 0 deletions
92
src/modules/password-reset/domain/entities/password-reset.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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { UserEntity } from '@modules/user/domain/entities/user/user.entity'; | ||
import { | ||
BaseEntity, | ||
BaseEntityProps, | ||
} from '@shared/domain/entities/base.entity'; | ||
import { Either, Right } from '@shared/helpers/either'; | ||
import { Replace } from '@shared/helpers/replace'; | ||
import { UUID } from 'node:crypto'; | ||
|
||
export interface PasswordResetEntityProps { | ||
userId: UUID; | ||
user?: UserEntity; | ||
code: string; | ||
validUntil: Date; | ||
used: boolean; | ||
} | ||
|
||
export type PasswordResetEntityCreateProps = Replace< | ||
PasswordResetEntityProps, | ||
{ | ||
code?: string; | ||
validUntil?: Date; | ||
used?: boolean; | ||
} | ||
>; | ||
|
||
export class PasswordResetEntity extends BaseEntity<PasswordResetEntityProps> { | ||
private constructor( | ||
props: PasswordResetEntityProps, | ||
baseEntityProps?: BaseEntityProps, | ||
) { | ||
super(props, baseEntityProps); | ||
Object.freeze(this); | ||
} | ||
|
||
static create( | ||
{ userId, code, user, validUntil, used }: PasswordResetEntityCreateProps, | ||
baseEntityProps?: BaseEntityProps, | ||
): Either<Error, PasswordResetEntity> { | ||
const day = 24 * 60 * 60 * 1000; | ||
const tomorrow = new Date(+new Date() + day); | ||
|
||
return new Right( | ||
new PasswordResetEntity( | ||
{ | ||
userId, | ||
user, | ||
code: code || this.generateCode(), | ||
validUntil: validUntil || tomorrow, | ||
used: used ?? false, | ||
}, | ||
baseEntityProps, | ||
), | ||
); | ||
} | ||
|
||
static generateCode(): string { | ||
const codeSize = 8; | ||
|
||
return [...Array(codeSize)] | ||
.map(() => | ||
Math.floor(Math.random() * 16) | ||
.toString(16) | ||
.toUpperCase(), | ||
) | ||
.join(''); | ||
} | ||
|
||
public get userId(): UUID { | ||
return this.props.userId; | ||
} | ||
|
||
public get user(): UserEntity | null { | ||
return this.props.user || null; | ||
} | ||
|
||
public get code(): string { | ||
return this.props.code; | ||
} | ||
|
||
public get validUntil(): Date { | ||
return this.props.validUntil; | ||
} | ||
|
||
public get used(): boolean { | ||
return this.props.used; | ||
} | ||
|
||
public setAsUsed(): void { | ||
this.props.used = true; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/modules/password-reset/domain/errors/incorrect-old-password.error.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,9 @@ | ||
import { DomainError } from '@shared/domain/errors/domain.error'; | ||
|
||
export class IncorrectOldPasswordError extends Error implements DomainError { | ||
constructor() { | ||
super(`Old password confirmation failed`); | ||
|
||
this.name = 'IncorrectOldPasswordError'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/modules/password-reset/domain/errors/password-reset-not-found.error.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,9 @@ | ||
import { DomainError } from '@shared/domain/errors/domain.error'; | ||
|
||
export class PasswordResetNotFoundError extends Error implements DomainError { | ||
constructor() { | ||
super(`PasswordReset not found`); | ||
|
||
this.name = 'PasswordResetNotFoundError'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/modules/password-reset/domain/repositories/password-reset.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { UUID } from 'node:crypto'; | ||
import { PasswordResetEntity } from '../entities/password-reset.entity'; | ||
|
||
export abstract class PasswordResetRepository { | ||
abstract save(entity: PasswordResetEntity): Promise<void>; | ||
abstract getById(id: UUID): Promise<PasswordResetEntity | null>; | ||
abstract getValidByUserId(userId: UUID): Promise<PasswordResetEntity | null>; | ||
abstract getValidByCode(code: string): Promise<PasswordResetEntity | null>; | ||
} |
Oops, something went wrong.