import {debug} from 'common/logger' import {PrivateUser} from 'common/user' import {getDefaultNotificationPreferences} from 'common/user-notification-preferences' import {cleanUsername} from 'common/util/clean-username' import {randomString} from 'common/util/random' import {createSupabaseDirectClient} from 'shared/supabase/init' import {insert} from 'shared/supabase/utils' import {getUser} from 'shared/utils' import UserAccountInformationForSeeding from '../backend/utils/userInformation' import {firebaseSignUp} from './firebaseUtils' /** * Function used to populate the database with profiles. * * @param userInfo - Class object containing information to create a user account generated by `fakerjs`. * @param profileType - Optional param used to signify how much information is used in the account generation. */ export async function seedDbUser( userInfo: UserAccountInformationForSeeding, profileType?: string, ): Promise { const pg = createSupabaseDirectClient() const userId = userInfo.user_id const deviceToken = randomString() const bio = { type: 'doc', content: [ { type: 'paragraph', content: [ { text: userInfo.bio, type: 'text', }, ], }, ], } const basicProfile = { user_id: userId, bio_length: userInfo.bio.length, bio: bio, age: userInfo.age, born_in_location: userInfo.born_in_location, company: userInfo.company, } const mediumProfile = { ...basicProfile, drinks_per_month: userInfo.drinks_per_month, diet: [userInfo.randomElement(userInfo.diet)], education_level: userInfo.randomElement(userInfo.education_level), ethnicity: [userInfo.randomElement(userInfo.ethnicity)], gender: userInfo.randomElement(userInfo.gender), height_in_inches: userInfo.height_in_inches, pref_gender: [userInfo.randomElement(userInfo.pref_gender)], pref_age_min: userInfo.pref_age.min, pref_age_max: userInfo.pref_age.max, } const fullProfile = { ...mediumProfile, occupation_title: userInfo.occupation_title, cannabis: userInfo.randomElement(userInfo.cannabis), psychedelics: userInfo.randomElement(userInfo.psychedelics), cannabis_intention: [userInfo.randomElement(userInfo.cannabis_intention)], psychedelics_intention: [userInfo.randomElement(userInfo.psychedelics_intention)], cannabis_pref: [userInfo.randomElement(userInfo.cannabis_pref)], psychedelics_pref: [userInfo.randomElement(userInfo.psychedelics_pref)], political_beliefs: [userInfo.randomElement(userInfo.political_beliefs)], pref_relation_styles: [userInfo.randomElement(userInfo.pref_relation_styles)], religion: [userInfo.randomElement(userInfo.religion)], } const profileData = profileType === 'basic' ? basicProfile : profileType === 'medium' ? mediumProfile : fullProfile const privateUser: PrivateUser = { id: userId, email: userInfo.email, initialIpAddress: userInfo.ip, initialDeviceToken: deviceToken, notificationPreferences: getDefaultNotificationPreferences(), blockedUserIds: [], blockedByUserIds: [], } return pg.tx(async (tx: any) => { const preexistingUser = await getUser(userId, tx) if (preexistingUser) return false await insert(tx, 'users', { id: userId, name: userInfo.name, username: cleanUsername(userInfo.userName), data: {}, }) await insert(tx, 'private_users', { id: userId, data: privateUser, }) await insert(tx, 'profiles', profileData) return true }) } export async function seedUser( email?: string | undefined, password?: string | undefined, profileType?: string | undefined, displayName?: string | undefined, userName?: string | undefined, ) { const userInfo = new UserAccountInformationForSeeding() if (email) userInfo.email = email if (password) userInfo.password = password if (displayName) userInfo.name = displayName if (userName) userInfo.userName = userName userInfo.user_id = await firebaseSignUp(userInfo.email, userInfo.password) if (userInfo.user_id) { const created = await seedDbUser(userInfo, profileType ?? 'full') if (created) debug('User created in Firebase and Supabase:', userInfo.email) } }