generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 201
/
profileDiffs.js
99 lines (87 loc) · 2.46 KB
/
profileDiffs.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
const { profileStatus } = require("../constants/users");
const firestore = require("../utils/firestore");
const profileDiffsModel = firestore.collection("profileDiffs");
const obfuscate = require("../utils/obfuscate");
/**
* Add profileDiff
* Fetches the pending profile diffs
* @return {Promise<profileDiffsModel|Array>}
*/
const fetchProfileDiffs = async () => {
try {
const snapshot = await profileDiffsModel.where("approval", "==", profileStatus.PENDING).get();
const profileDiffs = [];
snapshot.forEach((doc) => {
const { email = "", phone = "" } = doc.data();
const emailRedacted = obfuscate.obfuscateMail(email);
const phoneRedacted = obfuscate.obfuscatePhone(phone);
profileDiffs.push({
id: doc.id,
...doc.data(),
email: emailRedacted,
phone: phoneRedacted,
});
});
return profileDiffs;
} catch (err) {
logger.error("Error retrieving profile diffs ", err);
throw err;
}
};
/**
* Fetches the profileDiff data of the provided profileDiff Id
* @param profileDiffId profileDiffId of the diffs need to be fetched
* @returns profileDiff Data
*/
const fetchProfileDiff = async (profileDiffId) => {
try {
const profileDiff = await profileDiffsModel.doc(profileDiffId).get();
const profileDiffData = profileDiff.data();
return profileDiffData;
} catch (err) {
logger.error("Error retrieving profile Diff", err);
throw err;
}
};
/** Add profileDiff
*
* @param profileDiffData { Object }: Data to be added
*/
const add = async (profileDiffData) => {
try {
const profileDiff = await profileDiffsModel.add({
...profileDiffData,
});
return profileDiff.id;
} catch (err) {
logger.error("Error in adding profile diff", err);
throw err;
}
};
/**
* Update profileDiff
*
* @param profileDiffData { Object }: Data to be added
* @param profileId { String }: Id of the profileDiff
*/
const updateProfileDiff = async (profileDiffData, profileId) => {
try {
const profileDiff = await profileDiffsModel.doc(profileId).get();
const data = profileDiff.data();
if (!data) return { notFound: true };
await profileDiffsModel.doc(profileId).set({
...data,
...profileDiffData,
});
return { id: profileDiff.id, userId: data.userId };
} catch (err) {
logger.error("Error in updating profile diff", err);
throw err;
}
};
module.exports = {
fetchProfileDiffs,
fetchProfileDiff,
add,
updateProfileDiff,
};