All files / app/service search.ts

9.87% Statements 8/81
0% Branches 0/48
0% Functions 0/4
9.87% Lines 8/81

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219          80x 80x         80x             80x 80x                                                                                                                                                                                                                                                                                     79x           79x           79x                                                                                                
import {
  Coordinates,
  DEFAULT_AGE_MAX,
  DEFAULT_AGE_MIN,
  SleepingArrangementOptions,
} from "features/search/utils/constants";
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import {
  BoolValue,
  StringValue,
  UInt32Value,
} from "google-protobuf/google/protobuf/wrappers_pb";
import { HostingStatus, MeetupStatus } 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 {
  acceptsKids?: boolean;
  acceptsPets?: boolean;
  acceptsLastMinRequests?: boolean;
  ageMin?: number;
  ageMax?: number;
  drinkingAllowed?: boolean | undefined;
  query?: string;
  bbox?: Coordinates;
  lastActive?: number; //within x days
  hasReferences?: boolean;
  hasStrongVerification?: boolean;
  hostingStatusOptions?: HostingStatus[];
  meetupStatus?: MeetupStatus[];
  numGuests?: number;
  completeProfile?: boolean;
  pageNumber?: number;
  pageSize?: number;
  selectedUserId?: number;
  sleepingArrangement?: SleepingArrangementOptions[];
  smokesAtHome?: boolean | undefined;
}
 
function constructUserSearchReq(
  {
    acceptsKids,
    acceptsLastMinRequests,
    acceptsPets,
    ageMin,
    ageMax,
    drinkingAllowed,
    query,
    bbox,
    lastActive,
    hasReferences,
    hasStrongVerification,
    hostingStatusOptions,
    meetupStatus,
    numGuests,
    completeProfile,
    selectedUserId,
    sleepingArrangement,
    smokesAtHome,
  }: UserSearchFilters,
  pageToken = "",
) {
  const req = new UserSearchReq();
 
  Iif (pageToken) {
    req.setPageToken(pageToken);
  }
 
  Iif (acceptsKids) {
    req.setAcceptsKids(new BoolValue().setValue(acceptsKids));
  }
 
  Iif (acceptsLastMinRequests) {
    req.setLastMinute(new BoolValue().setValue(acceptsLastMinRequests));
  }
 
  Iif (acceptsPets) {
    req.setAcceptsPets(new BoolValue().setValue(acceptsPets));
  }
 
  Iif (drinkingAllowed !== undefined) {
    req.setDrinkingAllowed(new BoolValue().setValue(drinkingAllowed));
  }
 
  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 (hasReferences) {
    req.setOnlyWithReferences(hasReferences);
  }
 
  Iif (hasStrongVerification) {
    req.setOnlyWithStrongVerification(hasStrongVerification);
  }
 
  Iif (hostingStatusOptions && hostingStatusOptions.length > 0) {
    req.setHostingStatusFilterList(hostingStatusOptions);
  }
 
  Iif (meetupStatus && meetupStatus.length > 0) {
    req.setMeetupStatusFilterList(meetupStatus);
  }
 
  Iif (ageMin && ageMin !== DEFAULT_AGE_MIN) {
    req.setAgeMin(new UInt32Value().setValue(ageMin));
  }
 
  Iif (ageMax && ageMax !== DEFAULT_AGE_MAX) {
    req.setAgeMax(new UInt32Value().setValue(ageMax));
  }
 
  Iif (numGuests) {
    req.setGuests(new UInt32Value().setValue(numGuests));
  }
 
  Iif (selectedUserId !== undefined) {
    req.addExactlyUserIds(selectedUserId);
  }
 
  Iif (sleepingArrangement && sleepingArrangement.length > 0) {
    req.setSleepingArrangementFilterList(sleepingArrangement);
  }
 
  Iif (smokesAtHome !== undefined) {
    req.setSmokesAtHome(new BoolValue().setValue(smokesAtHome));
  }
 
  return req;
}
 
export async function userSearch(filters: UserSearchFilters, pageToken = "") {
  const req = constructUserSearchReq(filters, pageToken);
  const response = await client.search.userSearch(req);
  return response.toObject();
}
 
export async function userSearchV2(filters: UserSearchFilters, pageToken = "") {
  const req = constructUserSearchReq(filters, pageToken);
  const response = await client.search.userSearchV2(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();
}