All files / src/helpers/httpClient places.ts

10.81% Statements 4/37
12.5% Branches 3/24
33.33% Functions 1/3
10.81% Lines 4/37

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 117 118 119 120 121 122 123 124 125 126                                                                                                                                                2x 2x                                     2x 2x                                                                
/**
 * @fileoverview Places helper functions.
 * @module Places
 * @description Helper functions to get and handle places.
 */
 
import PlacesNeedingTranslationJSON, { NoloPlacesJSON } from '@global/types/httpClient/queries/places'
import { get } from './common'
 
/**
 * @function searchPlaces Search the places from the server.
 * @returns Promise of an array of places
 */
export default async function searchPlaces({
  latitude,
  longitude,
  q,
  radius,
  token,
}: {
  latitude?: number
  longitude?: number
  q?: string
  radius?: number
  token: string
}): Promise<NoloPlacesJSON> {
  try {
    const queryParams: { [key: string]: string } = {}
 
    if (latitude !== undefined) {
      queryParams.latitude = latitude.toString()
    }
    if (longitude !== undefined) {
      queryParams.longitude = longitude.toString()
    }
    if (radius !== undefined) {
      queryParams.radius = radius.toString()
    }
    if (q !== undefined) {
      queryParams.q = q
    }
 
    const queryString = new URLSearchParams(queryParams).toString()
    const endpoint = `search/sites${queryString ? `?${queryString}` : ''}`
 
    const response = await get({
      endpoint: `/${endpoint}`,
      authorizationToken: token,
    })
 
    const responseData = await response.json()
 
    if (!response.ok) {
      throw new Error(responseData.message)
    }
 
    return {
      json: responseData,
      status: response.status,
      message: responseData.message,
    }
  } catch (error) {
    console.log("Error, couldn't get places:", error)
    throw new Error(error instanceof Error ? error.message : String(error))
  }
}
 
/**
 * @function getPlaces Get the places from the server.
 * @returns Promise of an array of places
 */
export async function getPlaces({ token, getLatest }: { token: string; getLatest: boolean }): Promise<NoloPlacesJSON> {
  try {
    const response = await get({
      endpoint: `/sites${getLatest ? '?_sort=createdAt&_order=desc&_end=10' : '?_end=100'}`,
      authorizationToken: token,
    })
 
    const responseData = await response.json()
 
    console.log('getPlaces response:', responseData)
 
    if (!response.ok) {
      throw new Error(responseData.message)
    }
 
    return {
      json: responseData,
      status: response.status,
      message: responseData.message,
    }
  } catch (error) {
    console.log("Error, couldn't get places:", error)
    throw new Error(error instanceof Error ? error.message : String(error))
  }
}
 
/**
 * @function getPlacesNeedingDescription Get the places that need a description.
 * @param props The user's token
 * @param props.token The user's token
 * @returns Promise of an array of places
 */
export async function getPlacesNeedingDescription({ token }: { token: string }): Promise<PlacesNeedingTranslationJSON> {
  try {
    const response = await get({ endpoint: '/items/video-pending', authorizationToken: token })
 
    if (!response.ok) {
      throw new Error(response.statusText)
    }
 
    const responseData = await response.json()
 
    console.log('getPlacesNeedingDescription response:', responseData)
 
    return {
      json: responseData,
      status: response.status,
      message: response.statusText,
    }
  } catch (err) {
    console.error(err)
    throw new Error(err instanceof Error ? err.message : String(err))
  }
}