Skip to content

Commit 30a6b57

Browse files
author
puranban
committed
Feat: add utility function to format project and other improvements
1 parent 51f8345 commit 30a6b57

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

firebase/database.rules.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@
141141
},
142142
".indexOn": [
143143
"created",
144-
"teamId"
144+
"teamId",
145+
"usernameKey"
145146
]
146147
},
147148
"OSMAccessToken": {

firebase/functions/src/index.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ admin.initializeApp();
99
// seem possible to split them using the split system for multiple sites from
1010
// https://firebase.google.com/docs/hosting/multisites
1111
import {redirect, token} from './osm_auth';
12+
import { formatProjectTopic, formatUserName } from './utils';
1213

1314
exports.osmAuth = {};
1415

@@ -359,21 +360,18 @@ exports.addProjectTopicKey = functions.https.onRequest(async (_, res) => {
359360

360361
Object.keys(data).forEach((id) => {
361362
if (data[id]?.projectTopic) {
362-
const newProjectTopicKey = data[id].projectTopic?.toLowerCase() as string;
363+
const newProjectTopicKey = formatProjectTopic(data[id].projectTopic);
363364
newProjectData[`v2/projects/${id}/projectTopicKey`] = newProjectTopicKey;
364365
}
365366
});
366-
// NOTE: Update database with the new topic key
367-
await admin.database().ref().update(newProjectData);
368367

369-
await admin.database().ref().update(newProjectData).then(() => {
370-
const updatedProjectsCount = Object.keys(newProjectData).length;
371-
res.status(200).send(`Updated ${updatedProjectsCount} projects.`);
372-
return;
373-
});
368+
await admin.database().ref().update(newProjectData);
369+
const updatedProjectsCount = Object.keys(newProjectData).length;
370+
res.status(200).send(`Updated ${updatedProjectsCount} projects.`);
374371
}
375372
} catch (error) {
376-
res.status(500).send(error);
373+
console.log(error);
374+
res.status(500).send('Some error occurred');
377375
}
378376
});
379377

@@ -392,18 +390,17 @@ exports.addUserNameLowercase = functions.https.onRequest(async (_, res) => {
392390

393391
Object.keys(data).forEach((id) => {
394392
if (data[id]?.username) {
395-
const newUserNameKey = (data[id].username.trim()).toLowerCase() as string;
396-
newUserData[`v2/users/${id}/userNameKey`] = newUserNameKey;
393+
const newUsernameKey = formatUserName(data[id].username);
394+
newUserData[`v2/users/${id}/usernameKey`] = newUsernameKey;
397395
}
398396
});
399-
// NOTE: Update database with the new username lowercase
400-
await admin.database().ref().update(newUserData).then(() => {
401-
const updatedUserCount = Object.keys(newUserData).length;
402-
res.status(200).send(`Updated ${updatedUserCount} users.`);
403-
return;
404-
});
397+
398+
await admin.database().ref().update(newUserData);
399+
const updatedUserCount = Object.keys(newUserData).length;
400+
res.status(200).send(`Updated ${updatedUserCount} users.`);
405401
}
406402
} catch (error) {
407-
res.status(500).send(error);
403+
console.log(error);
404+
res.status(500).send('Some error occurred');
408405
}
409406
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// NOTE: We have a similar function in manager-dashbaord
2+
// manager-dashbaord/app/utills/common.tsx
3+
4+
export const formatProjectTopic = (projectTopic: string) => {
5+
// Note: this will remove start and end space
6+
const projectWithoutStartAndEndSpace = projectTopic.trim();
7+
8+
// Note: this will change multi space to single space
9+
const removeMultiSpaceToSingle = projectWithoutStartAndEndSpace.replace(/\s+/g, ' ');
10+
const newProjectTopic = removeMultiSpaceToSingle.toLowerCase();
11+
12+
return newProjectTopic;
13+
};
14+
15+
// NOTE: this validation mirrors feature from the app on signup
16+
export const formatUserName = (name: string) => {
17+
// Note: remove all space
18+
const removeUserNameSpace = name.replace(/\s+/g, '');
19+
const userNameLowercase = removeUserNameSpace.toLowerCase();
20+
21+
return userNameLowercase;
22+
};

manager-dashboard/app/utils/common.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,16 @@ export const iconMap = listToMap(
308308
(icon) => icon.key,
309309
(icon) => icon.component,
310310
);
311+
312+
// NOTE: We have a similar function in firebase function utils
313+
// firebase/functions/src/utils/index.ts
314+
export const formatProjectTopic = (projectTopic: string) => {
315+
// Note: this will remove start and end space
316+
const projectWithoutStartAndEndSpace = projectTopic.trim();
317+
318+
// Note: this will change multi space to single space
319+
const removeMultiSpaceToSingle = projectWithoutStartAndEndSpace.replace(/\s+/g, ' ');
320+
const newProjectTopic = removeMultiSpaceToSingle.toLowerCase();
321+
322+
return newProjectTopic;
323+
};

manager-dashboard/app/views/NewProject/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
PROJECT_TYPE_FOOTPRINT,
6060
PROJECT_TYPE_COMPLETENESS,
6161
PROJECT_TYPE_CHANGE_DETECTION,
62+
formatProjectTopic,
6263
} from '#utils/common';
6364
import { getValueFromFirebase } from '#utils/firebase';
6465

@@ -327,11 +328,13 @@ function NewProject(props: Props) {
327328
return;
328329
}
329330

331+
// NOTE: All the user don't have permission to access draft project
332+
// FIXME: The firebase rules need to be changed to perform this on draft project
330333
const database = getDatabase();
331-
// Note: remove start and end space
332-
const projectTopicKey = (projectTopic?.trim())?.toLowerCase() as string;
333-
334+
const projectTopicKeyLowercase = (projectTopic?.trim())?.toLowerCase() as string;
335+
const projectTopicKey = formatProjectTopic(projectTopicKeyLowercase);
334336
const projectRef = databaseRef(database, 'v2/projects/');
337+
335338
const prevProjectNameQuery = query(
336339
projectRef,
337340
orderByChild('projectTopicKey'),

0 commit comments

Comments
 (0)