Skip to content

Commit 7be7d6d

Browse files
authored
Merge pull request #955 from mapswipe/feature/username-lowercase-function
Add cloud function to save username in lowercase
2 parents 4e6fd4a + 8318b07 commit 7be7d6d

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

firebase/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+
});

mapswipe_workers/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ RUN mkdir --parents .local/share/mapswipe_workers
1919
COPY . .
2020

2121
# Update setuptools and install mapswipe-workers with dependencies (requirements.txt)
22-
RUN pip3 install --upgrade setuptools
22+
# XXX: setuptools==70.3.0 Fix packaging.version.InvalidVersion error
23+
RUN pip3 install --upgrade "setuptools==70.3.0"
2324
RUN pip3 install .
2425

2526
# Don't use a CMD here, this will be defined in docker-compose.yaml

0 commit comments

Comments
 (0)