MCPcopy Create free account
hub / github.com/RealDevSquad/website-backend / fetchUser

Function fetchUser

models/users.js:388–439  ·  view source on GitHub ↗
({ userId = null, username = null, githubUsername = null, discordId = null, email = null })

Source from the content-addressed store, hash-verified

386
387/**
388 * Fetches the user data from the the provided username or userId
389 *
390 * @param { Object }: Object with username and userId, any of the two can be used
391 * @return {Promise<{userExists: boolean, user: <userModel>}|{userExists: boolean, user: <userModel>}>}
392 */
393const fetchUser = async ({ userId = null, username = null, githubUsername = null, discordId = null, email = null }) => {
394 try {
395 // Cache lookup keys mirror the query branches below.
396 const cacheKeys = [];
397 if (userId) cacheKeys.push(`user:userId:${userId}`);
398 if (username) cacheKeys.push(`user:username:${username}`);
399 if (githubUsername) cacheKeys.push(`user:github:${githubUsername}`);
400 if (discordId) cacheKeys.push(`user:discordId:${discordId}`);
401 if (email) cacheKeys.push(`user:email:${email}`);
402
403 const cachedUser = cacheKeys.length === 1 ? userCache.get(cacheKeys[0]) : null;
404 if (cachedUser) {
405 if (discordId && cachedUser.roles?.archived !== false) {
406 userCache.invalidateUser(cachedUser.id);
407 } else {
408 return {
409 userExists: true,
410 user: cachedUser,
411 };
412 }
413 }
414
415 let userData, id;
416 if (username) {
417 const user = await userModel.where("username", "==", username).limit(1).get();
418 user.forEach((doc) => {
419 id = doc.id;
420 userData = doc.data();
421 });
422 } else if (userId) {
423 const user = await userModel.doc(userId).get();
424 id = userId;
425 userData = user.data();
426 } else if (githubUsername) {
427 const user = await userModel.where("github_id", "==", githubUsername).limit(1).get();
428 user.forEach((doc) => {
429 id = doc.id;
430 userData = doc.data();
431 });
432 } else if (discordId) {
433 const user = await userModel.where("discordId", "==", discordId).where("roles.archived", "==", false).get();
434 user.forEach((doc) => {
435 id = doc.id;
436 userData = doc.data();
437 });
438 } else if (email) {
439 const user = await userModel.where("email", "==", email).limit(1).get();
440 user.forEach((doc) => {
441 id = doc.id;
442 userData = doc.data();

Callers 12

addGroupRoleToMemberFunction · 0.85
deleteRoleFunction · 0.85
assertUserExistsFunction · 0.85
fetchPRsByUserFunction · 0.85
getUserDetailsFunction · 0.85
fetchParticipantsDataFunction · 0.85
getSuggestedUsersFunction · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected