1- import { BadRequestException , Injectable , NotFoundException , UnauthorizedException } from "@nestjs/common" ;
1+ import {
2+ BadRequestException ,
3+ ConflictException ,
4+ Injectable ,
5+ NotFoundException ,
6+ UnauthorizedException ,
7+ } from "@nestjs/common" ;
28import ImageTypes from "../webtoon/webtoon/models/enums/image-types" ;
39import { LoginPayload } from "./models/payloads/login.payload" ;
410import { UserEntity } from "./models/entities/user.entity" ;
@@ -7,6 +13,8 @@ import {MiscService} from "../misc/misc.service";
713import { EpisodeProgressions , Images , Users } from "@prisma/client" ;
814import { JwtService } from "@nestjs/jwt" ;
915import { EpisodeProgressionPayload } from "./models/payloads/episode-progression.payload" ;
16+ import { CreateUserDto } from "./models/dto/create-user.dto" ;
17+ import { ChangePasswordDto } from "./models/dto/change-password.dto" ;
1018
1119@Injectable ( )
1220export class UsersService {
@@ -28,6 +36,70 @@ export class UsersService{
2836 } ) ;
2937 }
3038
39+ async createUser ( userDto : CreateUserDto ) : Promise < UserEntity > {
40+ // Check if the user already exists
41+ const existingUser = await this . prismaService . users . findFirst ( {
42+ where : {
43+ OR : [
44+ { username : userDto . username } ,
45+ { email : userDto . email } ,
46+ ] ,
47+ } ,
48+ } ) ;
49+ if ( existingUser )
50+ throw new ConflictException ( "User with this username or email already exists" ) ;
51+ // Create the user
52+ const hashedPassword = this . miscService . hashPassword ( userDto . password ) ;
53+ const user = await this . prismaService . users . create ( {
54+ data : {
55+ id : Bun . randomUUIDv7 ( ) ,
56+ username : userDto . username ,
57+ email : userDto . email ,
58+ password : hashedPassword ,
59+ admin : false ,
60+ jwt_id : this . miscService . generateRandomBytes ( 16 ) ,
61+ } ,
62+ include : {
63+ avatar : true ,
64+ } ,
65+ } ) ;
66+ return this . toUserEntity ( user , user . avatar ?. sum ) ;
67+ }
68+
69+ async changePassword ( user : UserEntity , changePasswordDto : ChangePasswordDto ) : Promise < void > {
70+ // Check if the old password is valid
71+ if ( changePasswordDto . oldPassword && ! this . miscService . comparePassword ( changePasswordDto . oldPassword , user . password ) )
72+ throw new UnauthorizedException ( "Invalid old password" ) ;
73+ // Check if the new password is valid
74+ if ( changePasswordDto . newPassword . length < 8 )
75+ throw new BadRequestException ( "New password must be at least 8 characters long" ) ;
76+ // Hash the new password
77+ const hashedPassword = this . miscService . hashPassword ( changePasswordDto . newPassword ) ;
78+ // Update the user's password
79+ await this . prismaService . users . update ( {
80+ where : {
81+ id : user . id ,
82+ } ,
83+ data : {
84+ password : hashedPassword ,
85+ } ,
86+ } ) ;
87+ }
88+
89+ async changeUserPassword ( userId : string , newPassword : string ) : Promise < void > {
90+ // Hash the new password
91+ const hashedPassword = this . miscService . hashPassword ( newPassword ) ;
92+ // Update the user's password
93+ await this . prismaService . users . update ( {
94+ where : {
95+ id : userId ,
96+ } ,
97+ data : {
98+ password : hashedPassword ,
99+ } ,
100+ } ) ;
101+ }
102+
31103 async getAvailableAvatars ( ) : Promise < string [ ] > {
32104 const webtoons : any [ ] = await this . prismaService . webtoons . findMany ( {
33105 include : {
@@ -80,6 +152,33 @@ export class UsersService{
80152 return this . toUserEntity ( user , user . avatar ?. sum ) ;
81153 }
82154
155+ async getUsers ( ) : Promise < UserEntity [ ] > {
156+ const users = await this . prismaService . users . findMany ( {
157+ include : {
158+ avatar : true ,
159+ } ,
160+ } ) ;
161+ if ( ! users . length )
162+ return [ ] ;
163+ return users . map ( ( user ) : UserEntity => this . toUserEntity ( user , user . avatar ?. sum ) ) ;
164+ }
165+
166+ async deleteUserById ( userId : string ) : Promise < void > {
167+ const user = await this . prismaService . users . findUnique ( {
168+ where : {
169+ id : userId ,
170+ } ,
171+ } ) ;
172+ if ( ! user )
173+ throw new NotFoundException ( "User not found" ) ;
174+ // Delete the user
175+ await this . prismaService . users . delete ( {
176+ where : {
177+ id : userId ,
178+ } ,
179+ } ) ;
180+ }
181+
83182 async login ( usernameOrEmail : string , password : string ) : Promise < LoginPayload > {
84183 const user = await this . prismaService . users . findFirst ( {
85184 where : {
0 commit comments