All files / app/service search.ts

17.85% Statements 5/28
0% Branches 0/11
0% Functions 0/1
17.85% Lines 5/28

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  68x         68x   68x 68x                     68x                                                                                                    
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 { RectArea, UserSearchReq } from "proto/search_pb";
import client from "service/client";
 
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();
}