Skip to content

Commit ad23dcb

Browse files
feat: added pagination for lazy loading in the /groups route to load the discord groups asynchrounously instead of all at once
1 parent 70cfd78 commit ad23dcb

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

controllers/discordactions.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,20 @@ const deleteGroupRole = async (req, res) => {
124124
* @param res {Object} - Express response object
125125
*/
126126

127-
const getAllGroupRoles = async (req, res) => {
127+
const getPaginatedGroupRoles = async (req, res) => {
128128
try {
129+
const isDevMode = req.query?.dev === "true";
130+
if (isDevMode) {
131+
const latestDoc = req.query?.latestDoc;
132+
const { groups, newLatestDoc } = await discordRolesModel.getPaginatedGroupRoles(latestDoc);
133+
const discordId = req.userData?.discordId;
134+
const groupsWithMembershipInfo = await discordRolesModel.enrichGroupDataWithMembershipInfo(discordId, groups);
135+
return res.json({
136+
message: "Roles fetched successfully!",
137+
newLatestDoc: newLatestDoc,
138+
groups: groupsWithMembershipInfo,
139+
});
140+
}
129141
const { groups } = await discordRolesModel.getAllGroupRoles();
130142
const discordId = req.userData?.discordId;
131143
const groupsWithMembershipInfo = await discordRolesModel.enrichGroupDataWithMembershipInfo(discordId, groups);
@@ -431,7 +443,7 @@ const syncDiscordGroupRolesInFirestore = async (req, res) => {
431443
});
432444
await Promise.all(batch);
433445

434-
const allRolesInFirestore = await discordRolesModel.getAllGroupRoles();
446+
const allRolesInFirestore = await discordRolesModel.getPaginatedGroupRoles();
435447

436448
return res.json({
437449
response: allRolesInFirestore.groups,
@@ -534,7 +546,7 @@ const getUserDiscordInvite = async (req, res) => {
534546
module.exports = {
535547
getGroupsRoleId,
536548
createGroupRole,
537-
getAllGroupRoles,
549+
getPaginatedGroupRoles,
538550
addGroupRoleToMember,
539551
deleteRole,
540552
updateDiscordImageForVerification,

models/discordactions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const deleteRoleFromDatabase = async (roleId, discordId) => {
107107
* @param roleData { Object }: Data of the new role
108108
* @returns {Promise<discordRoleModel|Object>}
109109
*/
110+
110111
const getAllGroupRoles = async () => {
111112
try {
112113
const data = await discordRoleModel.get();
@@ -125,6 +126,28 @@ const getAllGroupRoles = async () => {
125126
}
126127
};
127128

129+
const getPaginatedGroupRoles = async (latestDoc) => {
130+
try {
131+
const data = await discordRoleModel
132+
.orderBy("roleid")
133+
.startAfter(latestDoc || 0)
134+
.limit(18)
135+
.get();
136+
const groups = [];
137+
data.forEach((doc) => {
138+
const group = {
139+
id: doc.id,
140+
...doc.data(),
141+
};
142+
groups.push(group);
143+
});
144+
return { groups, newLatestDoc: data.docs[data.docs.length - 1]?.data().roleid };
145+
} catch (err) {
146+
logger.error("Error in getting all group-roles", err);
147+
throw err;
148+
}
149+
};
150+
128151
/**
129152
*
130153
* @param groupRoleName String : name of the role
@@ -1086,6 +1109,7 @@ module.exports = {
10861109
removeMemberGroup,
10871110
getGroupRolesForUser,
10881111
getAllGroupRoles,
1112+
getPaginatedGroupRoles,
10891113
getGroupRoleByName,
10901114
updateGroupRole,
10911115
addGroupRoleToMember,

routes/discordactions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const authenticate = require("../middlewares/authenticate");
33
const {
44
createGroupRole,
55
getGroupsRoleId,
6-
getAllGroupRoles,
6+
getPaginatedGroupRoles,
77
addGroupRoleToMember,
88
deleteRole,
99
updateDiscordImageForVerification,
@@ -33,7 +33,7 @@ const { authorizeAndAuthenticate } = require("../middlewares/authorizeUsersAndSe
3333
const router = express.Router();
3434

3535
router.post("/groups", authenticate, checkIsVerifiedDiscord, validateGroupRoleBody, createGroupRole);
36-
router.get("/groups", authenticate, checkIsVerifiedDiscord, getAllGroupRoles);
36+
router.get("/groups", authenticate, checkIsVerifiedDiscord, getPaginatedGroupRoles);
3737
router.delete("/groups/:groupId", authenticate, checkIsVerifiedDiscord, authorizeRoles([SUPERUSER]), deleteGroupRole);
3838
router.post("/roles", authenticate, checkIsVerifiedDiscord, validateMemberRoleBody, addGroupRoleToMember);
3939
router.get("/invite", authenticate, getUserDiscordInvite);

test/unit/models/discordactions.test.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const tasksModel = firestore.collection("tasks");
1919

2020
const {
2121
createNewRole,
22-
getAllGroupRoles,
22+
getPaginatedGroupRoles,
2323
isGroupRoleExists,
2424
addGroupRoleToMember,
2525
deleteRoleFromDatabase,
@@ -93,27 +93,42 @@ describe("discordactions", function () {
9393
});
9494
});
9595

96-
describe("getAllGroupRoles", function () {
97-
let getStub;
96+
describe("getPaginatedGroupRoles", function () {
97+
let orderByStub, startAfterStub, limitStub, getStub;
9898

9999
beforeEach(function () {
100-
getStub = sinon.stub(discordRoleModel, "get").resolves({
101-
forEach: (callback) => groupData.forEach(callback),
100+
orderByStub = sinon.stub();
101+
startAfterStub = sinon.stub();
102+
limitStub = sinon.stub();
103+
getStub = sinon.stub();
104+
105+
orderByStub.returns({ startAfter: startAfterStub });
106+
startAfterStub.returns({ limit: limitStub });
107+
limitStub.returns({ get: getStub });
108+
getStub.resolves({
109+
docs: groupData.map((group) => ({
110+
id: group.id,
111+
data: () => group,
112+
})),
102113
});
114+
115+
sinon.stub(discordRoleModel, "orderBy").returns(orderByStub);
103116
});
104117

105118
afterEach(function () {
106-
getStub.restore();
119+
sinon.restore();
107120
});
108121

109-
it("should return all group-roles from the database", async function () {
110-
const result = await getAllGroupRoles();
111-
expect(result.groups).to.be.an("array");
122+
it("should return paginated group-roles from the database", async function () {
123+
const result = await getPaginatedGroupRoles();
124+
expect(result).to.have.property("groups").that.is.an("array");
125+
expect(result).to.have.property("newLatestDoc");
126+
expect(result.groups.length).to.be.at.most(18);
112127
});
113128

114129
it("should throw an error if getting group-roles fails", async function () {
115130
getStub.rejects(new Error("Database error"));
116-
return getAllGroupRoles().catch((err) => {
131+
return getPaginatedGroupRoles().catch((err) => {
117132
expect(err).to.be.an.instanceOf(Error);
118133
expect(err.message).to.equal("Database error");
119134
});

0 commit comments

Comments
 (0)