Skip to content

Commit

Permalink
Merge pull request #2179 from Real-Dev-Squad/feat/paginationInProfile…
Browse files Browse the repository at this point in the history
…DiffsAPI

Added Pagination in profile diffs API
  • Loading branch information
lakshayman authored Sep 28, 2024
2 parents eeafaaa + 7be7be2 commit 29122bb
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 7 deletions.
45 changes: 40 additions & 5 deletions controllers/profileDiffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,53 @@ const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");

const getProfileDiffs = async (req, res) => {
try {
const pendingProfileDiffs = await profileDiffsQuery.fetchProfileDiffs();
if (!req.query.dev) {
const pendingProfileDiffs = await profileDiffsQuery.fetchProfileDiffs();

return res.json({
message: "Profile Diffs returned successfully!",
profileDiffs: pendingProfileDiffs,
});
return res.json({
message: "Profile Diffs returned successfully!",
profileDiffs: pendingProfileDiffs,
});
} else {
const { status = "PENDING", order = "desc", size = 10, username = "", cursor = null } = req.query;
const { profileDiffs, next } = await profileDiffsQuery.fetchProfileDiffsWithPagination(
status,
order,
parseInt(size),
username,
cursor
);

return res.json({
message: "Profile Diffs returned successfully!",
profileDiffs,
next,
});
}
} catch (error) {
logger.error(`Error while fetching profile diffs: ${error}`);
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
}
};

const getProfileDiff = async (req, res) => {
try {
const result = await profileDiffsQuery.fetchProfileDiff(req.params.id);
if (result.profileDiffExists) {
return res.json({
message: "Profile Diff returned successfully!",
profileDiff: result,
});
}

return res.boom.notFound("Profile Diff doesn't exist");
} catch (error) {
logger.error(`Error while fetching Profile Diff: ${error}`);
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
}
};

module.exports = {
getProfileDiffs,
getProfileDiff,
};
74 changes: 72 additions & 2 deletions models/profileDiffs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { profileStatus } = require("../constants/users");
const firestore = require("../utils/firestore");
const userModel = firestore.collection("users");
const profileDiffsModel = firestore.collection("profileDiffs");
const obfuscate = require("../utils/obfuscate");
const { generateNextLink } = require("../utils/profileDiffs");

/**
* Add profileDiff
Expand Down Expand Up @@ -33,6 +35,74 @@ const fetchProfileDiffs = async () => {
}
};

const fetchProfileDiffsWithPagination = async (status, order, size, username, cursor) => {
try {
let query = profileDiffsModel.where("approval", "==", status);

if (username) {
const userSnapshot = await userModel
.where("username", ">=", username)
.where("username", "<=", username + "\uf8ff")
.get();
const userIds = userSnapshot.docs.map((doc) => doc.id);
if (userIds.length === 0) return { profileDiffs: [], next: "" };
query = query.where("userId", "in", userIds);
}

query = query.orderBy("timestamp", order);

if (cursor) {
const cursorSnapshot = await profileDiffsModel.doc(cursor).get();
query = query.startAfter(cursorSnapshot);
}

const snapshot = await query.limit(size).get();

const profileDiffs = [];
snapshot.forEach((doc) => {
const data = doc.data();
let emailRedacted = "";
let phoneRedacted = "";
if (data.email) {
emailRedacted = obfuscate.obfuscateMail(data.email);
}
if (data.phone) {
phoneRedacted = obfuscate.obfuscatePhone(data.phone);
}

profileDiffs.push({
id: doc.id,
...data,
email: emailRedacted,
phone: phoneRedacted,
});
});

const resultDataLength = profileDiffs.length;
const isNextLinkRequired = size && resultDataLength === size;
const lastVisible = isNextLinkRequired && profileDiffs[resultDataLength - 1];

const nextPageParams = {
dev: true,
status,
order,
size,
username,
cursor: lastVisible?.id,
};

let nextLink = "";
if (lastVisible) {
nextLink = generateNextLink(nextPageParams);
}

return { profileDiffs, next: nextLink };
} 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
Expand All @@ -41,8 +111,7 @@ const fetchProfileDiffs = async () => {
const fetchProfileDiff = async (profileDiffId) => {
try {
const profileDiff = await profileDiffsModel.doc(profileDiffId).get();
const profileDiffData = profileDiff.data();
return profileDiffData;
return { id: profileDiff.id, profileDiffExists: profileDiff.exists, ...profileDiff.data() };
} catch (err) {
logger.error("Error retrieving profile Diff", err);
throw err;
Expand Down Expand Up @@ -96,4 +165,5 @@ module.exports = {
fetchProfileDiff,
add,
updateProfileDiff,
fetchProfileDiffsWithPagination,
};
1 change: 1 addition & 0 deletions routes/profileDiffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const authenticate = require("../middlewares/authenticate");
const { SUPERUSER } = require("../constants/roles");

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

module.exports = router;
85 changes: 85 additions & 0 deletions test/fixtures/profileDiffs/profileDiffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,90 @@ module.exports = () => {
website: "",
message: "",
},
{
approval: "APPROVED",
timestamp: Date.now() - 86400000, // 1 day ago
first_name: "Jane",
last_name: "Doe",
email: "[email protected]",
phone: "9876543210",
yoe: "5",
company: "TechCorp",
designation: "Senior Developer",
github_id: "janedoe",
linkedin_id: "jane-doe-dev",
twitter_id: "janedoedev",
instagram_id: "jane.codes",
website: "https://janedoe.dev",
message: "Updated my work experience and social media profiles.",
},
{
approval: "NOT APPROVED",
timestamp: Date.now() - 172800000, // 2 days ago
first_name: "John",
last_name: "Smith",
email: "[email protected]",
phone: "5551234567",
yoe: "3",
company: "StartupX",
designation: "Full Stack Engineer",
github_id: "johnsmith",
linkedin_id: "john-smith-dev",
twitter_id: "johnsmithcodes",
instagram_id: "",
website: "https://johnsmith.io",
message: "Added new skills and updated job title.",
},
{
approval: "PENDING",
timestamp: Date.now() - 259200000, // 3 days ago
first_name: "Alice",
last_name: "Johnson",
email: "[email protected]",
phone: "1112223333",
yoe: "7",
company: "BigTech Inc.",
designation: "Lead Data Scientist",
github_id: "alicej",
linkedin_id: "alice-johnson-data",
twitter_id: "alicejdata",
instagram_id: "alice.codes.data",
website: "https://alicejohnson.ai",
message: "Updated my profile with recent Machine Learning certifications.",
},
{
approval: "APPROVED",
timestamp: Date.now() - 345600000, // 4 days ago
first_name: "Bob",
last_name: "Williams",
email: "[email protected]",
phone: "4445556666",
yoe: "2",
company: "CodeNinja",
designation: "Junior Developer",
github_id: "bobwilliams",
linkedin_id: "bob-williams-dev",
twitter_id: "bobcodes",
instagram_id: "",
website: "",
message: "First time updating my profile!",
},
{
approval: "PENDING",
timestamp: Date.now() - 432000000, // 5 days ago
first_name: "Emma",
last_name: "Brown",
email: "[email protected]",
phone: "7778889999",
yoe: "4",
company: "WebWizards LLC",
designation: "UX Designer",
github_id: "emmab",
linkedin_id: "emma-brown-ux",
twitter_id: "emmauxdesign",
instagram_id: "emma.designs",
website: "https://emmabrown.design",
message: "Updated portfolio and added recent UX projects.",
},
];
};
Loading

0 comments on commit 29122bb

Please sign in to comment.