Skip to content

Commit 3ac41b6

Browse files
authored
Merge pull request #955 from mapswipe/feature/username-lowercase-function
Add cloud function to save username in lowercase
2 parents 16fecbc + 7ae6d04 commit 3ac41b6

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

functions/src/index.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,15 @@ exports.decProjectProgress = functions.database.ref('/v2/projects/{projectId}/re
346346

347347
exports.addProjectTopicKey = functions.https.onRequest(async (_, res) => {
348348
try {
349-
const projectRef = admin.database().ref('v2/projects');
350-
const snapshot = projectRef.once('value');
351-
const data = (await snapshot).val();
349+
const projectRef = await admin.database().ref('v2/projects').once('value');
350+
const data = projectRef.val();
352351

353352
if (data) {
354353
const newProjectData: {[key: string]: string} = {};
355354

356355
Object.keys(data).forEach((id) => {
357356
if (data[id]?.projectTopic) {
358-
const newProjectTopicKey = data[id]?.projectTopic?.toLowerCase() as string;
357+
const newProjectTopicKey = data[id].projectTopic?.toLowerCase() as string;
359358
newProjectData[`v2/projects/${id}/projectTopicKey`] = newProjectTopicKey;
360359
}
361360
});
@@ -374,3 +373,33 @@ exports.addProjectTopicKey = functions.https.onRequest(async (_, res) => {
374373
res.status(500).send('Error fetching projects data');
375374
}
376375
});
376+
377+
exports.addUserNameLowercase = functions.https.onRequest(async (_, res) => {
378+
try {
379+
const userRef = await admin.database().ref('v2/users').once('value');
380+
const data = userRef.val();
381+
382+
if (data) {
383+
const newUserData: {[key: string]: string} = {};
384+
385+
Object.keys(data).forEach((id) => {
386+
if (data[id]?.username) {
387+
const newUserNameKey = data[id].username?.toLowerCase() as string;
388+
newUserData[`v2/users/${id}/userNameKey`] = newUserNameKey;
389+
}
390+
});
391+
// NOTE: Update database with the new username lowercase
392+
await admin.database().ref().update(newUserData);
393+
394+
// Fetch updated data
395+
const updatedSnapshot = await admin.database().ref('v2/users').once('value');
396+
const updatedUsersData = updatedSnapshot.val();
397+
398+
res.status(200).send(updatedUsersData);
399+
} else {
400+
res.status(404).send('No user found');
401+
}
402+
} catch (error) {
403+
res.status(500).send('Error fetching user data');
404+
}
405+
});

0 commit comments

Comments
 (0)