Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test cases for activity feed backend #1976

Merged
merged 17 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions constants/logs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const logType = {
import { REQUEST_LOG_TYPE } from "./requests";

export const logType = {
PROFILE_DIFF_APPROVED: "PROFILE_DIFF_APPROVED",
PROFILE_DIFF_REJECTED: "PROFILE_DIFF_REJECTED",
CLOUDFLARE_CACHE_PURGED: "CLOUDFLARE_CACHE_PURGED",
Expand All @@ -8,6 +10,11 @@ const logType = {
TASKS_MISSED_UPDATES_ERRORS: "TASKS_MISSED_UPDATES_ERRORS",
DISCORD_INVITES: "DISCORD_INVITES",
EXTERNAL_SERVICE: "EXTERNAL_SERVICE",
EXTENSION_REQUESTS: "extensionRequests",
TASK: "task",
...REQUEST_LOG_TYPE,
};

module.exports = { logType };
export const ALL_LOGS_FETCHED_SUCCESSFULLY = "All Logs fetched successfully";
export const LOGS_FETCHED_SUCCESSFULLY = "Logs fetched successfully";
export const ERROR_WHILE_FETCHING_LOGS = "Error while fetching logs";
60 changes: 58 additions & 2 deletions controllers/logs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getPaginatedLink } from "../utils/helper";
import { ALL_LOGS_FETCHED_SUCCESSFULLY, ERROR_WHILE_FETCHING_LOGS, LOGS_FETCHED_SUCCESSFULLY } from "../constants/logs";
const logsQuery = require("../models/logs");
const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");

Expand All @@ -11,15 +13,69 @@ const fetchLogs = async (req, res) => {
try {
const logs = await logsQuery.fetchLogs(req.query, req.params.type);
return res.json({
message: "Logs returned successfully!",
message: LOGS_FETCHED_SUCCESSFULLY,
logs,
});
} catch (error) {
logger.error(`Error while fetching logs: ${error}`);
logger.error(`${ERROR_WHILE_FETCHING_LOGS}: ${error}`);
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
}
};

const fetchAllLogs = async (req, res) => {
const { query } = req;
try {
if (query.dev !== "true") {
return res.boom.badRequest("Please use feature flag to make this request!");
}
const logs = await logsQuery.fetchAllLogs(query);
if (logs.length === 0) {
return res.status(204).send();
}
const { allLogs, next, prev, page } = logs;
if (page) {
const pageLink = `/logs?page=${page}&dev=${query.dev}`;
return res.status(200).json({
message: ALL_LOGS_FETCHED_SUCCESSFULLY,
data: allLogs,
page: pageLink,
});
}

let nextUrl = null;
let prevUrl = null;
if (next) {
const nextLink = getPaginatedLink({
endpoint: "/logs",
query,
cursorKey: "next",
docId: next,
});
nextUrl = nextLink;
}
if (prev) {
const prevLink = getPaginatedLink({
endpoint: "/logs",
query,
cursorKey: "prev",
docId: prev,
});
prevUrl = prevLink;
}

return res.status(200).json({
message: ALL_LOGS_FETCHED_SUCCESSFULLY,
data: allLogs,
next: nextUrl,
prev: prevUrl,
});
} catch (err) {
logger.error(ERROR_WHILE_FETCHING_LOGS, err);
return res.boom.badImplementation(ERROR_WHILE_FETCHING_LOGS);
}
};

module.exports = {
fetchLogs,
fetchAllLogs,
};
86 changes: 85 additions & 1 deletion models/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const firestore = require("../utils/firestore");
const { getBeforeHourTime } = require("../utils/time");
const logsModel = firestore.collection("logs");
const admin = require("firebase-admin");
const { logType } = require("../constants/logs");
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 SIZE = 25;

/**
* Adds log
Expand Down Expand Up @@ -155,9 +157,91 @@ const fetchLastAddedCacheLog = async (id) => {
}
};

const fetchAllLogs = async (query) => {
let { type, prev, next, page, size = SIZE, format } = query;
size = parseInt(size);
page = parseInt(page);

try {
let requestQuery = logsModel;

if (type) {
const logType = type.split(",");
if (logType.length >= 1) requestQuery = requestQuery.where("type", "in", logType);
}

requestQuery = requestQuery.orderBy("timestamp", "desc");
let requestQueryDoc = requestQuery;

if (prev) {
requestQueryDoc = requestQueryDoc.limitToLast(size);
} else {
requestQueryDoc = requestQueryDoc.limit(size);
}

if (page) {
const startAfter = (page - 1) * size;
requestQueryDoc = requestQueryDoc.offset(startAfter);
} else if (next) {
const doc = await logsModel.doc(next).get();
requestQueryDoc = requestQueryDoc.startAt(doc);
} else if (prev) {
const doc = await logsModel.doc(prev).get();
requestQueryDoc = requestQueryDoc.endAt(doc);
}

const snapshot = await requestQueryDoc.get();
let nextDoc, prevDoc;
if (!snapshot.empty) {
const first = snapshot.docs[0];
prevDoc = await requestQuery.endBefore(first).limitToLast(1).get();

const last = snapshot.docs[snapshot.docs.length - 1];
nextDoc = await requestQuery.startAfter(last).limit(1).get();
}
const allLogs = [];
if (!snapshot.empty) {
snapshot.forEach((doc) => {
allLogs.push({ ...doc.data() });
});
}
const userList = await getUsersListFromLogs(allLogs);
const usersMap = mapify(userList, "id");

if (allLogs.length === 0) {
return [];
}
if (format === "feed") {
let logsData = [];
logsData = allLogs.map((data) => {
const formattedLogs = formatLogsForFeed(data, usersMap);
if (!Object.keys(formattedLogs).length) return null;
return { ...formattedLogs, type: data.type, timestamp: convertTimestamp(data.timestamp) };
});
return {
allLogs: logsData.filter((log) => log),
prev: prevDoc.empty ? null : prevDoc.docs[0].id,
next: nextDoc.empty ? null : nextDoc.docs[0].id,
page: page ? page + 1 : null,
};
}

return {
allLogs: allLogs.filter((log) => log),
prev: prevDoc.empty ? null : prevDoc.docs[0].id,
next: nextDoc.empty ? null : nextDoc.docs[0].id,
page: page ? page + 1 : null,
};
} catch (error) {
logger.error(ERROR_WHILE_FETCHING_LOGS, error);
throw error;
}
};

module.exports = {
addLog,
fetchLogs,
fetchCacheLogs,
fetchLastAddedCacheLog,
fetchAllLogs,
};
1 change: 1 addition & 0 deletions routes/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const authorizeRoles = require("../middlewares/authorizeRoles");
const { SUPERUSER } = require("../constants/roles");

router.get("/:type", authenticate, authorizeRoles([SUPERUSER]), logs.fetchLogs);
router.get("/", authenticate, authorizeRoles([SUPERUSER]), logs.fetchAllLogs);

module.exports = router;
5 changes: 1 addition & 4 deletions test/fixtures/logs/archievedUsers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const archivedUserDetailsModal = [
export const archivedUserDetailsModal = [
{
type: "archived-details",
meta: {},
Expand Down Expand Up @@ -63,6 +63,3 @@ const archivedUserDetailsModal = [
},
},
];
module.exports = {
archivedUserDetailsModal,
};
26 changes: 17 additions & 9 deletions test/fixtures/logs/extensionRequests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const extensionRequestLogs = [
export const extensionRequestLogs = [
{
meta: {
extensionRequestId: "y79PXir0s82qNAzeIn8S",
Expand Down Expand Up @@ -33,10 +33,16 @@ const extensionRequestLogs = [
},
{
meta: {
oldETA: 1695832836,
newETA: 1695832936,
taskId: "mZB0akqPUa1GQQdrgsx7",
createdBy: "XBucw7nHW1wOxdWrmLVa",
username: "joygupta",
},
body: {
assignee: "XBucw7nHW1wOxdWrmLVa",
extensionRequestId: "y79PXir0s82qNAzeIn8S",
userId: "XBucw7nHW1wOxdWrmLVa",
oldEndsOn: 1707264000,
newEndsOn: 1706918400,
status: "PENDING",
},
type: "extensionRequests",
timestamp: {
Expand All @@ -46,10 +52,16 @@ const extensionRequestLogs = [
},
{
meta: {
taskId: "mZB0akqPUa1GQQdrgsx7",
createdBy: "XBucw7nHW1wOxdWrmLVa",
username: "joygupta",
},
body: {
assignee: "XBucw7nHW1wOxdWrmLVa",
oldTitle: "Hello World",
newTitle: "Hello JS",
extensionRequestId: "y79PXir0s82qNAzeIn8S",
userId: "XBucw7nHW1wOxdWrmLVa",
status: "PENDING",
},
type: "extensionRequests",
timestamp: {
Expand All @@ -58,7 +70,3 @@ const extensionRequestLogs = [
},
},
];

module.exports = {
extensionRequestLogs,
};
50 changes: 50 additions & 0 deletions test/fixtures/logs/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export const requestsLogs = [
{
meta: {
createdAt: 1709175284035,
createdBy: "iODXB6ns8jaZB9p0XlBw",
requestId: "CNExxpR1F4UPbtYIpxFP",
action: "create",
},
type: "REQUEST_CREATED",
body: {
createdAt: 1709175282967,
requestedBy: "iODXB6ns8jaZB9p0XlBw",
from: 1711676421000,
until: 1714354821000,
id: "CNExxpR1F4UPbtYIpxFP",
state: "PENDING",
type: "OOO",
message: "Out of office for personal reasons.",
updatedAt: 1709175282967,
},
timestamp: {
_seconds: 1709175284,
_nanoseconds: 35000000,
},
},
{
meta: {
createdAt: 1709170848580,
createdBy: "iODXB6ns8jaZB9p0XlBw",
requestId: "K7ioni8arDgRCBAWwYxp",
action: "create",
},
type: "REQUEST_CREATED",
body: {
createdAt: 1709170848318,
requestedBy: "iODXB6ns8jaZB9p0XlBw",
from: 1711676421000,
until: 1714354821000,
id: "K7ioni8arDgRCBAWwYxp",
state: "PENDING",
type: "OOO",
message: "Out of office for personal reasons.",
updatedAt: 1709170848318,
},
timestamp: {
_seconds: 1709170848,
_nanoseconds: 580000000,
},
},
];
56 changes: 56 additions & 0 deletions test/fixtures/logs/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export const taskLogs = [
{
meta: {
userId: "aaL1MXrpmnUNfLkhgXRj",
taskId: "P8ZZX2ef8heTs0erA6a8",
username: "shubham-sharma",
},
type: "task",
yesyash marked this conversation as resolved.
Show resolved Hide resolved
body: {
new: {
status: "NEEDS_REVIEW",
},
subType: "update",
},
timestamp: {
_seconds: 1710731591,
_nanoseconds: 323000000,
},
},
{
meta: {
userId: "aaL1MXrpmnUNfLkhgXRj",
taskId: "P8ZZX2ef8heTs0erA6a8",
username: "shubham-sharma",
},
type: "task",
body: {
new: {
status: "IN_PROGRESS",
},
subType: "update",
},
timestamp: {
_seconds: 1710731469,
_nanoseconds: 274000000,
},
},
{
meta: {
userId: "aaL1MXrpmnUNfLkhgXRj",
taskId: "P8ZZX2ef8heTs0erA6a8",
username: "shubham-sharma",
},
type: "task",
body: {
new: {
status: "BLOCKED",
},
subType: "update",
},
timestamp: {
_seconds: 1710731408,
_nanoseconds: 684000000,
},
},
];
Loading
Loading