All files / src/api outdoorPlacesOfInterestApi.ts

4.34% Statements 1/23
0% Branches 0/14
0% Functions 0/5
4.54% Lines 1/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                4x                                                                                                                      
import { API_BASE_URL } from "../const";
import { TransportModeApi } from "../type";
import { checkAndGetViableShuttleDestination } from "../utils/navigationUtils";
import {
  getOutdoorDirections,
  getOutdoorDirectionsWithShuttle,
  OutdoorDirectionResponse,
} from "./outdoorDirectionsApi";
 
export interface NearbyPlace {
  id: string;
  name: string;
  address: string;
  location?: {
    latitude: number;
    longitude: number;
  };
  rating?: number;
  openingHours?: string[];
  phoneNumber?: string;
}
 
export async function getNearbyPlaces(
  latitude: number,
  longitude: number,
  placeType: string,
): Promise<NearbyPlace[]> {
  try {
    const response = await fetch(
      `${API_BASE_URL}/api/places/outdoor` +
        `?latitude=${latitude}` +
        `&longitude=${longitude}` +
        `&placeType=${placeType}` +
        `&maxResultCount=20`,
      { method: "POST" },
    );
 
    if (!response.ok) {
      throw new Error(`Search API error: ${response.status}`);
    }
 
    const json = await response.json();
 
    return (json.places ?? []).map((p: any, index: number) => ({
      id: index.toString(),
      name: p.displayName?.text ?? "Unknown",
      address: p.formattedAddress ?? "",
      location: p.location,
      rating: p.rating,
      openingHours: p.currentOpeningHours ?? "N/A",
      phoneNumber: p.nationalPhoneNumber ?? "N/A",
    }));
  } catch {
    return [];
  }
}
export const getAllOutdoorDirectionsInfo = async (
  origin: {
    latitude: number;
    longitude: number;
    buildingId?: string;
  },
  destination: {
    latitude: number;
    longitude: number;
    buildingId?: string;
  },
) => {
  const modes: TransportModeApi[] = [
    "walking",
    "bicycling",
    "transit",
    "driving",
  ];
 
  const originStr = `${origin.latitude},${origin.longitude}`;
  const destStr = `${destination.latitude},${destination.longitude}`;
 
  try {
    const results = await Promise.all(
      modes.map((mode) =>
        getOutdoorDirections(originStr, destStr, mode, {
          originBuildingId: origin.buildingId,
          destinationBuildingId: destination.buildingId,
        }),
      ),
    );
    const dest_shuttle = checkAndGetViableShuttleDestination(
      destination,
      origin,
    );
    if (dest_shuttle) {
      results.push(
        await getOutdoorDirectionsWithShuttle(
          originStr,
          destStr,
          dest_shuttle,
          {
            originBuildingId: origin.buildingId,
            destinationBuildingId: destination.buildingId,
          },
        ),
      );
    }
 
    const validResults = results.filter(
      (res): res is OutdoorDirectionResponse => res !== null,
    );
 
    return validResults;
  } catch (error) {
    console.error("Error fetching all transport modes:", error);
    return [];
  }
};