1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
// 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<UserProfile | null> {
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<void> {
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 } : {}),
});
}
},
};
|