All files / app/service user.ts

77.77% Statements 63/81
50% Branches 3/6
33.33% Functions 3/9
77.77% Lines 63/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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 27669x 69x                       69x 69x   69x                                                                                 69x                           69x                 69x                           69x                       69x     2x   2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x     2x     2x       2x   2x           2x                                         2x     69x           69x 2x     2x         2x     2x     2x     2x 2x     2x     2x 2x     2x     2x     2x 2x     2x     2x     2x     2x 2x     2x 2x 2x     2x 2x     2x   2x                                                   2x           69x      
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import wrappers from "google-protobuf/google/protobuf/wrappers_pb";
import {
  GetUserReq,
  LanguageAbility,
  NullableBoolValue,
  NullableStringValue,
  NullableUInt32Value,
  PingReq,
  RepeatedLanguageAbilityValue,
  RepeatedStringValue,
  UpdateProfileReq,
  User,
} from "proto/api_pb";
import { AuthReq } from "proto/auth_pb";
 
import client from "./client";
import { ProtoToJsTypes } from "./utils/types";
 
type RequiredUpdateProfileReq = Required<UpdateProfileReq.AsObject>;
type ProfileFormData = {
  [K in keyof RequiredUpdateProfileReq]: ProtoToJsTypes<
    RequiredUpdateProfileReq[K]
  >;
};
 
export type UpdateUserProfileData = Pick<
  ProfileFormData,
  | "name"
  | "city"
  | "hometown"
  | "lat"
  | "lng"
  | "radius"
  | "pronouns"
  | "occupation"
  | "education"
  | "aboutMe"
  | "myTravels"
  | "thingsILike"
  | "hostingStatus"
  | "meetupStatus"
  | "languageAbilities"
  | "regionsVisited"
  | "regionsLived"
  | "additionalInformation"
  | "avatarKey"
>;
 
export type HostingPreferenceData = Omit<
  ProfileFormData,
  keyof UpdateUserProfileData | "gender"
>;
 
/**
 * Login user using password
 */
export async function passwordLogin(
  username: string,
  password: string,
  rememberDevice: boolean
) {
  const req = new AuthReq();
  req.setUser(username);
  req.setPassword(password);
  req.setRememberDevice(rememberDevice);
 
  const res = await client.auth.authenticate(req);
  return res.toObject();
}
 
export async function getAuthState() {
  return (await client.auth.getAuthState(new Empty())).toObject();
}
 
/**
 * Returns User record of logged in user
 *
 * @returns {Promise<User.AsObject>}
 */
export async function getCurrentUser(): Promise<User.AsObject> {
  const req = new PingReq();
 
  const response = await client.api.ping(req);
 
  return response.getUser()!.toObject();
}
 
/**
 * Returns User record by Username or id
 *
 * @param {string} user
 * @returns {Promise<User.AsObject>}
 */
export async function getUser(user: string): Promise<User.AsObject> {
  const userReq = new GetUserReq();
  userReq.setUser(user || "");
 
  const response = await client.api.getUser(userReq);
 
  return response.toObject();
}
 
/**
 * Updates user profile
 */
export async function updateProfile(
  profile: UpdateUserProfileData
): Promise<Empty> {
  const req = new UpdateProfileReq();
 
  const avatarKey = profile.avatarKey
    ? new NullableStringValue().setValue(profile.avatarKey)
    : undefined;
  const name = new wrappers.StringValue().setValue(profile.name);
  const city = new wrappers.StringValue().setValue(profile.city);
  const hometown = new NullableStringValue().setValue(profile.hometown);
  const lat = new wrappers.DoubleValue().setValue(profile.lat);
  const lng = new wrappers.DoubleValue().setValue(profile.lng);
  const radius = new wrappers.DoubleValue().setValue(profile.radius);
  const pronouns = new NullableStringValue().setValue(profile.pronouns);
  const occupation = new NullableStringValue().setValue(profile.occupation);
  const education = new NullableStringValue().setValue(profile.education);
  const aboutMe = new NullableStringValue().setValue(profile.aboutMe);
  const myTravels = new NullableStringValue().setValue(profile.myTravels);
  const thingsILike = new NullableStringValue().setValue(profile.thingsILike);
  const hostingStatus = profile.hostingStatus;
  const meetupStatus = profile.meetupStatus;
 
  const regionsVisited = new RepeatedStringValue().setValueList(
    profile.regionsVisited
  );
  const regionsLived = new RepeatedStringValue().setValueList(
    profile.regionsLived
  );
  const additionalInformation = new NullableStringValue().setValue(
    profile.additionalInformation
  );
 
  const languageAbilities = new RepeatedLanguageAbilityValue().setValueList(
    profile.languageAbilities.valueList.map((languageAbility) =>
      new LanguageAbility()
        .setCode(languageAbility.code)
        .setFluency(languageAbility.fluency)
    )
  );
 
  req
    .setAvatarKey(avatarKey)
    .setName(name)
    .setCity(city)
    .setHometown(hometown)
    .setLat(lat)
    .setLng(lng)
    .setRadius(radius)
    .setPronouns(pronouns)
    .setOccupation(occupation)
    .setEducation(education)
    .setLanguageAbilities(languageAbilities)
    .setAboutMe(aboutMe)
    .setMyTravels(myTravels)
    .setThingsILike(thingsILike)
    .setHostingStatus(hostingStatus)
    .setMeetupStatus(meetupStatus)
    .setRegionsVisited(regionsVisited)
    .setRegionsLived(regionsLived)
    .setAdditionalInformation(additionalInformation);
 
  return client.api.updateProfile(req);
}
 
