Skip to content

Commit 3bca9c4

Browse files
backend/activity-feed: refactor code and add test cases (#1982)
1 parent a9b5f3e commit 3bca9c4

File tree

6 files changed

+336
-51
lines changed

6 files changed

+336
-51
lines changed

constants/logs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const logType = {
1212
EXTERNAL_SERVICE: "EXTERNAL_SERVICE",
1313
EXTENSION_REQUESTS: "extensionRequests",
1414
TASK: "task",
15+
TASK_REQUESTS: "taskRequests",
1516
...REQUEST_LOG_TYPE,
1617
};
1718

models/logs.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ const admin = require("firebase-admin");
55
const { logType, ERROR_WHILE_FETCHING_LOGS } = require("../constants/logs");
66
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
77
const { getFullName } = require("../utils/users");
8-
const { getUsersListFromLogs, formatLogsForFeed, mapify, convertTimestamp } = require("../utils/logs");
8+
const {
9+
getUsersListFromLogs,
10+
formatLogsForFeed,
11+
mapify,
12+
convertTimestamp,
13+
getTasksFromLogs,
14+
} = require("../utils/logs");
915
const SIZE = 25;
1016

1117
/**
@@ -205,16 +211,22 @@ const fetchAllLogs = async (query) => {
205211
allLogs.push({ ...doc.data() });
206212
});
207213
}
208-
const userList = await getUsersListFromLogs(allLogs);
209-
const usersMap = mapify(userList, "id");
210-
211214
if (allLogs.length === 0) {
212-
return [];
215+
return {
216+
allLogs: [],
217+
prev: null,
218+
next: null,
219+
page: page ? page + 1 : null,
220+
};
213221
}
214222
if (format === "feed") {
215223
let logsData = [];
224+
const userList = await getUsersListFromLogs(allLogs);
225+
const taskIdList = await getTasksFromLogs(allLogs);
226+
const usersMap = mapify(userList, "id");
227+
const tasksMap = mapify(taskIdList, "id");
216228
logsData = allLogs.map((data) => {
217-
const formattedLogs = formatLogsForFeed(data, usersMap);
229+
const formattedLogs = formatLogsForFeed(data, usersMap, tasksMap);
218230
if (!Object.keys(formattedLogs).length) return null;
219231
return { ...formattedLogs, type: data.type, timestamp: convertTimestamp(data.timestamp) };
220232
});

test/integration/logs.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ describe("/logs", function () {
142142
expect(res.body.message).to.equal("All Logs fetched successfully");
143143
expect(res.body.data).to.lengthOf(7);
144144
expect(res.body.data[0]).to.contain({
145-
user: "joygupta",
145+
username: "joygupta",
146+
taskTitle: "Untitled Task",
146147
taskId: "mZB0akqPUa1GQQdrgsx7",
147148
extensionRequestId: "y79PXir0s82qNAzeIn8S",
148149
status: "PENDING",
@@ -170,7 +171,7 @@ describe("/logs", function () {
170171
});
171172
});
172173

173-
it("should return 204, if no logs are present", function (done) {
174+
it("if no logs are present, should return valid response", function (done) {
174175
chai
175176
.request(app)
176177
.get("/logs?type=REQUEST_CREATED1&dev=true")
@@ -179,7 +180,8 @@ describe("/logs", function () {
179180
if (err) {
180181
return done(err);
181182
}
182-
expect(res).to.have.status(204);
183+
expect(res).to.have.status(200);
184+
expect(res.body.data).to.have.lengthOf(0);
183185
return done();
184186
});
185187
});

test/unit/models/logs.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const cookieName = config.get("userToken.cookieName");
1515
const authService = require("../../../services/authService");
1616
const { extensionRequestLogs } = require("../../fixtures/logs/extensionRequests");
1717
const { LOGS_FETCHED_SUCCESSFULLY } = require("../../../constants/logs");
18+
const tasks = require("../../../models/tasks");
19+
const tasksData = require("../../fixtures/tasks/tasks")();
1820
chai.use(chaiHttp);
1921
const superUser = userData[4];
2022
const userToBeMadeMember = userData[1];
@@ -133,6 +135,10 @@ describe("Logs", function () {
133135
describe("GET /logs", function () {
134136
before(async function () {
135137
await addLogs();
138+
const tasksPromise = tasksData.map(async (task) => {
139+
await tasks.updateTask(task);
140+
});
141+
await Promise.all(tasksPromise);
136142
});
137143

138144
after(async function () {
@@ -195,7 +201,7 @@ describe("Logs", function () {
195201
it("Should return null if no logs are presnet the logs for specific types", async function () {
196202
await cleanDb();
197203
const result = await logsQuery.fetchAllLogs({});
198-
expect(result).to.lengthOf(0);
204+
expect(result.allLogs).to.lengthOf(0);
199205
});
200206

201207
it("should throw an error and log it", async function () {

test/unit/utils/logs.test.js

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
const { expect } = require("chai");
2+
const { formatLogsForFeed, mapify } = require("../../../utils/logs");
3+
4+
describe("logs utils", function () {
5+
describe("formatLogsForFeed", function () {
6+
const usersMap = {
7+
user1: { username: "palak-gupta" },
8+
user2: { username: "mock-2" },
9+
};
10+
11+
const tasksMap = {
12+
task1: { title: "Link details page to status site" },
13+
task2: { title: "Introduce /ooo command on discord" },
14+
};
15+
16+
it("should format logs for OOO type", function () {
17+
const logsSnapshot = {
18+
meta: {
19+
createdAt: 1710181066410,
20+
createdBy: "user1",
21+
requestId: "request123",
22+
action: "create",
23+
},
24+
type: "REQUEST_CREATED",
25+
body: {
26+
createdAt: 1710181064968,
27+
requestedBy: "user1",
28+
from: 1710288000000,
29+
until: 1710288050000,
30+
id: "NOAWuKmazlIJHaN7Pihg",
31+
state: "PENDING",
32+
type: "OOO",
33+
message: "For testing purpose",
34+
updatedAt: 1710181064968,
35+
},
36+
timestamp: {
37+
_seconds: 1710181066,
38+
_nanoseconds: 410000000,
39+
},
40+
};
41+
42+
const formattedLog = formatLogsForFeed(logsSnapshot, usersMap);
43+
44+
expect(formattedLog).to.deep.equal({
45+
user: "palak-gupta",
46+
requestId: "request123",
47+
from: 1710288000000,
48+
until: 1710288050000,
49+
message: "For testing purpose",
50+
});
51+
});
52+
53+
it("should format logs for extensionRequests type", function () {
54+
const logsSnapshot = {
55+
meta: {
56+
extensionRequestId: "po1gNOCXUP2IFsChcmn8",
57+
userId: "user2",
58+
taskId: "task1",
59+
username: "techlord",
60+
},
61+
type: "extensionRequests",
62+
body: {
63+
status: "APPROVED",
64+
},
65+
timestamp: {
66+
_seconds: 1709316797,
67+
_nanoseconds: 616000000,
68+
},
69+
};
70+
71+
const formattedLog = formatLogsForFeed(logsSnapshot, usersMap, tasksMap);
72+
73+
expect(formattedLog).to.deep.equal({
74+
extensionRequestId: "po1gNOCXUP2IFsChcmn8",
75+
status: "APPROVED",
76+
taskId: "task1",
77+
taskTitle: "Link details page to status site",
78+
type: "extensionRequests",
79+
user: "mock-2",
80+
userId: "user2",
81+
username: "techlord",
82+
});
83+
});
84+
85+
it("should return empty object when usersMap does not contain requestedBy user", function () {
86+
const invalidLogsSnapshot = {
87+
meta: { requestId: "request123", taskId: "task456" },
88+
body: {
89+
type: "OOO",
90+
requestedBy: 3, // User not in usersMap
91+
from: "2024-03-24",
92+
until: "2024-03-25",
93+
message: "Out of office",
94+
extensionRequestId: "extension123",
95+
},
96+
};
97+
98+
const formattedLog = formatLogsForFeed(invalidLogsSnapshot, usersMap);
99+
100+
expect(formattedLog).to.deep.equal({});
101+
});
102+
103+
it("should format logs for task type", function () {
104+
const logsSnapshot = {
105+
meta: {
106+
userId: "user1",
107+
taskId: "task2",
108+
username: "shubham-sharma",
109+
},
110+
type: "task",
111+
body: {
112+
new: {
113+
percentCompleted: 40,
114+
},
115+
subType: "update",
116+
},
117+
timestamp: {
118+
_seconds: 1711273137,
119+
_nanoseconds: 96000000,
120+
},
121+
};
122+
123+
const formattedLog = formatLogsForFeed(logsSnapshot, usersMap, tasksMap);
124+
125+
expect(formattedLog).to.deep.equal({
126+
percentCompleted: 40,
127+
subType: "update",
128+
taskId: "task2",
129+
taskTitle: "Introduce /ooo command on discord",
130+
type: "task",
131+
user: "shubham-sharma",
132+
userId: "user1",
133+
username: "shubham-sharma",
134+
});
135+
});
136+
137+
it("should format logs for PROFILE_DIFF_REJECTED type", function () {
138+
const logsSnapshot = {
139+
meta: {
140+
rejectedBy: "user2",
141+
userId: "user1",
142+
},
143+
type: "PROFILE_DIFF_REJECTED",
144+
body: {
145+
profileDiffId: "F8e0II1X7qZwzA1CbF0l",
146+
message: "",
147+
},
148+
timestamp: {
149+
_seconds: 1708098695,
150+
_nanoseconds: 709000000,
151+
},
152+
};
153+
154+
const formattedLog = formatLogsForFeed(logsSnapshot, usersMap);
155+
156+
expect(formattedLog).to.deep.equal({
157+
user: "palak-gupta",
158+
rejectedBy: "mock-2",
159+
message: "",
160+
});
161+
});
162+
163+
it("should format logs for PROFILE_DIFF_APPROVED type", function () {
164+
const logsSnapshot = {
165+
meta: {
166+
approvedBy: "user1",
167+
userId: "user2",
168+
},
169+
type: "PROFILE_DIFF_APPROVED",
170+
body: {
171+
profileDiffId: "7sPvm4ooC1PyC91A5KVS",
172+
message: "",
173+
},
174+
timestamp: {
175+
_seconds: 1707253607,
176+
_nanoseconds: 697000000,
177+
},
178+
};
179+
180+
const formattedLog = formatLogsForFeed(logsSnapshot, usersMap);
181+
182+
expect(formattedLog).to.deep.equal({
183+
approvedBy: "palak-gupta",
184+
user: "mock-2",
185+
message: "",
186+
});
187+
});
188+
});
189+
190+
describe("mapify function", function () {
191+
const data = [
192+
{ username: "palak", id: "100", task: "task2" },
193+
{ username: "mock-user-1", id: "101", task: "task23" },
194+
{ username: "mock-user-2", id: "102", task: "task4" },
195+
];
196+
197+
it("mapify data based on username", function () {
198+
const mapifiedData = mapify(data, "username");
199+
expect(mapifiedData).to.deep.equal({
200+
"mock-user-1": {
201+
id: "101",
202+
username: "mock-user-1",
203+
task: "task23",
204+
},
205+
"mock-user-2": {
206+
id: "102",
207+
username: "mock-user-2",
208+
task: "task4",
209+
},
210+
palak: {
211+
id: "100",
212+
username: "palak",
213+
task: "task2",
214+
},
215+
});
216+
});
217+
});
218+
});

0 commit comments

Comments
 (0)