@@ -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
1111import { redirect , token } from './osm_auth' ;
12+ import { formatProjectTopic , formatUserName } from './utils' ;
1213
1314exports . 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} ) ;
0 commit comments