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

Function updateUsersWithNewUsernames

models/users.js:1029–1112  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1027 }
1028 return users;
1029 } catch (err) {
1030 logger.error("Firebase fetch operation failed!");
1031 return [];
1032 }
1033};
1034
1035const getNonNickNameSyncedUsers = async () => {
1036 try {
1037 const usersRef = await userModel
1038 .where("roles.archived", "==", false)
1039 .where("nickname_synced", "==", false)
1040 .where("discordId", "!=", null)
1041 .get();
1042 const users = [];
1043 usersRef.forEach((user) => {
1044 const userData = user.data();
1045 if (userData?.discordId)
1046 users.push({
1047 ...userData,
1048 id: user.id,
1049 });
1050 });
1051 return users;
1052 } catch (err) {
1053 logger.error(`Error while fetching all users: ${err}`);
1054 throw err;
1055 }
1056};
1057
1058const updateUsernamesInBatch = async (usersData) => {
1059 const batch = firestore.batch();
1060 const usersBatch = [];
1061 const summary = {
1062 totalUpdatedUsernames: 0,
1063 totalOperationsFailed: 0,
1064 failedUserDetails: [],
1065 };
1066
1067 usersData.forEach((user) => {
1068 const updateUserData = { ...user, username: user.username };
1069 batch.update(userModel.doc(user.id), updateUserData);
1070 usersBatch.push(user.id);
1071 });
1072
1073 try {
1074 await batch.commit();
1075 summary.totalUpdatedUsernames += usersData.length;
1076 usersBatch.forEach((id) => {
1077 userCache.invalidateUser(id);
1078 });
1079 return { ...summary };
1080 } catch (err) {
1081 logger.error("Firebase batch Operation Failed!");
1082 summary.totalOperationsFailed += usersData.length;
1083 summary.failedUserDetails = [...usersBatch];
1084 return { ...summary };
1085 }
1086};

Callers

nothing calls this directly

Calls 4

formatUsernameFunction · 0.85
chunksFunction · 0.85
updateUsernamesInBatchFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected