@@ -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} ) ;
0 commit comments