// src/features/profile/services/profileService.ts import { UserProfile } from "@/features/profile/types"; import { auth, db } from "@/shared/lib/firebase/config"; import { mockDelay, mockUserProfile } from "@/shared/lib/mockData"; import { updateProfile } from "firebase/auth"; import { doc, getDoc, setDoc } from "firebase/firestore"; const USE_MOCK = process.env.EXPO_PUBLIC_USE_MOCK_DATA === "true"; export const profileService = { async getMyProfile(uid: string): Promise { if (USE_MOCK) { await mockDelay(); return mockUserProfile.uid === uid ? mockUserProfile : null; } const snapshot = await getDoc(doc(db, "users", uid)); if (!snapshot.exists()) { return null; } const data = snapshot.data(); return { uid, email: data.email ?? "", displayName: data.displayName ?? "", photoURL: data.photoURL ?? "", phone: data.phone ?? "", company: data.company ?? "", bio: data.bio ?? "", }; }, async updateMyProfile(params: { uid: string; displayName: string; photoURL?: string; phone?: string; company?: string; bio?: string; }): Promise { if (USE_MOCK) { await mockDelay(); mockUserProfile.displayName = params.displayName; if (params.photoURL !== undefined) { mockUserProfile.photoURL = params.photoURL; } if (params.phone !== undefined) { mockUserProfile.phone = params.phone; } if (params.company !== undefined) { mockUserProfile.company = params.company; } if (params.bio !== undefined) { mockUserProfile.bio = params.bio; } return; } const { uid, displayName, photoURL, phone, company, bio } = params; const currentUser = auth.currentUser; await setDoc( doc(db, "users", uid), { email: currentUser?.email ?? "", displayName, ...(photoURL !== undefined ? { photoURL } : {}), ...(phone !== undefined ? { phone } : {}), ...(company !== undefined ? { company } : {}), ...(bio !== undefined ? { bio } : {}), }, { merge: true }, ); if (currentUser) { await updateProfile(currentUser, { displayName, ...(photoURL !== undefined ? { photoURL } : {}), }); } }, };