All files / app/service search.ts

12.24% Statements 6/49
0% Branches 0/25
0% Functions 0/2
12.24% Lines 6/49

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  78x         78x             78x 78x                       77x                                                                                                     77x                                                                                                
import { Coordinates } from "features/search/constants";
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import {
  BoolValue,
  StringValue,
  UInt32Value,
} from "google-protobuf/google/protobuf/wrappers_pb";
import { HostingStatus } from "proto/api_pb";
import {
  EventSearchReq,
  EventSearchRes,
  RectArea,
  UserSearchReq,
} from "proto/search_pb";
import client from "service/client";
import { GeocodeResult } from "utils/hooks";
 
export interface UserSearchFilters {
  query?: string;
  bbox?: Coordinates;
  lastActive?: number; //within x days
  hostingStatusOptions?: HostingStatus[];
  numGuests?: number;
  completeProfile?: boolean;
}
 
export async function userSearch(
  {
    query,
    bbox,
    lastActive,
    hostingStatusOptions,
    numGuests,
    completeProfile,
  }: UserSearchFilters,
  pageToken = ""
) {
  const req = new UserSearchReq();
  req.setPageToken(pageToken);
 
  Iif (query) {
    req.setQuery(new StringValue().setValue(query));
  }
 
  Iif (bbox !== undefined && bbox.join() !== "0,0,0,0") {
    const rectAreaSearch = new RectArea();
 
    rectAreaSearch.setLngMin(bbox[0]);
    rectAreaSearch.setLatMin(bbox[1]);
    rectAreaSearch.setLngMax(bbox[2]);
    rectAreaSearch.setLatMax(bbox[3]);
 
    req.setSearchInRectangle(rectAreaSearch);
  }
 
  Iif (lastActive) {
    const timestamp = new Timestamp();
    timestamp.fromDate(new Date(Date.now() - 1000 * 60 * 60 * 24 * lastActive));
    req.setLastActive(timestamp);
  }
 
  Iif (completeProfile) {
    req.setProfileCompleted(new BoolValue().setValue(completeProfile));
  }
 
  Iif (hostingStatusOptions && hostingStatusOptions.length !== 0) {
    req.setHostingStatusFilterList(hostingStatusOptions);
  }
 
  Iif (numGuests) {
    req.setGuests(new UInt32Value().setValue(numGuests));
  }
 
  const response = await client.search.userSearch(req);
  return response.toObject();
}
 
export async function EventSearch({
  pageNumber,
  pageSize,
  pastEvents,
  isMyCommunities,
  isOnlineOnly,
  searchLocation,
}: {
  pageNumber: number;
  pageSize: number;
  pastEvents?: boolean;
  isMyCommunities?: boolean;
  isOnlineOnly?: boolean;
  searchLocation?: GeocodeResult | "";
}): Promise<EventSearchRes.AsObject> {
  const req = new EventSearchReq();
  req.setPageSize(pageSize);
  req.setPageNumber(pageNumber);
 
  Iif (pastEvents !== undefined) {
    req.setPast(pastEvents);
  }
  Iif (typeof searchLocation !== "string") {
    // If it's a region (i.e. "France" or "United States") use query search by name
    // This will search for events in the region by that name
    if (searchLocation?.isRegion) {
      req.setQuery(new StringValue().setValue(searchLocation.name));
    } else {
      // Otherwise use rectangle search so we get the area around a city
      // This is because if you search a small town, you might want to search around it too
      const location = new RectArea();
      location.setLatMin(searchLocation?.bbox[1] || 0);
      location.setLatMax(searchLocation?.bbox[3] || 0);
      location.setLngMin(searchLocation?.bbox[0] || 0);
      location.setLngMax(searchLocation?.bbox[2] || 0);
      req.setSearchInRectangle(location);
    }
  }
  Iif (isMyCommunities !== undefined) {
    req.setMyCommunities(isMyCommunities);
  }
  Iif (isOnlineOnly !== undefined) {
    req.setOnlyOnline(isOnlineOnly);
  }
 
  const res = await client.search.eventSearch(req);
  return res.toObject();
}