From 0d35a19cc51cb7943d1e0bd2c35cb65125e93f8e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 20 Dec 2025 21:31:35 +0000 Subject: [PATCH] Add endpoint to get last movie update timestamp Co-authored-by: mathieumoran1 --- public/movies.html | 2 +- routes/movies.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/public/movies.html b/public/movies.html index 67e429b..fb1ec57 100644 --- a/public/movies.html +++ b/public/movies.html @@ -98,7 +98,7 @@ diff --git a/routes/movies.js b/routes/movies.js index a07501e..1794197 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -38,6 +38,33 @@ router.get("/", async (req, res) => { } }); +// Get latest movie update/add timestamp +router.get("/last-update", async (req, res) => { + const token = req.headers.authorization?.split(" ")[1]; + if (!token) return res.status(401).json({ message: "Token is required" }); + + try { + jwt.verify(token, process.env.JWT_SECRET); + + const latestMovie = await Movie.findOne().sort({ updatedAt: -1, createdAt: -1, _id: -1 }); + if (!latestMovie) { + return res.status(200).json({ lastUpdated: null }); + } + + const lastUpdated = + latestMovie.updatedAt || + latestMovie.createdAt || + (typeof latestMovie._id?.getTimestamp === "function" ? latestMovie._id.getTimestamp() : null); + + return res.status(200).json({ lastUpdated: lastUpdated ? new Date(lastUpdated).toISOString() : null }); + } catch (error) { + if (error instanceof jwt.JsonWebTokenError) { + return res.status(401).json({ message: "Invalid or expired token" }); + } + return res.status(500).json({ error: error.message }); + } +}); + // Get watched movies for the user router.get("/watchedMovies", async (req, res) => { const token = req.headers.authorization?.split(" ")[1]; // Get token from header