All files / app/service references.ts

21.95% Statements 9/41
33.33% Branches 2/6
16.66% Functions 1/6
21.95% Lines 9/41

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            79x   79x   79x   79x                                                               77x                                   77x                                 77x                   77x                               77x                              
import type { ReferenceTypeState } from "features/profile/view/References";
import {
  AvailableWriteReferencesReq,
  ListReferencesReq,
  WriteFriendReferenceReq,
  WriteHostRequestReferenceReq,
} from "proto/references_pb";
 
import client from "./client";
 
const REFERENCES_PAGE_SIZE = 25;
 
export enum ReferenceTypeStrings {
  "friend",
  "surfed",
  "hosted",
}
 
interface GetReferencesBaseInput {
  userId: number;
  pageToken?: string;
}
 
interface GetAvailableReferencesInput {
  userId: number;
}
 
interface WriteReferenceBaseInput {
  text: string;
  wasAppropriate: boolean;
  rating: number;
}
 
export interface WriteHostRequestReferenceInput
  extends WriteReferenceBaseInput {
  hostRequestId: number;
}
 
export interface WriteFriendReferenceInput extends WriteReferenceBaseInput {
  toUserId: number;
}
 
type GetReferencesGivenInput = GetReferencesBaseInput;
 
export async function getReferencesGivenByUser({
  userId,
  pageToken = "0",
}: GetReferencesGivenInput) {
  const req = new ListReferencesReq();
  req.setFromUserId(userId);
  req.setReferenceTypeFilterList([]);
  req.setPageSize(REFERENCES_PAGE_SIZE);
  req.setPageToken(pageToken);
 
  const res = await client.references.listReferences(req);
  return res.toObject();
}
 
interface GetReferencesReceivedInput extends GetReferencesBaseInput {
  referenceType: Exclude<ReferenceTypeState, "given">;
}
 
export async function getReferencesReceivedForUser({
  userId,
  pageToken = "0",
  referenceType,
}: GetReferencesReceivedInput) {
  const req = new ListReferencesReq();
  req.setToUserId(userId);
  req.setReferenceTypeFilterList(
    referenceType !== "all" ? [referenceType] : []
  );
  req.setPageSize(REFERENCES_PAGE_SIZE);
  req.setPageToken(pageToken);
 
  const res = await client.references.listReferences(req);
  return res.toObject();
}
 
export async function getAvailableReferences({
  userId,
}: GetAvailableReferencesInput) {
  const req = new AvailableWriteReferencesReq();
  req.setToUserId(userId);
 
  const res = await client.references.availableWriteReferences(req);
  return res.toObject();
}
 
export async function writeHostRequestReference({
  hostRequestId,
  text,
  wasAppropriate,
  rating,
}: WriteHostRequestReferenceInput) {
  const req = new WriteHostRequestReferenceReq();
  req.setHostRequestId(hostRequestId);
  req.setText(text);
  req.setWasAppropriate(wasAppropriate);
  req.setRating(rating);
 
  const res = await client.references.writeHostRequestReference(req);
  return res.toObject();
}
 
export async function writeFriendRequestReference({
  toUserId,
  text,
  wasAppropriate,
  rating,
}: WriteFriendReferenceInput) {
  const req = new WriteFriendReferenceReq();
  req.setToUserId(toUserId);
  req.setText(text);
  req.setWasAppropriate(wasAppropriate);
  req.setRating(rating);
 
  const res = await client.references.writeFriendReference(req);
  return res.toObject();
}