-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#7] feat: add new models and dto for user
- Loading branch information
Showing
8 changed files
with
186 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Schema, model } from 'mongoose' | ||
import { IUser } from '../types/IUser' | ||
import { Proficiency } from '../types/Proficiency' | ||
import { Role } from '../types/Role' | ||
|
||
const UserSchema = new Schema<IUser>({ | ||
username: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
role: { | ||
type: String, | ||
enum: Object.values(Role), | ||
required: true, | ||
}, | ||
proficiency: { | ||
type: String, | ||
enum: Object.values(Proficiency), | ||
required: false, | ||
}, | ||
createdAt: { | ||
type: Date, | ||
required: false, | ||
}, | ||
updatedAt: { | ||
type: Date, | ||
required: false, | ||
default: null, | ||
}, | ||
deletedAt: { | ||
type: Date, | ||
required: false, | ||
default: null, | ||
}, | ||
}) | ||
|
||
export default model<IUser>('User', UserSchema) |
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,56 @@ | ||
import { | ||
IsEmail, | ||
IsEnum, | ||
IsNotEmpty, | ||
IsOptional, | ||
IsString, | ||
IsStrongPassword, | ||
validate, | ||
ValidationError, | ||
} from 'class-validator' | ||
import { Proficiency } from './Proficiency' | ||
import { Role } from './Role' | ||
import { TypedRequest } from './TypedRequest' | ||
|
||
export class CreateUserDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
username: string | ||
|
||
@IsStrongPassword({ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1 }) | ||
password: string | ||
|
||
@IsEmail() | ||
email: string | ||
|
||
@IsEnum(Role) | ||
role: Role | ||
|
||
@IsOptional() | ||
@IsEnum(Proficiency) | ||
proficiency?: Proficiency | ||
|
||
constructor(username: string, email: string, password: string, role: Role, proficiency?: Proficiency) { | ||
this.username = username | ||
this.email = email | ||
this.password = password | ||
this.role = role | ||
this.proficiency = proficiency | ||
} | ||
|
||
static fromRequest({ | ||
body: { username, email, password, role, proficiency }, | ||
}: TypedRequest<{ | ||
username: string | ||
password: string | ||
email: string | ||
role: Role | ||
proficiency: Proficiency | undefined | ||
}>): CreateUserDto { | ||
return new CreateUserDto(username, email, password, role, proficiency) | ||
} | ||
|
||
async validate(): Promise<ValidationError[]> { | ||
return validate(this) | ||
} | ||
} |
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,14 @@ | ||
import { Proficiency } from './Proficiency' | ||
import { Role } from './Role' | ||
|
||
export interface IUser { | ||
id: string | ||
username: string | ||
email: string | ||
password: string | ||
role: Role | ||
proficiency?: Proficiency | ||
createdAt?: Date | ||
updatedAt?: Date | ||
deletedAt?: Date | ||
} |
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,6 @@ | ||
export enum Proficiency { | ||
BEGINNER = 'BEGINNER', | ||
INTERMEDIATE = 'INTERMEDIATE', | ||
ADVANCED = 'ADVANCED', | ||
EXPERT = 'EXPERT', | ||
} |
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,4 @@ | ||
export enum Role { | ||
USER = 'USER', | ||
ADMIN = 'ADMIN', | ||
} |
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,5 @@ | ||
import { Request } from 'express' | ||
|
||
export interface TypedRequest<T> extends Request { | ||
body: T | ||
} |
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,53 @@ | ||
import { IsEmail, IsEnum, IsNotEmpty, IsOptional, IsString, validate, ValidationError } from 'class-validator' | ||
import { IUser } from './IUser' | ||
import { Proficiency } from './Proficiency' | ||
import { Role } from './Role' | ||
import { TypedRequest } from './TypedRequest' | ||
|
||
export class UserDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
id: string | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
username: string | ||
|
||
@IsEmail() | ||
email: string | ||
|
||
@IsEnum(Role) | ||
role: Role | ||
|
||
@IsOptional() | ||
@IsEnum(Proficiency) | ||
proficiency?: Proficiency | ||
|
||
constructor(id: string, username: string, email: string, role: Role, proficiency?: Proficiency) { | ||
this.id = id | ||
this.username = username | ||
this.email = email | ||
this.role = role | ||
this.proficiency = proficiency | ||
} | ||
|
||
static fromModel({ id, username, email, role, proficiency }: IUser): UserDto { | ||
return new UserDto(id, username, email, role, proficiency) | ||
} | ||
|
||
static fromRequest({ | ||
body: { id, username, email, role, proficiency }, | ||
}: TypedRequest<{ | ||
id: string | ||
username: string | ||
email: string | ||
role: Role | ||
proficiency: Proficiency | undefined | ||
}>): UserDto { | ||
return new UserDto(id, username, email, role, proficiency) | ||
} | ||
|
||
async validate(): Promise<ValidationError[]> { | ||
return validate(this) | ||
} | ||
} |
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