export function updateAvatar(avatarKey: string) {
  const req = new UpdateProfileReq();
  req.setAvatarKey(new NullableStringValue().setValue(avatarKey));
  return client.api.updateProfile(req);
}
 
export function updateHostingPreference(preferences: HostingPreferenceData) {
  const req = new UpdateProfileReq();
 
  const maxGuests =
    preferences.maxGuests !== null
      ? new NullableUInt32Value()
          .setValue(preferences.maxGuests)
          .setIsNull(false)
      : new NullableUInt32Value().setIsNull(true);
  const lastMinute = new NullableBoolValue()
    .setValue(preferences.lastMinute)
    .setIsNull(false);
  const hasPets = new NullableBoolValue()
    .setValue(preferences.hasPets)
    .setIsNull(false);
  const acceptsPets = new NullableBoolValue()
    .setValue(preferences.acceptsPets)
    .setIsNull(false);
  const petDetails = new NullableStringValue().setValue(preferences.petDetails);
  const hasKids = new NullableBoolValue()
    .setValue(preferences.hasKids)
    .setIsNull(false);
  const acceptsKids = new NullableBoolValue()
    .setValue(preferences.acceptsKids)
    .setIsNull(false);
  const kidDetails = new NullableStringValue().setValue(preferences.kidDetails);
  const hasHousemates = new NullableBoolValue()
    .setValue(preferences.hasHousemates)
    .setIsNull(false);
  const housemateDetails = new NullableStringValue().setValue(
    preferences.housemateDetails
  );
  const wheelchairAccessible = new NullableBoolValue()
    .setValue(preferences.wheelchairAccessible)
    .setIsNull(false);
  const smokingAllowed = preferences.smokingAllowed;
  const smokesAtHome = new NullableBoolValue()
    .setValue(preferences.smokesAtHome)
    .setIsNull(false);
  const drinkingAllowed = new NullableBoolValue()
    .setValue(preferences.drinkingAllowed)
    .setIsNull(false);
  const drinksAtHome = new NullableBoolValue()
    .setValue(preferences.drinksAtHome)
    .setIsNull(false);
  const otherHostInfo = new NullableStringValue().setValue(
    preferences.otherHostInfo
  );
  const sleepingArrangement = preferences.sleepingArrangement;
  const sleepingDetails = new NullableStringValue().setValue(
    preferences.sleepingDetails
  );
  const area = new NullableStringValue().setValue(preferences.area);
  const houseRules = new NullableStringValue().setValue(preferences.houseRules);
  const parking = new NullableBoolValue()
    .setValue(preferences.parking)
    .setIsNull(false);
  const parkingDetails = preferences.parkingDetails;
  const campingOk = new NullableBoolValue()
    .setValue(preferences.campingOk)
    .setIsNull(false);
  const aboutPlace = new NullableStringValue().setValue(preferences.aboutPlace);
 
  req
    .setMaxGuests(maxGuests)
    .setLastMinute(lastMinute)
    .setHasPets(hasPets)
    .setAcceptsPets(acceptsPets)
    .setPetDetails(petDetails)
    .setHasKids(hasKids)
    .setAcceptsKids(acceptsKids)
    .setKidDetails(kidDetails)
    .setHasHousemates(hasHousemates)
    .setHousemateDetails(housemateDetails)
    .setWheelchairAccessible(wheelchairAccessible)
    .setSmokingAllowed(smokingAllowed)
    .setSmokesAtHome(smokesAtHome)
    .setDrinkingAllowed(drinkingAllowed)
    .setDrinksAtHome(drinksAtHome)
    .setOtherHostInfo(otherHostInfo)
    .setSleepingArrangement(sleepingArrangement)
    .setSleepingDetails(sleepingDetails)
    .setArea(area)
    .setHouseRules(houseRules)
    .setParking(parking)
    .setParkingDetails(parkingDetails)
    .setCampingOk(campingOk)
    .setAboutPlace(aboutPlace);
 
  return client.api.updateProfile(req);
}
 
/**
 * Logout user
 */
export function logout() {
  return client.auth.deauthenticate(new Empty());
}