Repository navigation
Expand file tree
/
Copy pathmegaboxHandlers.ts
More file actions
219 lines (203 loc) · 7.4 KB
/
Copy pathmegaboxHandlers.ts
File metadata and controls
219 lines (203 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { isFreshOperationalRequest } from './operationalFreshness.js';
import { hasInvalidCinemaQuery, CINEMA_INVALID_INPUT } from '../services/cgv/validation.js';
import { geocodeBindings } from '../utils/geocode.js';
/**
* 메가박스 GET API 핸들러
*/
import { fetchMegaboxBookingList, toYyyymmdd } from '../services/megabox/client.js';
import {
fetchMegaboxNearbyTheaters,
resolveMegaboxNearestTheater,
} from '../services/megabox/location.js';
import { type ApiContext, errorResponse, successResponse } from './response.js';
function parseOptionalNumber(value: string | undefined): number | undefined {
return value === undefined ? undefined : Number(value);
}
/**
* 메가박스 주변 지점 조회 API 핸들러
* GET /api/megabox/theaters?lat={위도}&lng={경도}&playDate={YYYYMMDD}&areaCode={지역코드}
*/
export async function handleMegaboxFindNearbyTheaters(c: ApiContext) {
if (hasInvalidCinemaQuery((key) => c.req.query(key)))
return errorResponse(c, 'INVALID_INPUT', CINEMA_INVALID_INPUT, 400);
const keyword = c.req.query('keyword') || undefined;
const lat = parseOptionalNumber(c.req.query('lat'));
const lng = parseOptionalNumber(c.req.query('lng'));
const playDate = c.req.query('playDate') || toYyyymmdd();
const areaCode = c.req.query('areaCode') || undefined;
const limit = parseInt(c.req.query('limit') || '10');
const timeoutMs = parseInt(c.req.query('timeoutMs') || '15000');
try {
const result = await fetchMegaboxNearbyTheaters(
{
keyword,
latitude: lat,
longitude: lng,
playDate,
areaCode,
limit,
timeout: timeoutMs,
},
{
...geocodeBindings(c.env),
bypassCache: await isFreshOperationalRequest(c),
timeout: timeoutMs,
},
);
return successResponse(c, result, { total: result.count, pageSize: limit });
} catch (error) {
const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
return errorResponse(c, 'MEGABOX_THEATER_SEARCH_FAILED', message, 500);
}
}
/**
* 메가박스 영화/회차 목록 조회 API 핸들러
* GET /api/megabox/movies?playDate={YYYYMMDD}&theaterId={지점ID}&movieId={영화ID}
*/
export async function handleMegaboxListNowShowing(c: ApiContext) {
if (hasInvalidCinemaQuery((key) => c.req.query(key)))
return errorResponse(c, 'INVALID_INPUT', CINEMA_INVALID_INPUT, 400);
const playDate = c.req.query('playDate') || toYyyymmdd();
const keyword = c.req.query('keyword') || undefined;
const lat = parseOptionalNumber(c.req.query('lat'));
const lng = parseOptionalNumber(c.req.query('lng'));
let theaterId = c.req.query('theaterId') || undefined;
const movieId = c.req.query('movieId') || undefined;
const areaCode = c.req.query('areaCode') || undefined;
const timeoutMs = parseInt(c.req.query('timeoutMs') || '15000');
try {
let resolvedTheater = null;
let resolvedLocation = null;
if (!theaterId && (keyword || typeof lat === 'number' || typeof lng === 'number')) {
const resolved = await resolveMegaboxNearestTheater(
{
keyword,
latitude: lat,
longitude: lng,
areaCode,
playDate,
timeout: timeoutMs,
},
{
...geocodeBindings(c.env),
bypassCache: await isFreshOperationalRequest(c),
timeout: timeoutMs,
},
);
if (!resolved.theater) throw new Error('요청 위치의 메가박스 극장을 찾을 수 없습니다.');
theaterId = resolved.theater.theaterId;
resolvedTheater = resolved.theater;
resolvedLocation = resolved.location;
}
const resolvedAreaCode = resolvedLocation?.areaCode || areaCode || '11';
const result = await fetchMegaboxBookingList({
playDate,
theaterId,
movieId,
areaCode: resolvedAreaCode,
timeout: timeoutMs,
});
return successResponse(
c,
{
playDate,
filters: {
theaterId: theaterId || null,
movieId: movieId || null,
keyword: keyword || null,
latitude: lat ?? null,
longitude: lng ?? null,
areaCode: resolvedAreaCode,
},
resolvedTheater,
theaters: result.theaters,
movies: result.movies,
showtimes: result.showtimes,
},
{ total: result.showtimes.length },
);
} catch (error) {
const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
return errorResponse(c, 'MEGABOX_MOVIE_LIST_FAILED', message, 500);
}
}
/**
* 메가박스 잔여 좌석 조회 API 핸들러
* GET /api/megabox/seats?playDate={YYYYMMDD}&theaterId={지점ID}&movieId={영화ID}
*/
export async function handleMegaboxGetRemainingSeats(c: ApiContext) {
if (hasInvalidCinemaQuery((key) => c.req.query(key)))
return errorResponse(c, 'INVALID_INPUT', CINEMA_INVALID_INPUT, 400);
const playDate = c.req.query('playDate') || toYyyymmdd();
const keyword = c.req.query('keyword') || undefined;
const lat = parseOptionalNumber(c.req.query('lat'));
const lng = parseOptionalNumber(c.req.query('lng'));
let theaterId = c.req.query('theaterId') || undefined;
const movieId = c.req.query('movieId') || undefined;
const areaCode = c.req.query('areaCode') || undefined;
const limit = parseInt(c.req.query('limit') || '50');
const timeoutMs = parseInt(c.req.query('timeoutMs') || '15000');
try {
let resolvedTheater = null;
let resolvedLocation = null;
if (!theaterId && (keyword || typeof lat === 'number' || typeof lng === 'number')) {
const resolved = await resolveMegaboxNearestTheater(
{
keyword,
latitude: lat,
longitude: lng,
areaCode,
playDate,
timeout: timeoutMs,
},
{
...geocodeBindings(c.env),
bypassCache: await isFreshOperationalRequest(c),
timeout: timeoutMs,
},
);
if (!resolved.theater) throw new Error('요청 위치의 메가박스 극장을 찾을 수 없습니다.');
theaterId = resolved.theater.theaterId;
resolvedTheater = resolved.theater;
resolvedLocation = resolved.location;
}
const resolvedAreaCode = resolvedLocation?.areaCode || areaCode || '11';
const { showtimes } = await fetchMegaboxBookingList({
playDate,
theaterId,
movieId,
areaCode: resolvedAreaCode,
timeout: timeoutMs,
});
const seats = showtimes
.filter((item) => (theaterId ? item.theaterId === theaterId : true))
.filter((item) => (movieId ? item.movieId === movieId : true))
.sort((a, b) => {
if (a.startTime === b.startTime) {
return a.theaterName.localeCompare(b.theaterName);
}
return a.startTime.localeCompare(b.startTime);
})
.slice(0, limit);
return successResponse(
c,
{
playDate,
filters: {
theaterId: theaterId || null,
movieId: movieId || null,
keyword: keyword || null,
latitude: lat ?? null,
longitude: lng ?? null,
areaCode: resolvedAreaCode,
},
resolvedTheater,
seats,
},
{ total: seats.length, pageSize: limit },
);
} catch (error) {
const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
return errorResponse(c, 'MEGABOX_SEAT_LIST_FAILED', message, 500);
}
}