Skip to content

Commit

Permalink
created getUserProfileById
Browse files Browse the repository at this point in the history
If possible, refactor to call getUserProfile with custom parameters
  • Loading branch information
pyerino committed Oct 10, 2024
1 parent fad6682 commit 6f89aec
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
73 changes: 73 additions & 0 deletions functions/src/functions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const signUp = functions.https.onCall(async (data, context) => {
});

export const getUserProfile = functions.https.onCall(async (_, context) => {

if (!context.auth) {
return serializedErrorResponse("unauthenticated", "The request has to be authenticated.");
}
Expand Down Expand Up @@ -97,6 +98,78 @@ export const getUserProfile = functions.https.onCall(async (_, context) => {
}
});

export const getUserProfileById = functions.https.onCall(async (data, context) => {

const {userId} = data;

if (!context.auth?.uid) {
return serializedErrorResponse("unauthenticated", "The request has to be authenticated.");
}

const userDoc = await db.collection("users").doc(context.auth.uid).get();

if (!userDoc.exists) {
return serializedErrorResponse("user-not-found", "User not found.");
}

const userData = userDoc.data();

if (!userData) {
return serializedErrorResponse("user-not-found", "User data not found.");
}

if (userData.role != "staff") {
return serializedErrorResponse("permission-denied", "User not authorized.");
}


try {
const userDoc = await db.collection("users").doc(userId).get();

if (!userDoc.exists) {
return serializedErrorResponse("user-not-found", "The user does not exist.");
}

const userData = userDoc.data();

if (!userData) {
return serializedErrorResponse("user-not-found", "The user does not exist.");
}

var group: Group | null = null;

if (userData.group != null) {
const groupResponse: GenericResponse<Group> = await parseGroupRef(userData.group);

if (groupResponse.error) {
return serializedErrorResponse(groupResponse.error.errorCode, groupResponse.error.details);
}

group = groupResponse.data as Group;
}

const userProfile: UserProfile = {
userId: userDoc.id,
nickname: userData.nickname,
email: userData.email,
name: userData.name,
surname: userData.surname,
group: group,
groupId: group ? group.groupId : null,
position: null,
score: userData.score,
role: null
};

return serializedSuccessResponse(userProfile);
} catch (error) {
console.error("An error occurred while fetching the user profile.", error);
return serializedExceptionResponse(error);
}

});


export const redeemAuthCode = functions.https.onCall(async (data, context) => {
const {code} = data;

Expand Down
3 changes: 2 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require("module-alias/register");

import * as admin from "firebase-admin";
import { getUserProfile, signUp, redeemAuthCode } from "./functions/auth";
import { getUserProfile, signUp, redeemAuthCode, getUserProfileById } from "./functions/auth";
import { createTalk, getTalkList } from "./functions/talk";
import { getSponsorList } from "./functions/sponsor";
import { getLeaderboard, refreshLeaderboard } from "./functions/leaderboard";
Expand Down Expand Up @@ -34,4 +34,5 @@ export {
refreshLeaderboard,
redeemAuthCode,
addPointsToUsers,
getUserProfileById,
};

0 comments on commit 6f89aec

Please sign in to comment.