Skip to content

Commit

Permalink
Update SuperUser domain model
Browse files Browse the repository at this point in the history
  • Loading branch information
madnoberson committed Sep 14, 2024
1 parent 60bc2c3 commit 1ad7bcc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/domain/models/superuser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class SuperUser {
public id: SuperUserId
public firstName: string
public lastName: string
public email: string
public isActive: boolean
public avatarUrl: string
public roles: Array<SuperUserRole>

Expand All @@ -14,13 +16,17 @@ export class SuperUser {
id: SuperUserId,
firstName: string,
lastName: string,
email: string,
isActive: boolean,
avatarUrl: string,
roles: Array<SuperUserRole>,
},
) {
this.id = superUserProps.id
this.firstName = superUserProps.firstName
this.lastName = superUserProps.lastName
this.email = superUserProps.email
this.isActive = superUserProps.isActive
this.avatarUrl = superUserProps.avatarUrl
this.roles = superUserProps.roles
}
Expand Down
8 changes: 8 additions & 0 deletions src/domain/services/createSuperUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SuperUserRole } from "src/domain/constants";
import { SuperUserId } from "src/domain/valueObjects";
import {
EmailValidator,
SuperUserFirstNameValidator,
SuperUserLastNameValidator,
} from "src/domain/validators";
Expand All @@ -10,33 +11,40 @@ import { SuperUser } from "src/domain/models";
export class CreateSuperUser {
protected readonly firstNameValidator: SuperUserFirstNameValidator
protected readonly lastNameValidator: SuperUserLastNameValidator
protected readonly emailValidator: EmailValidator

constructor(
createSuperUserProps: {
firstNameValidator: SuperUserFirstNameValidator,
lastNameValidator: SuperUserLastNameValidator,
emailValidator: EmailValidator,
},
) {
this.firstNameValidator = createSuperUserProps.firstNameValidator
this.lastNameValidator = createSuperUserProps.lastNameValidator
this.emailValidator = createSuperUserProps.emailValidator
}

execute(
createUserParams: {
id: SuperUserId,
firstName: string,
lastName: string,
email: string,
avatarUrl: string,
roles: Array<SuperUserRole>,
}
): SuperUser {
this.firstNameValidator.validate(createUserParams.firstName)
this.lastNameValidator.validate(createUserParams.lastName)
this.emailValidator.validate(createUserParams.email)

return new SuperUser({
id: createUserParams.id,
firstName: createUserParams.firstName,
lastName: createUserParams.lastName,
email: createUserParams.email,
isActive: true,
avatarUrl: createUserParams.avatarUrl,
roles: createUserParams.roles,
})
Expand Down
2 changes: 1 addition & 1 deletion src/domain/services/createUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UserId } from "src/domain/valueObjects";
import {
UserNameValidator,
EmailValidator,
UserNameValidator,
TelegramValidator,
} from "src/domain/validators";
import { User } from "src/domain/models";
Expand Down
14 changes: 14 additions & 0 deletions src/domain/validators/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { InvalidEmailError } from "src/domain/errors"


const EMAIL_VALIDATION_PATTERN =
/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b/


export class EmailValidator {
validate(value: string): void {
if (!EMAIL_VALIDATION_PATTERN.test(value)) {
throw new InvalidEmailError()
}
}
}
3 changes: 2 additions & 1 deletion src/domain/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./common";
export * from "./user";
export * from "./superUser"
export * from "./superUser";
13 changes: 0 additions & 13 deletions src/domain/validators/user.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import {
InvalidUserNameError,
InvalidEmailError,
InvalidTelegramError
} from "src/domain/errors"


const USER_NAME_MIN_LENGTH = 5
const USER_NAME_MAX_LENGTH = 64

const EMAIL_VALIDATION_PATTERN =
/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b/

const TELEGRAM_MIN_LENGTH = 5
const TELEGRAM_MAX_LENGTH = 32
const TELEGRAM_VALIDATION_PATTERN = /^[a-zA-Z0-9_]+$/
Expand All @@ -30,15 +26,6 @@ export class UserNameValidator {
}


export class EmailValidator {
validate(value: string): void {
if (!EMAIL_VALIDATION_PATTERN.test(value)) {
throw new InvalidEmailError()
}
}
}


export class TelegramValidator {
validate(value: string): void {
if (
Expand Down

0 comments on commit 1ad7bcc

Please sign in to comment.