Skip to content

Commit 14fd4f4

Browse files
authored
Merge pull request #2188 from Real-Dev-Squad/develop
Dev to main (Added Pagination in ProfileDiffs API)
2 parents 742ebb9 + 29122bb commit 14fd4f4

File tree

7 files changed

+364
-7
lines changed

7 files changed

+364
-7
lines changed

controllers/profileDiffs.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,53 @@ const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");
1010

1111
const getProfileDiffs = async (req, res) => {
1212
try {
13-
const pendingProfileDiffs = await profileDiffsQuery.fetchProfileDiffs();
13+
if (!req.query.dev) {
14+
const pendingProfileDiffs = await profileDiffsQuery.fetchProfileDiffs();
1415

15-
return res.json({
16-
message: "Profile Diffs returned successfully!",
17-
profileDiffs: pendingProfileDiffs,
18-
});
16+
return res.json({
17+
message: "Profile Diffs returned successfully!",
18+
profileDiffs: pendingProfileDiffs,
19+
});
20+
} else {
21+
const { status = "PENDING", order = "desc", size = 10, username = "", cursor = null } = req.query;
22+
const { profileDiffs, next } = await profileDiffsQuery.fetchProfileDiffsWithPagination(
23+
status,
24+
order,
25+
parseInt(size),
26+
username,
27+
cursor
28+
);
29+
30+
return res.json({
31+
message: "Profile Diffs returned successfully!",
32+
profileDiffs,
33+
next,
34+
});
35+
}
1936
} catch (error) {
2037
logger.error(`Error while fetching profile diffs: ${error}`);
2138
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
2239
}
2340
};
2441

42+
const getProfileDiff = async (req, res) => {
43+
try {
44+
const result = await profileDiffsQuery.fetchProfileDiff(req.params.id);
45+
if (result.profileDiffExists) {
46+
return res.json({
47+
message: "Profile Diff returned successfully!",
48+
profileDiff: result,
49+
});
50+
}
51+
52+
return res.boom.notFound("Profile Diff doesn't exist");
53+
} catch (error) {
54+
logger.error(`Error while fetching Profile Diff: ${error}`);
55+
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
56+
}
57+
};
58+
2559
module.exports = {
2660
getProfileDiffs,
61+
getProfileDiff,
2762
};

models/profileDiffs.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { profileStatus } = require("../constants/users");
22
const firestore = require("../utils/firestore");
3+
const userModel = firestore.collection("users");
34
const profileDiffsModel = firestore.collection("profileDiffs");
45
const obfuscate = require("../utils/obfuscate");
6+
const { generateNextLink } = require("../utils/profileDiffs");
57

