Skip to content

Commit 3f1c8d5

Browse files
author
puranban
committed
Add cloud function to save username in lowercase
1 parent 4e6fd4a commit 3f1c8d5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

firebase/functions/src/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,34 @@ exports.addProjectTopicKey = functions.https.onRequest(async (_, res) => {
374374
res.status(500).send('Error fetching projects data');
375375
}
376376
});
377+
378+
exports.addUserNameLowercase = functions.https.onRequest(async (_, res) => {
379+
try {
380+
const userRef = admin.database().ref('v2/users');
381+
const snapshot = userRef.once('value');
382+
const data = (await snapshot).val();
383+
384+
if (data) {
385+
const newUserData: {[key: string]: string} = {};
386+
387+
Object.keys(data).forEach((id) => {
388+
if (data[id]?.username) {
389+
const newUserNameKey = data[id]?.username?.toLowerCase() as string;
390+
newUserData[`v2/users/${id}/userNameKey`] = newUserNameKey;
391+
}
392+
});
393+
// NOTE: Update database with the new username lowercase
394+
await admin.database().ref().update(newUserData);
395+
396+
// Fetch updated data
397+
const updatedSnapshot = await admin.database().ref('v2/users').once('value');
398+
const updatedUsersData = updatedSnapshot.val();
399+
400+
res.status(200).send(updatedUsersData);
401+
} else {
402+
res.status(404).send('No user found');
403+
}
404+
} catch (error) {
405+
res.status(500).send('Error fetching user data');
406+
}
407+
});

0 commit comments

Comments
 (0)