1- import { Injectable , NotFoundException , ConflictException } from '@nestjs/common' ;
1+ import { Injectable } from '@nestjs/common' ;
22import { InjectRepository } from '@nestjs/typeorm' ;
33import { Repository } from 'typeorm' ;
44import { User } from './entities/user.entity' ;
55import { CreateUserDto } from './dto/create-user.dto' ;
66import { UpdateUserDto } from './dto/update-user.dto' ;
77import * as bcrypt from 'bcryptjs' ;
8+ import { ensureUserExists , ensureUserDoesNotExist } from '../common/utils/user.utils' ;
89import { paginate , PaginatedResponse } from '../common/utils/pagination.util' ;
10+ import { PaginationQueryDto } from '../common/dto/pagination.dto' ;
911import { GetUsersDto } from './dto/get-users.dto' ;
1012import { CachingService } from '../caching/caching.service' ;
1113import { CACHE_TTL , CACHE_PREFIXES , CACHE_EVENTS } from '../caching/caching.constants' ;
@@ -25,10 +27,7 @@ export class UsersService {
2527 const existingUser = await this . userRepository . findOne ( {
2628 where : { email : createUserDto . email } ,
2729 } ) ;
28-
29- if ( existingUser ) {
30- throw new ConflictException ( 'User with this email already exists' ) ;
31- }
30+ ensureUserDoesNotExist ( existingUser , 'User with this email already exists' ) ;
3231
3332 // Hash password
3433 const hashedPassword = await bcrypt . hash ( createUserDto . password , 10 ) ;
@@ -65,7 +64,7 @@ export class UsersService {
6564 ) ;
6665 }
6766
68- return await paginate ( query , filter ) ;
67+ return await paginate ( query , filter || new PaginationQueryDto ( ) ) ;
6968 } ,
7069 CACHE_TTL . USER_PROFILE ,
7170 ) ;
@@ -76,17 +75,22 @@ export class UsersService {
7675 return await this . userRepository . findByIds ( ids ) ;
7776 }
7877
78+ /**
79+ * Helper method to find a user by ID or throw NotFoundException.
80+ * Can be used internally to eliminate duplication.
81+ */
82+ async findUserOrThrow ( id : string ) : Promise < User > {
83+ const user = await this . userRepository . findOne ( { where : { id } } ) ;
84+ return ensureUserExists ( user , 'User not found' ) ;
85+ }
86+
7987 async findOne ( id : string ) : Promise < User > {
8088 const cacheKey = `${ CACHE_PREFIXES . USER_PROFILE } :${ id } ` ;
8189
8290 return this . cachingService . getOrSet (
8391 cacheKey ,
8492 async ( ) => {
85- const user = await this . userRepository . findOne ( { where : { id } } ) ;
86- if ( ! user ) {
87- throw new NotFoundException ( 'User not found' ) ;
88- }
89- return user ;
93+ return await this . findUserOrThrow ( id ) ;
9094 } ,
9195 CACHE_TTL . USER_PROFILE ,
9296 ) ;
@@ -109,10 +113,7 @@ export class UsersService {
109113 }
110114
111115 async update ( id : string , updateUserDto : UpdateUserDto ) : Promise < User > {
112- const user = await this . userRepository . findOne ( { where : { id } } ) ;
113- if ( ! user ) {
114- throw new NotFoundException ( 'User not found' ) ;
115- }
116+ const user = await this . findUserOrThrow ( id ) ;
116117
117118 // If updating password, hash it
118119 if ( updateUserDto . password ) {
@@ -129,7 +130,7 @@ export class UsersService {
129130 }
130131
131132 async updateRefreshToken ( userId : string , refreshToken : string | null ) : Promise < void > {
132- await this . userRepository . update ( userId , { refreshToken } ) ;
133+ await this . userRepository . update ( userId , { refreshToken : refreshToken as unknown as string } ) ;
133134 // Invalidate user cache
134135 this . eventEmitter . emit ( CACHE_EVENTS . USER_UPDATED , { userId } ) ;
135136 }
@@ -140,8 +141,8 @@ export class UsersService {
140141 expires : Date | null ,
141142 ) : Promise < void > {
142143 await this . userRepository . update ( userId , {
143- passwordResetToken : token ,
144- passwordResetExpires : expires ,
144+ passwordResetToken : token as unknown as string ,
145+ passwordResetExpires : expires as unknown as Date ,
145146 } ) ;
146147 }
147148
@@ -151,8 +152,8 @@ export class UsersService {
151152 expires : Date | null ,
152153 ) : Promise < void > {
153154 await this . userRepository . update ( userId , {
154- emailVerificationToken : token ,
155- emailVerificationExpires : expires ,
155+ emailVerificationToken : token as unknown as string ,
156+ emailVerificationExpires : expires as unknown as Date ,
156157 } ) ;
157158 }
158159
@@ -161,10 +162,7 @@ export class UsersService {
161162 }
162163
163164 async remove ( id : string ) : Promise < void > {
164- const user = await this . userRepository . findOne ( { where : { id } } ) ;
165- if ( ! user ) {
166- throw new NotFoundException ( 'User not found' ) ;
167- }
165+ const user = await this . findUserOrThrow ( id ) ;
168166 await this . userRepository . remove ( user ) ;
169167
170168 // Invalidate cache after delete
0 commit comments