Skip to content

Commit bb63a10

Browse files
authored
Merge pull request #235 from boostcampwm-2024/feat/#234-collect-leaving-users-bookedseats
좌석 선택 중 이탈 시 좌석 회수
2 parents c0c9ce5 + 00c5887 commit bb63a10

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

back/src/domains/booking/service/booking.service.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { UserService } from '../../user/service/user.service';
99
import { BookingAdmissionStatusDto } from '../dto/bookingAdmissionStatus.dto';
1010
import { ServerTimeDto } from '../dto/serverTime.dto';
1111

12+
import { BookingSeatsService } from './booking-seats.service';
1213
import { EnterBookingService } from './enter-booking.service';
1314
import { InBookingService } from './in-booking.service';
1415
import { OpenBookingService } from './open-booking.service';
@@ -24,6 +25,7 @@ export class BookingService {
2425
private readonly redisService: RedisService,
2526
private readonly eventService: EventService,
2627
private readonly authService: AuthService,
28+
private readonly bookingSeatsService: BookingSeatsService,
2729
private readonly inBookingService: InBookingService,
2830
private readonly openBookingService: OpenBookingService,
2931
private readonly waitingQueueService: WaitingQueueService,
@@ -35,11 +37,25 @@ export class BookingService {
3537

3638
@OnEvent('seats-sse-close')
3739
async onSeatsSseDisconnected(event: { sid: string }) {
38-
const eventId = await this.userService.getUserEventTarget(event.sid);
39-
await this.inBookingService.emitSession(event.sid);
40+
const sid = event.sid;
41+
const eventId = await this.userService.getUserEventTarget(sid);
42+
await this.collectSeatsIfNotSaved(eventId, sid);
43+
await this.inBookingService.emitSession(sid);
4044
await this.letInNextWaiting(eventId);
4145
}
4246

47+
private async collectSeatsIfNotSaved(eventId: number, sid: string) {
48+
const inBookingSession = await this.inBookingService.getSession(eventId, sid);
49+
if (inBookingSession && !inBookingSession.saved) {
50+
const bookedSeats = inBookingSession.bookedSeats;
51+
bookedSeats.forEach((seat) => {
52+
this.bookingSeatsService.updateSeatDeleted(eventId, seat);
53+
});
54+
inBookingSession.bookedSeats = [];
55+
await this.inBookingService.setSession(eventId, inBookingSession);
56+
}
57+
}
58+
4359
@OnEvent('entering-sessions-gc')
4460
async onEnteringSessionsGc(event: { eventId: number }) {
4561
await this.letInNextWaiting(event.eventId);

back/src/domains/booking/service/in-booking.service.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type InBookingSession = {
1010
sid: string;
1111
bookingAmount: number;
1212
bookedSeats: [number, number][];
13+
saved: boolean;
1314
};
1415

1516
@Injectable()
@@ -61,6 +62,7 @@ export class InBookingService {
6162
sid,
6263
bookingAmount,
6364
bookedSeats: [],
65+
saved: false,
6466
};
6567
await this.setSession(eventId, session);
6668
return true;
@@ -102,6 +104,19 @@ export class InBookingService {
102104
await this.setSession(eventId, session);
103105
}
104106

107+
async getIsSaved(sid: string) {
108+
const eventId = await this.getTargetEventId(sid);
109+
const session = await this.getSession(eventId, sid);
110+
return session.saved;
111+
}
112+
113+
async setIsSaved(sid: string, saved: boolean) {
114+
const eventId = await this.getTargetEventId(sid);
115+
const session = await this.getSession(eventId, sid);
116+
session.saved = saved;
117+
await this.setSession(eventId, session);
118+
}
119+
105120
async emitSession(sid: string) {
106121
const eventId = await this.getTargetEventId(sid);
107122
await this.removeInBooking(eventId, sid);
@@ -129,15 +144,15 @@ export class InBookingService {
129144
return `in-booking:${eventId}:sessions`;
130145
}
131146

132-
private async setSession(eventId: number, inBookingSession: InBookingSession): Promise<void> {
147+
async setSession(eventId: number, inBookingSession: InBookingSession): Promise<void> {
133148
const sessionKey = this.getSessionKey(eventId, inBookingSession.sid);
134149
const eventKey = this.getEventKey(eventId);
135150

136151
await this.redis.sadd(eventKey, inBookingSession.sid);
137152
await this.redis.set(sessionKey, JSON.stringify(inBookingSession));
138153
}
139154

140-
private async getSession(eventId: number, sid: string): Promise<InBookingSession | null> {
155+
async getSession(eventId: number, sid: string): Promise<InBookingSession | null> {
141156
const session = await this.redis.get(this.getSessionKey(eventId, sid));
142157
return session ? JSON.parse(session) : null;
143158
}

back/src/domains/reservation/service/reservation.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ export class ReservationService {
134134

135135
await queryRunner.commitTransaction();
136136

137+
await this.inBookingService.setIsSaved(sid, true);
138+
137139
return {
138140
programName: program.name,
139141
runningDate: event[0].runningDate,

0 commit comments

Comments
 (0)