Skip to content
Snippets Groups Projects
Commit 89d36a6a authored by Federico Hurtado's avatar Federico Hurtado
Browse files

Able to get login events within a specified timerange. Set up endpoint fully.

parent 5596f9e5
No related branches found
No related tags found
1 merge request!8Feature/activity logger
...@@ -3,6 +3,7 @@ const session = require("express-session"); ...@@ -3,6 +3,7 @@ const session = require("express-session");
const cors = require("cors"); const cors = require("cors");
require("dotenv").config(); require("dotenv").config();
const userRoutes = require("./routes/userRoutes"); const userRoutes = require("./routes/userRoutes");
const activityRouter = require("./routes/activityMonitoringRoutes");
// create connection to db and initialize table schema // create connection to db and initialize table schema
const { pool } = require("./config/database/database.config"); const { pool } = require("./config/database/database.config");
...@@ -49,6 +50,7 @@ app.use(function (req, res, next) { ...@@ -49,6 +50,7 @@ app.use(function (req, res, next) {
require("./config/auth/auth.routes")(app); require("./config/auth/auth.routes")(app);
app.use("/api/user", userRoutes); app.use("/api/user", userRoutes);
app.use("/api/person", peopleRouter); app.use("/api/person", peopleRouter);
app.use("/api/activity", activityRouter);
// Basic route to check if server is running // Basic route to check if server is running
app.get("/api", (req, res) => { app.get("/api", (req, res) => {
......
...@@ -27,7 +27,23 @@ async function logSuccessfulLogin(userId, timestamp) { ...@@ -27,7 +27,23 @@ async function logSuccessfulLogin(userId, timestamp) {
} }
} }
async function getLoginDataTimerange() {} async function getLoginDataTimerange(start, end) {
try {
// get all login data within the start and end timerange
const query = `
SELECT *
FROM userLogin
WHERE eventTimestamp BETWEEN ? AND ?
ORDER BY eventTimestamp DESC
`;
const [rows] = await pool.query(query, [start, end]);
return rows;
} catch (error) {
// throw error to be caught in service
console.error("Error fetching login data:", error);
throw error;
}
}
async function getLoginDataByUser() {} async function getLoginDataByUser() {}
...@@ -39,4 +55,5 @@ async function getDatabaseChangeByUser() {} ...@@ -39,4 +55,5 @@ async function getDatabaseChangeByUser() {}
module.exports = { module.exports = {
logSuccessfulLogin, logSuccessfulLogin,
getLoginDataTimerange,
}; };
const express = require("express");
const { viewLoginsTimerange } = require("../service/activityLoggerService");
const activityRouter = express.Router();
// get login history by timerange
activityRouter.get("/login-data", async (req, res) => {
const { start, end } = req.query;
// ensure both params are given
if (!start || !end) {
return res
.status(400)
.json({ message: "Please provide both start and end dates." });
}
try {
const data = await viewLoginsTimerange(start, end);
res.status(200).json(data);
} catch {
console.log("error fetching login data: ", error);
res.status(500).json({ message: "Internal service error." });
}
});
// get db history for timerange
module.exports = activityRouter;
const { logSuccessfulLogin } = require("../repository/activityRepository"); const {
logSuccessfulLogin,
getLoginDataTimerange,
} = require("../repository/activityRepository");
/* /*
Function that creates a row of data in the userLogin table Function that creates a row of data in the userLogin table
...@@ -15,8 +18,14 @@ async function documentSuccessfulLogin(userId, timestamp) { ...@@ -15,8 +18,14 @@ async function documentSuccessfulLogin(userId, timestamp) {
async function logDatabaseChange() {} async function logDatabaseChange() {}
async function viewLogins() {} /*
Get the login data for a sopecific time range
*/
async function viewLoginsTimerange(start, end) {
// will throw error if there is a problem
return await getLoginDataTimerange(start, end);
}
async function viewDatabaseChanges() {} async function viewDatabaseChanges() {}
module.exports = { documentSuccessfulLogin }; module.exports = { documentSuccessfulLogin, viewLoginsTimerange };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment