import { User } from '@entities/User' ; import { firebaseAdmin } from '@src/utils/auth/firebaseAdmin' ; import { getRepository } from 'typeorm' ; export const getOrRegisterUser = async ( user ) => { try { const userRepository = await getRepository ( User ) ; let name = '' ; let surname = '' ; if ( user ?. name ) { name = user . name . substr ( 0 , user . name . indexOf ( ' ' ) ) ; surname = user . name . substr ( user . name . indexOf ( ' ' ) + 1 ) ; } let foundUser = await userRepository . findOne ( { where : { id : user . uid } , cache : true , } ) ; if ( ! foundUser ) { foundUser = await userRepository . create ( { id : user . uid , firstName : name , lastName : surname , email : user . email , picture : user . picture , } ) ; foundUser . save ( ) ; } return foundUser ; } catch ( error ) { console . error ( 'Could not get or register user: ' , error ) ; return ; } } ;