All files / app/service publicTrips.ts

12.5% Statements 8/64
0% Branches 0/26
0% Functions 0/6
13.55% Lines 8/59

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                140x     140x   139x                                         139x                                                             139x                       139x                                             139x                                                   139x                                      
import {
  CreatePublicTripReq,
  ListMyCommunitiesPublicTripsReq,
  ListPublicTripsByUserReq,
  ListPublicTripsReq,
  OfferToHostReq,
  PublicTripStatus,
  UpdatePublicTripReq,
} from "proto/public_trips_pb";
import { Temporal } from "temporal-polyfill";
 
import client from "./client";
 
export async function listPublicTrips({
  communityId,
  pageToken,
  pageSize,
}: {
  communityId: number;
  pageToken?: string;
  pageSize?: number;
}) {
  const req = new ListPublicTripsReq();
  req.setCommunityId(communityId);
  if (pageToken) {
    req.setPageToken(pageToken);
  }
  if (pageSize) {
    req.setPageSize(pageSize);
  }
  const res = await client.publicTrips.listPublicTrips(req);
  return res.toObject();
}
 
export async function listPublicTripsByUser({
  userId,
  pageToken,
  pageSize,
  ascending,
  includePast,
}: {
  userId: number;
  pageToken?: string;
  pageSize?: number;
  ascending?: boolean;
  includePast?: boolean;
}) {
  const req = new ListPublicTripsByUserReq();
  req.setUserId(userId);
  if (pageToken) {
    req.setPageToken(pageToken);
  }
  if (pageSize) {
    req.setPageSize(pageSize);
  }
  if (ascending !== undefined) {
    req.setAscending(ascending);
  }
  if (includePast !== undefined) {
    req.setIncludePast(includePast);
  }
  const res = await client.publicTrips.listPublicTripsByUser(req);
  return res.toObject();
}
 
export async function listMyCommunitiesPublicTrips({ pageToken, pageSize }: { pageToken?: string; pageSize?: number }) {
  const req = new ListMyCommunitiesPublicTripsReq();
  if (pageToken) {
    req.setPageToken(pageToken);
  }
  if (pageSize) {
    req.setPageSize(pageSize);
  }
  const res = await client.publicTrips.listMyCommunitiesPublicTrips(req);
  return res.toObject();
}
 
export async function createPublicTrip({
  communityId,
  fromDate,
  toDate,
  description,
  sameGenderOnly,
}: {
  communityId: number;
  fromDate: Temporal.PlainDate;
  toDate: Temporal.PlainDate;
  description: string;
  sameGenderOnly: boolean;
}) {
  const req = new CreatePublicTripReq();
  req.setCommunityId(communityId);
  req.setFromDate(fromDate.toString());
  req.setToDate(toDate.toString());
  req.setDescription(description);
  req.setSameGenderOnly(sameGenderOnly);
  const res = await client.publicTrips.createPublicTrip(req);
  return res.toObject();
}
 
export async function updatePublicTrip({
  tripId,
  fromDate,
  toDate,
  description,
  status,
  sameGenderOnly,
}: {
  tripId: number;
  fromDate?: Temporal.PlainDate;
  toDate?: Temporal.PlainDate;
  description?: string;
  status?: PublicTripStatus;
  sameGenderOnly?: boolean;
}) {
  const req = new UpdatePublicTripReq();
  req.setTripId(tripId);
  if (fromDate !== undefined) req.setFromDate(fromDate.toString());
  if (toDate !== undefined) req.setToDate(toDate.toString());
  if (description !== undefined) req.setDescription(description);
  if (status !== undefined) req.setStatus(status);
  if (sameGenderOnly !== undefined) req.setSameGenderOnly(sameGenderOnly);
  const res = await client.publicTrips.updatePublicTrip(req);
  return res.toObject();
}
 
export async function offerToHost({
  tripId,
  fromDate,
  toDate,
  text,
}: {
  tripId: number;
  fromDate: Temporal.PlainDate;
  toDate: Temporal.PlainDate;
  text: string;
}) {
  const req = new OfferToHostReq();
  req.setTripId(tripId);
  req.setFromDate(fromDate.toString());
  req.setToDate(toDate.toString());
  req.setText(text);
  const res = await client.publicTrips.offerToHost(req);
  return res.getHostRequestId();
}