-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1518e3d
commit 92aa10a
Showing
10 changed files
with
1,166 additions
and
102 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
IsEmail, | ||
IsNotEmpty, | ||
IsOptional, | ||
IsString, | ||
IsNumber, | ||
IsUUID, | ||
Min, | ||
MinLength, | ||
} from 'class-validator'; | ||
import { Exclude, Expose } from 'class-transformer'; | ||
|
||
export class UserDTO { | ||
@IsUUID() | ||
@IsOptional() | ||
@Expose() | ||
id?: string; // Optional: Assigned automatically upon creation | ||
|
||
@IsString() | ||
@IsNotEmpty({ message: 'Username is required' }) | ||
@MinLength(3, { message: 'Username must be at least 3 characters long' }) | ||
@Expose() | ||
username: string; | ||
|
||
@IsEmail({}, { message: 'Invalid email address' }) | ||
@IsNotEmpty({ message: 'Email is required' }) | ||
@Expose() | ||
email: string; | ||
|
||
@IsString() | ||
@IsNotEmpty({ message: 'Password is required' }) | ||
@MinLength(8, { message: 'Password must be at least 8 characters long' }) | ||
@Exclude({ toPlainOnly: true }) // Exclude password when converting the object to plain data | ||
password: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
@Expose() | ||
avatar?: string; // Optional: Profile picture URL | ||
|
||
@IsNumber() | ||
@Min(0, { message: 'Tokens cannot be negative' }) | ||
@Expose() | ||
tokens: number; | ||
|
||
@IsNumber() | ||
@Min(0, { message: 'Score cannot be negative' }) | ||
@Expose() | ||
totalScore: number; | ||
|
||
@IsNumber() | ||
@Min(0, { message: 'Games played cannot be negative' }) | ||
@Expose() | ||
gamesPlayed: number; | ||
|
||
@IsNumber() | ||
@Min(0, { message: 'Games won cannot be negative' }) | ||
@Expose() | ||
gamesWon: number; | ||
|
||
@Expose() | ||
createdAt?: Date; // Optional: Automatically set on creation | ||
|
||
@Expose() | ||
updatedAt?: Date; // Optional: Automatically set on updates | ||
} |
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,58 @@ | ||
import { | ||
BadRequestException, | ||
Injectable, | ||
RequestTimeoutException, | ||
} from '@nestjs/common'; | ||
import { HashingProvider } from './hashing.provider'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { User } from '../user.entity'; | ||
import { Repository } from 'typeorm'; | ||
import { UserDTO } from '../dtos/create-user.dto'; | ||
|
||
@Injectable() | ||
export class CreateUserProvider { | ||
constructor( | ||
@InjectRepository(User) | ||
private readonly userRepository: Repository<User>, | ||
|
||
private readonly hashingProvider: HashingProvider, | ||
) {} | ||
|
||
public async createUsers(userDto: UserDTO) { | ||
// check if user already exits | ||
let existingUser: User; | ||
|
||
try { | ||
existingUser = await this.userRepository.findOne({ | ||
where: { email: userDto.email }, | ||
}); | ||
} catch (error) { | ||
throw new RequestTimeoutException( | ||
'Unable to process your request at the moment, Please try later', | ||
{ | ||
description: 'Error processing your request', | ||
}, | ||
); | ||
} | ||
// Handle Error | ||
if (existingUser) { | ||
throw new BadRequestException('User already exist'); | ||
} | ||
// Create the user | ||
let newUser = this.userRepository.create({ | ||
...userDto, | ||
password: await this.hashingProvider.hashPassword(userDto.password), | ||
}); | ||
try { | ||
newUser = await this.userRepository.save(newUser); | ||
} catch (error) { | ||
throw new RequestTimeoutException( | ||
'Unable to process your request at the moment, Please try later', | ||
{ | ||
description: 'Error processing your request', | ||
}, | ||
); | ||
} | ||
return [newUser]; | ||
} | ||
} |
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,11 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import * as bcrypt from 'bcrypt'; | ||
|
||
@Injectable() | ||
export class HashingProvider { | ||
// hashing | ||
public async hashPassword(data: string | Buffer): Promise<string> { | ||
const salt = await bcrypt.genSalt(); | ||
return bcrypt.hash(data, salt); | ||
} | ||
} |
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,45 @@ | ||
import { Exclude } from 'class-transformer'; | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
|
||
@Entity('users') | ||
export class User { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; // Unique identifier for the user | ||
|
||
@Column({ unique: true }) | ||
username: string; // The username for the player | ||
|
||
@Column({ unique: true }) | ||
email: string; // Player's email for account identification and recovery | ||
|
||
@Exclude() // passwords should not be return when user is return | ||
@Column() | ||
password: string; // Hashed password for security | ||
|
||
@Column({ nullable: true }) | ||
avatar: string; // Optional profile picture URL | ||
|
||
@Column({ default: 0 }) | ||
tokens: number; // Amount of in-game tokens the player owns | ||
|
||
@Column({ default: 0 }) | ||
totalScore: number; // Cumulative score across all games played | ||
|
||
@Column({ default: 0 }) | ||
gamesPlayed: number; // Total number of games the user has participated in | ||
|
||
@Column({ default: 0 }) | ||
gamesWon: number; // Number of games the user has won | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; // Timestamp for when the account was created | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; // Timestamp for the last account update | ||
} |
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