68
/**
79
* Add profileDiff
@@ -33,6 +35,74 @@ const fetchProfileDiffs = async () => {
3335
}
3436
};
3537

38+
const fetchProfileDiffsWithPagination = async (status, order, size, username, cursor) => {
39+
try {
40+
let query = profileDiffsModel.where("approval", "==", status);
41+
42+
if (username) {
43+
const userSnapshot = await userModel
44+
.where("username", ">=", username)
45+
.where("username", "<=", username + "\uf8ff")
46+
.get();
47+
const userIds = userSnapshot.docs.map((doc) => doc.id);
48+
if (userIds.length === 0) return { profileDiffs: [], next: "" };
49+
query = query.where("userId", "in", userIds);
50+
}
51+
52+
query = query.orderBy("timestamp", order);
53+
54+
if (cursor) {
55+
const cursorSnapshot = await profileDiffsModel.doc(cursor).get();
56+
query = query.startAfter(cursorSnapshot);
57+
}
58+
59+
const snapshot = await query.limit(size).get();
60+
61+
const profileDiffs = [];
62+
snapshot.forEach((doc) => {
63+
const data = doc.data();
64+
let emailRedacted = "";
65+
let phoneRedacted = "";
66+
if (data.email) {
67+
emailRedacted = obfuscate.obfuscateMail(data.email);
68+
}
69+
if (data.phone) {
70+
phoneRedacted = obfuscate.obfuscatePhone(data.phone);
71+
}
72+
73+
profileDiffs.push({
74+
id: doc.id,
75+
...data,
76+
email: emailRedacted,
77+
phone: phoneRedacted,
78+
});
79+
});
80+
81+
const resultDataLength = profileDiffs.length;
82+
const isNextLinkRequired = size && resultDataLength === size;
83+
const lastVisible = isNextLinkRequired && profileDiffs[resultDataLength - 1];
84+
85+
const nextPageParams = {
86+
dev: true,
87+
status,
88+
order,
89+
size,
90+
username,
91+
cursor: lastVisible?.id,
92+
};
93+
94+
let nextLink = "";
95+
if (lastVisible) {
96+
nextLink = generateNextLink(nextPageParams);
97+
}
98+
99+
return { profileDiffs, next: nextLink };
100+
} catch (err) {
101+
logger.error("Error retrieving profile diffs ", err);
102+
throw err;
103+
}
104+
};
105+
36106
/**
37107
* Fetches the profileDiff data of the provided profileDiff Id
38108
* @param profileDiffId profileDiffId of the diffs need to be fetched
@@ -41,8 +111,7 @@ const fetchProfileDiffs = async () => {
41111
const fetchProfileDiff = async (profileDiffId) => {
42112
try {
43113
const profileDiff = await profileDiffsModel.doc(profileDiffId).get();
44-
const profileDiffData = profileDiff.data();
45-
return profileDiffData;
114+
return { id: profileDiff.id, profileDiffExists: profileDiff.exists, ...profileDiff.data() };
46115
} catch (err) {
47116
logger.error("Error retrieving profile Diff", err);
48117
throw err;
@@ -96,4 +165,5 @@ module.exports = {
96165
fetchProfileDiff,
97166
add,
98167
updateProfileDiff,
168+
fetchProfileDiffsWithPagination,
99169
};

routes/profileDiffs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ const authenticate = require("../middlewares/authenticate");
66
const { SUPERUSER } = require("../constants/roles");
77

88
router.get("/", authenticate, authorizeRoles([SUPERUSER]), profileDiffs.getProfileDiffs);
9+
router.get("/:id", authenticate, authorizeRoles([SUPERUSER]), profileDiffs.getProfileDiff);
910

1011
module.exports = router;

test/fixtures/profileDiffs/profileDiffs.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,90 @@ module.exports = () => {
1717
website: "",
1818
message: "",
1919
},
20+
{
21+
approval: "APPROVED",
22+
timestamp: Date.now() - 86400000, // 1 day ago
23+
first_name: "Jane",
24+
last_name: "Doe",
25+
26+
phone: "9876543210",
27+
yoe: "5",
28+
company: "TechCorp",
29+
designation: "Senior Developer",
30+
github_id: "janedoe",
31+
linkedin_id: "jane-doe-dev",
32+
twitter_id: "janedoedev",
33+
instagram_id: "jane.codes",
34+
website: "https://janedoe.dev",
35+
message: "Updated my work experience and social media profiles.",
36+
},
37+
{
38+
approval: "NOT APPROVED",
39+
timestamp: Date.now() - 172800000, // 2 days ago
40+
first_name: "John",
41+
last_name: "Smith",
42+
43+
phone: "5551234567",
44+
yoe: "3",
45+
company: "StartupX",
46+
designation: "Full Stack Engineer",
47+
github_id: "johnsmith",
48+
linkedin_id: "john-smith-dev",
49+
twitter_id: "johnsmithcodes",
50+
instagram_id: "",
51+
website: "https://johnsmith.io",
52+
message: "Added new skills and updated job title.",
53+
},
54+
{
55+
approval: "PENDING",
56+
timestamp: Date.now() - 259200000, // 3 days ago
57+
first_name: "Alice",
58+
last_name: "Johnson",
59+
60+
phone: "1112223333",
61+
yoe: "7",
62+
company: "BigTech Inc.",
63+
designation: "Lead Data Scientist",
64+
github_id: "alicej",
65+
linkedin_id: "alice-johnson-data",
66+
twitter_id: "alicejdata",
67+
instagram_id: "alice.codes.data",
68+
website: "https://alicejohnson.ai",
69+
message: "Updated my profile with recent Machine Learning certifications.",
70+
},
71+
{
72+
approval: "APPROVED",
73+
timestamp: Date.now() - 345600000, // 4 days ago
74+
first_name: "Bob",
75+
last_name: "Williams",
76+
77+
phone: "4445556666",
78+
yoe: "2",
79+
company: "CodeNinja",
80+
designation: "Junior Developer",
81+
github_id: "bobwilliams",
82+
linkedin_id: "bob-williams-dev",
83+
twitter_id: "bobcodes",
84+
instagram_id: "",
85+
website: "",
86+
message: "First time updating my profile!",
87+
},
88+
{
89+
approval: "PENDING",
90+
timestamp: Date.now() - 432000000, // 5 days ago
91+
first_name: "Emma",
92+
last_name: "Brown",
93+
94+
phone: "7778889999",
95+
yoe: "4",
96+
company: "WebWizards LLC",
97+
designation: "UX Designer",
98+
github_id: "emmab",
99+
linkedin_id: "emma-brown-ux",
100+
twitter_id: "emmauxdesign",
101+
instagram_id: "emma.designs",
102+
website: "https://emmabrown.design",
103+
message: "Updated portfolio and added recent UX projects.",
104+
},
20105
];
21106
};

0 commit comments

Comments
 (0)