99} from '@nestjs/common' ;
1010import * as bcrypt from 'bcryptjs' ;
1111import Redis from 'ioredis' ;
12+ import { DataSource } from 'typeorm' ;
1213import { v4 as uuidv4 } from 'uuid' ;
1314
1415import { USER_STATUS } from '../../../auth/const/userStatus.const' ;
@@ -30,6 +31,7 @@ export class UserService {
3031 @Inject ( ) private readonly userRepository : UserRepository ,
3132 @Inject ( ) private readonly redisService : RedisService ,
3233 @Inject ( ) private readonly authService : AuthService ,
34+ @Inject ( ) private readonly dataSource : DataSource ,
3335 ) {
3436 this . redis = this . redisService . getOrThrow ( ) ;
3537 }
@@ -148,4 +150,33 @@ export class UserService {
148150 const session = JSON . parse ( await this . redis . get ( `user:${ sid } ` ) ) ;
149151 return session . targetEvent ;
150152 }
153+
154+ async makeGuestUser ( ) {
155+ try {
156+ // make guest user
157+ const uuid = uuidv4 ( ) ;
158+ const guestId = `guest-${ uuid } ` ;
159+
160+ const guestInfo = await this . userRepository . createUser ( {
161+ loginId : guestId ,
162+ role : USER_ROLE . USER ,
163+ checkGuest : true ,
164+ } ) ;
165+
166+ const guestSession = {
167+ id : guestInfo . id ,
168+ loginId : guestInfo . loginId ,
169+ userStatus : USER_STATUS . LOGIN ,
170+ targetEvent : null ,
171+ } ;
172+
173+ this . redis . set ( `user-id:${ guestId } ` , uuid , 'EX' , 3600 ) ;
174+ this . redis . set ( `user:${ uuid } ` , JSON . stringify ( guestSession ) , 'EX' , 3600 ) ;
175+
176+ return { sessionId : uuid , userInfo : guestSession } ;
177+ } catch ( err ) {
178+ this . logger . error ( err . name , err . stack ) ;
179+ throw new InternalServerErrorException ( '게스트 사용자 생성에 실패하였습니다.' ) ;
180+ }
181+ }
151182}
0 commit comments