diff --git a/src/services/megabox/location.ts b/src/services/megabox/location.ts index 1a5d96ae..8e4fd104 100644 --- a/src/services/megabox/location.ts +++ b/src/services/megabox/location.ts @@ -107,12 +107,17 @@ function isValidCoordinate(value: number | undefined): value is number { function resolveAreaCodeFromText(text: string): string | null { const normalized = text.replace(/\s+/g, ''); + // 목록 순서가 아니라 텍스트에서 가장 앞에 나온 지역을 사용합니다. (예: "경기 광주시"는 광주광역시가 아닌 경기도) + let matchedAreaCode: string | null = null; + let matchedIndex = Number.POSITIVE_INFINITY; for (const [region, areaCode] of MEGABOX_AREA_CODE_BY_REGION) { - if (normalized.includes(region.replace(/\s+/g, ''))) { - return areaCode; + const index = normalized.indexOf(region); + if (index !== -1 && index < matchedIndex) { + matchedAreaCode = areaCode; + matchedIndex = index; } } - return null; + return matchedAreaCode; } export async function resolveMegaboxLocation( diff --git a/tests/services/megabox/location.test.ts b/tests/services/megabox/location.test.ts index 3fd27afe..83b7ef01 100644 --- a/tests/services/megabox/location.test.ts +++ b/tests/services/megabox/location.test.ts @@ -211,4 +211,12 @@ describe('무료 위치 해석 계약', () => { mockFetch.mockResolvedValueOnce(new Response(JSON.stringify({ documents: [{ place_name: '부산역', address_name: '부산 동구', x: '129.041', y: '35.115' }] }))); expect(await resolveMegaboxLocation({ keyword: '부산역' }, { kakaoRestApiKey: 'free' })).toMatchObject({ areaCode: '26', latitude: 35.115 }); }); + it('경기 광주시 주소는 광주광역시가 아닌 경기 지역으로 결정한다', async () => { + mockFetch.mockResolvedValueOnce(new Response(JSON.stringify({ documents: [{ place_name: '경안동', address_name: '경기 광주시 경안동', x: '127.255', y: '37.41' }] }))); + expect(await resolveMegaboxLocation({ keyword: '경안동' }, { kakaoRestApiKey: 'free' })).toMatchObject({ areaCode: '41' }); + }); + it('광주광역시 주소는 광주 지역으로 결정한다', async () => { + mockFetch.mockResolvedValueOnce(new Response(JSON.stringify({ documents: [{ place_name: '광주송정역', address_name: '광주 광산구 송정동', x: '126.793', y: '35.137' }] }))); + expect(await resolveMegaboxLocation({ keyword: '광주송정역' }, { kakaoRestApiKey: 'free' })).toMatchObject({ areaCode: '29' }); + }); });