/** * @ai-summary Business logic for stations feature */ import { StationsRepository } from '../data/stations.repository'; import { googleMapsClient } from '../external/google-maps/google-maps.client'; import { StationSearchRequest, StationSearchResponse } from './stations.types'; import { logger } from '../../../core/logging/logger'; export class StationsService { constructor(private repository: StationsRepository) {} async searchNearbyStations( request: StationSearchRequest, userId: string ): Promise { logger.info('Searching for stations', { userId, ...request }); // Search via Google Maps const stations = await googleMapsClient.searchNearbyStations( request.latitude, request.longitude, request.radius || 5000 ); // Cache stations for future reference for (const station of stations) { await this.repository.cacheStation(station); } // Sort by distance stations.sort((a, b) => (a.distance || 0) - (b.distance || 0)); return { stations, searchLocation: { latitude: request.latitude, longitude: request.longitude }, searchRadius: request.radius || 5000, timestamp: new Date().toISOString() }; } async saveStation( placeId: string, userId: string, data?: { nickname?: string; notes?: string; isFavorite?: boolean } ) { // Get station details from cache const station = await this.repository.getCachedStation(placeId); if (!station) { throw new Error('Station not found. Please search for stations first.'); } // Save to user's saved stations const saved = await this.repository.saveStation(userId, placeId, data); return { ...saved, station }; } async getUserSavedStations(userId: string) { const savedStations = await this.repository.getUserSavedStations(userId); // Enrich with cached station data const enriched = await Promise.all( savedStations.map(async (saved) => { const station = await this.repository.getCachedStation(saved.stationId); return { ...saved, station }; }) ); return enriched; } async removeSavedStation(placeId: string, userId: string) { const removed = await this.repository.deleteSavedStation(userId, placeId); if (!removed) { throw new Error('Saved station not found'); } } }