Repository navigation
Expand file tree
/
Copy pathcgvHandlers.ts
More file actions
246 lines (226 loc) · 7.44 KB
/
Copy pathcgvHandlers.ts
File metadata and controls
246 lines (226 loc) · 7.44 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { geocodeBindings } from '../utils/geocode.js';
/**
* CGV GET API 핸들러
*/
import {
fetchCgvMovies,
fetchCgvTheaters,
fetchCgvTimetable,
toYyyymmdd,
} from '../services/cgv/client.js';
import { isCgvUpstreamUnavailableError } from '../services/cgv/errors.js';
import { fetchCgvNearbyTheaters, resolveCgvNearestTheater } from '../services/cgv/location.js';
import { filterAndSortTimetable } from '../services/cgv/timetable.js';
import { type ApiContext, errorResponse, serviceErrorResponse, successResponse } from './response.js';
function parseOptionalNumber(value: string | undefined): number | undefined {
if (!value) {
return undefined;
}
const parsed = Number.parseFloat(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
/**
* CGV 극장 목록 조회 API 핸들러
* GET /api/cgv/theaters?playDate={YYYYMMDD}®ionCode={지역코드}
*/
export async function handleCgvFindTheaters(c: ApiContext) {
const playDate = c.req.query('playDate') || toYyyymmdd();
const regionCode = c.req.query('regionCode') || undefined;
const keyword = c.req.query('keyword') || undefined;
const latitude = parseOptionalNumber(c.req.query('lat'));
const longitude = parseOptionalNumber(c.req.query('lng'));
const limit = parseInt(c.req.query('limit') || '30');
const timeoutMs = parseInt(c.req.query('timeoutMs') || '15000');
try {
if (keyword || typeof latitude === 'number' || typeof longitude === 'number') {
const result = await fetchCgvNearbyTheaters(
{
playDate,
regionCode,
keyword,
latitude,
longitude,
limit,
timeout: timeoutMs,
},
{
timeout: timeoutMs,
zyteApiKey: c.env?.ZYTE_API_KEY,
...geocodeBindings(c.env),
},
);
return successResponse(c, result, { total: result.count, pageSize: limit });
}
const theaters = await fetchCgvTheaters({
playDate,
regionCode,
timeout: timeoutMs,
zyteApiKey: c.env?.ZYTE_API_KEY,
});
const sliced = theaters.slice(0, limit);
return successResponse(
c,
{
playDate,
filters: {
regionCode: regionCode || null,
keyword: keyword || null,
latitude: latitude ?? null,
longitude: longitude ?? null,
},
theaters: sliced,
},
{ total: sliced.length, pageSize: limit },
);
} catch (error) {
if (isCgvUpstreamUnavailableError(error)) {
return serviceErrorResponse(c, error, 'theaters');
}
const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
return errorResponse(c, 'CGV_THEATER_SEARCH_FAILED', message, 500);
}
}
/**
* CGV 영화 목록 조회 API 핸들러
* GET /api/cgv/movies?playDate={YYYYMMDD}&theaterCode={극장코드}
*/
export async function handleCgvSearchMovies(c: ApiContext) {
const playDate = c.req.query('playDate') || toYyyymmdd();
let theaterCode = c.req.query('theaterCode') || undefined;
const keyword = c.req.query('keyword') || undefined;
const latitude = parseOptionalNumber(c.req.query('lat'));
const longitude = parseOptionalNumber(c.req.query('lng'));
const timeoutMs = parseInt(c.req.query('timeoutMs') || '15000');
try {
let resolvedTheater = null;
if (
!theaterCode &&
(keyword || typeof latitude === 'number' || typeof longitude === 'number')
) {
const resolved = await resolveCgvNearestTheater(
{
playDate,
keyword,
latitude,
longitude,
timeout: timeoutMs,
},
{
timeout: timeoutMs,
zyteApiKey: c.env?.ZYTE_API_KEY,
...geocodeBindings(c.env),
},
);
resolvedTheater = resolved.theater;
theaterCode = resolved.theater?.theaterCode;
}
const shouldReturnEmpty =
!
8000
theaterCode && (keyword || typeof latitude === 'number' || typeof longitude === 'number');
const movies = shouldReturnEmpty
? []
: await fetchCgvMovies({
playDate,
theaterCode,
timeout: timeoutMs,
zyteApiKey: c.env?.ZYTE_API_KEY,
});
return successResponse(
c,
{
playDate,
filters: {
theaterCode: theaterCode || null,
keyword: keyword || null,
latitude: latitude ?? null,
longitude: longitude ?? null,
},
resolvedTheater,
movies,
},
{ total: movies.length },
);
} catch (error) {
if (isCgvUpstreamUnavailableError(error)) {
return serviceErrorResponse(c, error, 'movies');
}
const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
return errorResponse(c, 'CGV_MOVIE_SEARCH_FAILED', message, 500);
}
}
/**
* CGV 시간표 조회 API 핸들러
* GET /api/cgv/timetable?playDate={YYYYMMDD}&theaterCode={극장코드}&movieCode={영화코드}
*/
export async function handleCgvGetTimetable(c: ApiContext) {
const playDate = c.req.query('playDate') || toYyyymmdd();
let theaterCode = c.req.query('theaterCode') || undefined;
const movieCode = c.req.query('movieCode') || undefined;
const keyword = c.req.query('keyword') || undefined;
const latitude = parseOptionalNumber(c.req.query('lat'));
const longitude = parseOptionalNumber(c.req.query('lng'));
const limit = parseInt(c.req.query('limit') || '50');
const timeoutMs = parseInt(c.req.query('timeoutMs') || '15000');
try {
let resolvedTheater = null;
if (
!theaterCode &&
(keyword || typeof latitude === 'number' || typeof longitude === 'number')
) {
const resolved = await resolveCgvNearestTheater(
{
playDate,
keyword,
latitude,
longitude,
timeout: timeoutMs,
},
{
timeout: timeoutMs,
zyteApiKey: c.env?.ZYTE_API_KEY,
...geocodeBindings(c.env),
},
);
resolvedTheater = resolved.theater;
theaterCode = resolved.theater?.theaterCode;
}
const shouldReturnEmpty =
!theaterCode && (keyword || typeof latitude === 'number' || typeof longitude === 'number');
const timetable = shouldReturnEmpty
? []
: await fetchCgvTimetable({
playDate,
theaterCode,
movieCode,
timeout: timeoutMs,
zyteApiKey: c.env?.ZYTE_API_KEY,
});
const exactFiltered = filterAndSortTimetable(timetable, { theaterCode, movieCode, limit });
const filterRelaxed = Boolean(movieCode && exactFiltered.length === 0 && timetable.length > 0);
const filtered = filterRelaxed
? filterAndSortTimetable(timetable, { theaterCode, limit })
: exactFiltered;
return successResponse(
c,
{
playDate,
filters: {
theaterCode: theaterCode || null,
movieCode: movieCode || null,
keyword: keyword || null,
latitude: latitude ?? null,
longitude: longitude ?? null,
},
resolvedTheater,
filterRelaxed,
timetable: filtered,
},
{ total: filtered.length, pageSize: limit },
);
} catch (error) {
if (isCgvUpstreamUnavailableError(error)) {
return serviceErrorResponse(c, error, 'timetable');
}
const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
return errorResponse(c, 'CGV_TIMETABLE_FETCH_FAILED', message, 500);
}
}