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