Skip to content

Commit 1d1b7eb

Browse files
authored
Merge pull request #958 from mapswipe/fix/project-draft-topic
Fix project draft topic save
2 parents 3ac41b6 + a7b0d36 commit 1d1b7eb

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

database.rules.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
"
3737
},
3838
".indexOn": [
39-
"status", "isFeatured", "teamId"
39+
"status",
40+
"isFeatured",
41+
"teamId",
42+
"projectTopicKey"
4043
]
4144
},
4245
"projectDrafts": {
@@ -138,7 +141,8 @@
138141
},
139142
".indexOn": [
140143
"created",
141-
"teamId"
144+
"teamId",
145+
"usernameKey"
142146
]
143147
},
144148
"OSMAccessToken": {

functions/src/index.ts

Lines changed: 26 additions & 25 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

@@ -349,28 +350,28 @@ exports.addProjectTopicKey = functions.https.onRequest(async (_, res) => {
349350
const projectRef = await admin.database().ref('v2/projects').once('value');
350351
const data = projectRef.val();
351352

352-
if (data) {
353+
const isEmptyProject = Object.keys(data).length === 0;
354+
if (isEmptyProject) {
355+
res.status(404).send('No projects found');
356+
}
357+
358+
if (!isEmptyProject && data) {
353359
const newProjectData: {[key: string]: string} = {};
354360

355361
Object.keys(data).forEach((id) => {
356362
if (data[id]?.projectTopic) {
357-
const newProjectTopicKey = data[id].projectTopic?.toLowerCase() as string;
363+
const newProjectTopicKey = formatProjectTopic(data[id].projectTopic);
358364
newProjectData[`v2/projects/${id}/projectTopicKey`] = newProjectTopicKey;
359365
}
360366
});
361-
// NOTE: Update database with the new topic key
362-
await admin.database().ref().update(newProjectData);
363367

364-
// Fetch updated data
365-
const updatedSnapshot = await admin.database().ref('v2/projects').once('value');
366-
const updatedProjectData = updatedSnapshot.val();
367-
368-
res.status(200).send(updatedProjectData);
369-
} else {
370-
res.status(404).send('No projects found');
368+
await admin.database().ref().update(newProjectData);
369+
const updatedProjectsCount = Object.keys(newProjectData).length;
370+
res.status(200).send(`Updated ${updatedProjectsCount} projects.`);
371371
}
372372
} catch (error) {
373-
res.status(500).send('Error fetching projects data');
373+
console.log(error);
374+
res.status(500).send('Some error occurred');
374375
}
375376
});
376377

@@ -379,27 +380,27 @@ exports.addUserNameLowercase = functions.https.onRequest(async (_, res) => {
379380
const userRef = await admin.database().ref('v2/users').once('value');
380381
const data = userRef.val();
381382

382-
if (data) {
383+
const isEmptyUser = Object.keys(data).length === 0;
384+
if (isEmptyUser) {
385+
res.status(404).send('No user found');
386+
}
387+
388+
if (!isEmptyUser && data) {
383389
const newUserData: {[key: string]: string} = {};
384390

385391
Object.keys(data).forEach((id) => {
386392
if (data[id]?.username) {
387-
const newUserNameKey = data[id].username?.toLowerCase() as string;
388-
newUserData[`v2/users/${id}/userNameKey`] = newUserNameKey;
393+
const newUsernameKey = formatUserName(data[id].username);
394+
newUserData[`v2/users/${id}/usernameKey`] = newUsernameKey;
389395
}
390396
});
391-
// NOTE: Update database with the new username lowercase
392-
await admin.database().ref().update(newUserData);
393397

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');
398+
await admin.database().ref().update(newUserData);
399+
const updatedUserCount = Object.keys(newUserData).length;
400+
res.status(200).send(`Updated ${updatedUserCount} users.`);
401401
}
402402
} catch (error) {
403-
res.status(500).send('Error fetching user data');
403+
console.log(error);
404+
res.status(500).send('Some error occurred');
404405
}
405406
});

functions/src/utils/index.ts

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+
};

0 commit comments

Comments
 (0)