Skip to content

Commit

Permalink
backend/activity-feed: refactor code and add test cases (#1982)
Browse files Browse the repository at this point in the history
  • Loading branch information
palakgupta2712 authored Mar 25, 2024
1 parent a9b5f3e commit 3bca9c4
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 51 deletions.
1 change: 1 addition & 0 deletions constants/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const logType = {
EXTERNAL_SERVICE: "EXTERNAL_SERVICE",
EXTENSION_REQUESTS: "extensionRequests",
TASK: "task",
TASK_REQUESTS: "taskRequests",
...REQUEST_LOG_TYPE,
};

Expand Down
24 changes: 18 additions & 6 deletions models/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ const admin = require("firebase-admin");
const { logType, ERROR_WHILE_FETCHING_LOGS } = require("../constants/logs");
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
const { getFullName } = require("../utils/users");
const { getUsersListFromLogs, formatLogsForFeed, mapify, convertTimestamp } = require("../utils/logs");
const {
getUsersListFromLogs,
formatLogsForFeed,
mapify,
convertTimestamp,
getTasksFromLogs,
} = require("../utils/logs");
const SIZE = 25;

/**
Expand Down Expand Up @@ -205,16 +211,22 @@ const fetchAllLogs = async (query) => {
allLogs.push({ ...doc.data() });
});
}
const userList = await getUsersListFromLogs(allLogs);
const usersMap = mapify(userList, "id");

if (allLogs.length === 0) {
return [];
return {
allLogs: [],
prev: null,
next: null,
page: page ? page + 1 : null,
};
}
if (format === "feed") {
let logsData = [];
const userList = await getUsersListFromLogs(allLogs);
const taskIdList = await getTasksFromLogs(allLogs);
const usersMap = mapify(userList, "id");
const tasksMap = mapify(taskIdList, "id");
logsData = allLogs.map((data) => {
const formattedLogs = formatLogsForFeed(data, usersMap);
const formattedLogs = formatLogsForFeed(data, usersMap, tasksMap);
if (!Object.keys(formattedLogs).length) return null;
return { ...formattedLogs, type: data.type, timestamp: convertTimestamp(data.timestamp) };
});
Expand Down
8 changes: 5 additions & 3 deletions test/integration/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ describe("/logs", function () {
expect(res.body.message).to.equal("All Logs fetched successfully");
expect(res.body.data).to.lengthOf(7);
expect(res.body.data[0]).to.contain({
user: "joygupta",
username: "joygupta",
taskTitle: "Untitled Task",
taskId: "mZB0akqPUa1GQQdrgsx7",
extensionRequestId: "y79PXir0s82qNAzeIn8S",
status: "PENDING",
Expand Down Expand Up @@ -170,7 +171,7 @@ describe("/logs", function () {
});
});

it("should return 204, if no logs are present", function (done) {
it("if no logs are present, should return valid response", function (done) {
chai
.request(app)
.get("/logs?type=REQUEST_CREATED1&dev=true")
Expand All @@ -179,7 +180,8 @@ describe("/logs", function () {
if (err) {
return done(err);
}
expect(res).to.have.status(204);
expect(res).to.have.status(200);
expect(res.body.data).to.have.lengthOf(0);
return done();
});
});
Expand Down
8 changes: 7 additions & 1 deletion test/unit/models/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const cookieName = config.get("userToken.cookieName");
const authService = require("../../../services/authService");
const { extensionRequestLogs } = require("../../fixtures/logs/extensionRequests");
const { LOGS_FETCHED_SUCCESSFULLY } = require("../../../constants/logs");
const tasks = require("../../../models/tasks");
const tasksData = require("../../fixtures/tasks/tasks")();
chai.use(chaiHttp);
const superUser = userData[4];
const userToBeMadeMember = userData[1];
Expand Down Expand Up @@ -133,6 +135,10 @@ describe("Logs", function () {
describe("GET /logs", function () {
before(async function () {
await addLogs();
const tasksPromise = tasksData.map(async (task) => {
await tasks.updateTask(task);
});
await Promise.all(tasksPromise);
});

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

it("should throw an error and log it", async function () {
Expand Down
218 changes: 218 additions & 0 deletions test/unit/utils/logs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
const { expect } = require("chai");
const { formatLogsForFeed, mapify } = require("../../../utils/logs");

describe("logs utils", function () {
describe("formatLogsForFeed", function () {
const usersMap = {
user1: { username: "palak-gupta" },
user2: { username: "mock-2" },
};

const tasksMap = {
task1: { title: "Link details page to status site" },
task2: { title: "Introduce /ooo command on discord" },
};

it("should format logs for OOO type", function () {
const logsSnapshot = {
meta: {
createdAt: 1710181066410,
createdBy: "user1",
requestId: "request123",
action: "create",
},
type: "REQUEST_CREATED",
body: {
createdAt: 1710181064968,
requestedBy: "user1",
from: 1710288000000,
until: 1710288050000,
id: "NOAWuKmazlIJHaN7Pihg",
state: "PENDING",
type: "OOO",
message: "For testing purpose",
updatedAt: 1710181064968,
},
timestamp: {
_seconds: 1710181066,
_nanoseconds: 410000000,
},
};

const formattedLog = formatLogsForFeed(logsSnapshot, usersMap);

expect(formattedLog).to.deep.equal({
user: "palak-gupta",
requestId: "request123",
from: 1710288000000,
until: 1710288050000,
message: "For testing purpose",
});
});

it("should format logs for extensionRequests type", function () {
const logsSnapshot = {
meta: {
extensionRequestId: "po1gNOCXUP2IFsChcmn8",
userId: "user2",
taskId: "task1",
username: "techlord",
},
type: "extensionRequests",
body: {
status: "APPROVED",
},
timestamp: {
_seconds: 1709316797,
_nanoseconds: 616000000,
},
};

const formattedLog = formatLogsForFeed(logsSnapshot, usersMap, tasksMap);

expect(formattedLog).to.deep.equal({
extensionRequestId: "po1gNOCXUP2IFsChcmn8",
status: "APPROVED",
taskId: "task1",
taskTitle: "Link details page to status site",
type: "extensionRequests",
user: "mock-2",
userId: "user2",
username: "techlord",
});
});

it("should return empty object when usersMap does not contain requestedBy user", function () {
const invalidLogsSnapshot = {
meta: { requestId: "request123", taskId: "task456" },
body: {
type: "OOO",
requestedBy: 3, // User not in usersMap
from: "2024-03-24",
until: "2024-03-25",
message: "Out of office",
extensionRequestId: "extension123",
},
};

const formattedLog = formatLogsForFeed(invalidLogsSnapshot, usersMap);

expect(formattedLog).to.deep.equal({});
});

it("should format logs for task type", function () {
const logsSnapshot = {
meta: {
userId: "user1",
taskId: "task2",
username: "shubham-sharma",
},
type: "task",
body: {
new: {
percentCompleted: 40,
},
subType: "update",
},
timestamp: {
_seconds: 1711273137,
_nanoseconds: 96000000,
},
};

const formattedLog = formatLogsForFeed(logsSnapshot, usersMap, tasksMap);

expect(formattedLog).to.deep.equal({
percentCompleted: 40,
subType: "update",
taskId: "task2",
taskTitle: "Introduce /ooo command on discord",
type: "task",
user: "shubham-sharma",
userId: "user1",
username: "shubham-sharma",
});
});

it("should format logs for PROFILE_DIFF_REJECTED type", function () {
const logsSnapshot = {
meta: {
rejectedBy: "user2",
userId: "user1",
},
type: "PROFILE_DIFF_REJECTED",
body: {
profileDiffId: "F8e0II1X7qZwzA1CbF0l",
message: "",
},
timestamp: {
_seconds: 1708098695,
_nanoseconds: 709000000,
},
};

const formattedLog = formatLogsForFeed(logsSnapshot, usersMap);

expect(formattedLog).to.deep.equal({
user: "palak-gupta",
rejectedBy: "mock-2",
message: "",
});
});

it("should format logs for PROFILE_DIFF_APPROVED type", function () {
const logsSnapshot = {
meta: {
approvedBy: "user1",
userId: "user2",
},
type: "PROFILE_DIFF_APPROVED",
body: {
profileDiffId: "7sPvm4ooC1PyC91A5KVS",
message: "",
},
timestamp: {
_seconds: 1707253607,
_nanoseconds: 697000000,
},
};

const formattedLog = formatLogsForFeed(logsSnapshot, usersMap);

expect(formattedLog).to.deep.equal({
approvedBy: "palak-gupta",
user: "mock-2",
message: "",
});
});
});

describe("mapify function", function () {
const data = [
{ username: "palak", id: "100", task: "task2" },
{ username: "mock-user-1", id: "101", task: "task23" },
{ username: "mock-user-2", id: "102", task: "task4" },
];

it("mapify data based on username", function () {
const mapifiedData = mapify(data, "username");
expect(mapifiedData).to.deep.equal({
"mock-user-1": {
id: "101",
username: "mock-user-1",
task: "task23",
},
"mock-user-2": {
id: "102",
username: "mock-user-2",
task: "task4",
},
palak: {
id: "100",
username: "palak",
task: "task2",
},
});
});
});
});
Loading

0 comments on commit 3bca9c4

Please sign in to comment.