Skip to content

Commit

Permalink
Changed getConnectedUserDisplayName() to getConnectedUser(), and retu…
Browse files Browse the repository at this point in the history
…rn all ConnectedUser document information. Updated /users?userId GET endpoint to match this change.
  • Loading branch information
h1divp committed Mar 20, 2024
1 parent ca66f6f commit aa9fef5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 3 additions & 5 deletions server/src/actions/getConnectedUsers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { distanceBetween, geohashForLocation, geohashQueryBounds } from 'geofire-common'
import { connectedUsersCollection } from '../utilities/firebaseInit'

export const getConnectedUserDisplayName = async (socketID: string) => {
export const getConnectedUser = async (socketID: string) => {
try {
const user = await connectedUsersCollection.doc(socketID).get();
return {
"displayName": user.data().displayName
}
return user.data();
} catch (error) {
console.error("getConnectedUserDisplayName() failed:", error.message);
console.error("getConnectedUser failed:", error.message);
return false;
}
}
Expand Down
14 changes: 9 additions & 5 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createMessage } from './actions/createMessage';
import { createUser } from './actions/createConnectedUser';
import { toggleUserConnectionStatus, updateUserLocation, updateUserDisplayName } from './actions/updateConnectedUser';
import { deleteConnectedUserByUID } from './actions/deleteConnectedUser';
import { findNearbyUsers, getConnectedUserDisplayName } from './actions/getConnectedUsers';
import { findNearbyUsers, getConnectedUser } from './actions/getConnectedUsers';
import {geohashForLocation} from 'geofire-common';
import { ConnectedUser } from './types/User';
import { getAuth } from 'firebase-admin/auth';
Expand Down Expand Up @@ -165,17 +165,18 @@ app.get("/users", async (req, res) => {

const userIds = await findNearbyUsers(lat, lon, radius);
res.json(userIds);

} else if (req.query.userId) {
query = "?userId";
const userId = req.query.userId;
if (typeof userId != "string") throw Error(" [userId] is not a string.");

const displayName = await getConnectedUserDisplayName(userId);
if (displayName) {
res.json(displayName);
const user = await getConnectedUser(userId);
if (user) {
res.json(user);
} else {
// getConnectedUserDisplayName() will return false is an error is thrown, and print it to console.
throw Error("getConnectedUserDisplayName() failed.");
throw Error("getConnectedUser() failed.");
}
}
} catch (error) {
Expand Down Expand Up @@ -226,6 +227,7 @@ app.put("/users", async (req, res) => {

const success = await toggleUserConnectionStatus(userId);
if (!success) throw Error(" toggleUserConnectionStatus() failed.");

} else if (req.query.userId && req.query.lat && req.query.lon) {
query = "?userId&lat&lon";
const userId = req.query.userId;
Expand All @@ -237,6 +239,7 @@ app.put("/users", async (req, res) => {

const success = await updateUserLocation(userId, lat, lon);
if (!success) throw Error(" toggleUserConnectionStatus() failed.");

} else if (req.query.userId && req.query.displayName) {
query = "?userId&displayName";
const userId = req.query.userId;
Expand All @@ -249,6 +252,7 @@ app.put("/users", async (req, res) => {
}
console.log(`[EXP] Request <PUT /users${query}> returned successfully.`);
res.json(`Operation <PUT /user${query}> was handled successfully.`);

} catch (error) {
console.error(
`[EXP] Error returning request <PUT /users${query}>:\n`,
Expand Down

0 comments on commit aa9fef5

Please sign in to comment.