Skip to content

Commit 51f8345

Browse files
thenav56puranban
authored andcommitted
Add projectTopicKey to projectDraft -> project
1 parent 4f25192 commit 51f8345

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

firebase/functions/src/index.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,12 @@ exports.addProjectTopicKey = functions.https.onRequest(async (_, res) => {
349349
const projectRef = await admin.database().ref('v2/projects').once('value');
350350
const data = projectRef.val();
351351

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

355360
Object.keys(data).forEach((id) => {
@@ -361,16 +366,14 @@ exports.addProjectTopicKey = functions.https.onRequest(async (_, res) => {
361366
// NOTE: Update database with the new topic key
362367
await admin.database().ref().update(newProjectData);
363368

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');
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+
});
371374
}
372375
} catch (error) {
373-
res.status(500).send('Error fetching projects data');
376+
res.status(500).send(error);
374377
}
375378
});
376379

@@ -379,27 +382,28 @@ exports.addUserNameLowercase = functions.https.onRequest(async (_, res) => {
379382
const userRef = await admin.database().ref('v2/users').once('value');
380383
const data = userRef.val();
381384

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

385393
Object.keys(data).forEach((id) => {
386394
if (data[id]?.username) {
387-
const newUserNameKey = data[id].username?.toLowerCase() as string;
395+
const newUserNameKey = (data[id].username.trim()).toLowerCase() as string;
388396
newUserData[`v2/users/${id}/userNameKey`] = newUserNameKey;
389397
}
390398
});
391399
// 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');
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+
});
401405
}
402406
} catch (error) {
403-
res.status(500).send('Error fetching user data');
407+
res.status(500).send(error);
404408
}
405409
});

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ import BasicProjectInfoForm from './BasicProjectInfoForm';
8585
// eslint-disable-next-line postcss-modules/no-unused-class
8686
import styles from './styles.css';
8787

88-
const PROJECT_ERROR_MESSAGE = 'A project with this name already exists, please use a different project name (Please note that the name comparison is not case sensitive)';
89-
9088
const defaultProjectFormValue: PartialProjectFormType = {
9189
// projectType: PROJECT_TYPE_BUILD_AREA,
9290
projectNumber: 1,
@@ -330,7 +328,8 @@ function NewProject(props: Props) {
330328
}
331329

332330
const database = getDatabase();
333-
const projectTopicKey = projectTopic?.toLowerCase() as string;
331+
// Note: remove start and end space
332+
const projectTopicKey = (projectTopic?.trim())?.toLowerCase() as string;
334333

335334
const projectRef = databaseRef(database, 'v2/projects/');
336335
const prevProjectNameQuery = query(
@@ -347,7 +346,7 @@ function NewProject(props: Props) {
347346
if (snapshot.exists()) {
348347
setError((prevErr) => ({
349348
...getErrorObject(prevErr),
350-
[nonFieldError]: PROJECT_ERROR_MESSAGE,
349+
[nonFieldError]: 'A project with this name already exists, please use a different project name (Please note that the name comparison is not case sensitive)',
351350
projectTopic: 'A project with this name already exists',
352351
}));
353352
setProjectSubmissionStatus(undefined);

0 commit comments

Comments
 (0)