@@ -10,12 +10,14 @@ import {
1010import { Cron } from '@nestjs/schedule' ;
1111import * as bcrypt from 'bcryptjs' ;
1212import Redis from 'ioredis' ;
13- import { DataSource } from 'typeorm' ;
13+ import { DataSource , In } from 'typeorm' ;
1414import { v4 as uuidv4 } from 'uuid' ;
1515
1616import { USER_STATUS } from '../../../auth/const/userStatus.const' ;
1717import { AuthService } from '../../../auth/service/auth.service' ;
1818import { UserParamDto } from '../../../util/user-injection/userParamDto' ;
19+ import { Reservation } from '../../reservation/entity/reservation.entity' ;
20+ import { ReservedSeat } from '../../reservation/entity/reservedSeat.entity' ;
1921import { USER_ROLE } from '../const/userRole' ;
2022import { UserCreateDto } from '../dto/userCreate.dto' ;
2123import { UserInfoDto } from '../dto/userInfo.dto' ;
@@ -183,14 +185,24 @@ export class UserService {
183185
184186 @Cron ( '0 0 0 * * *' , { name : 'removeGuestReservation' } )
185187 async removeAllGuest ( ) {
188+ const queryRunner = this . dataSource . createQueryRunner ( ) ;
189+ await queryRunner . connect ( ) ;
190+ await queryRunner . startTransaction ( ) ;
186191 try {
187- const queryRunner = this . dataSource . createQueryRunner ( ) ;
188- await queryRunner . connect ( ) ;
189- await queryRunner . startTransaction ( ) ;
192+ const user = await queryRunner . manager . find ( User , { where : { checkGuest : true } } ) ;
193+ const userIds = user . map ( ( u ) => u . id ) ;
194+
195+ const reservationIds = await queryRunner . manager . find ( Reservation , { where : { user : In ( userIds ) } } ) ;
196+ await queryRunner . manager . delete ( ReservedSeat , { reservation : In ( reservationIds . map ( ( r ) => r . id ) ) } ) ;
197+ await queryRunner . manager . delete ( Reservation , { user : In ( userIds ) } ) ;
198+ await queryRunner . manager . delete ( User , { id : In ( userIds ) } ) ;
199+
200+ await queryRunner . commitTransaction ( ) ;
190201
191202 return this . userRepository . deleteAllGuest ( ) ;
192203 } catch ( err ) {
193204 this . logger . error ( err . name , err . stack ) ;
205+ await queryRunner . rollbackTransaction ( ) ;
194206 throw new InternalServerErrorException ( '게스트 사용자 삭제에 실패하였습니다.' ) ;
195207 }
196208 }
0 commit comments