From 367c9899a0cc6ae896830bafe63ceb16e179a55e Mon Sep 17 00:00:00 2001 From: X4Lo Date: Sat, 12 Jul 2025 02:50:44 +0100 Subject: [PATCH] Backend: Added auto entry refrech each 24h --- backend/controllers/entryController.js | 2 +- backend/services/entryManager.js | 38 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/backend/controllers/entryController.js b/backend/controllers/entryController.js index b4ebe4d..aa6f097 100644 --- a/backend/controllers/entryController.js +++ b/backend/controllers/entryController.js @@ -27,7 +27,7 @@ class EntryController { async getEntryById(req, res) { const { id } = req.params; - let entry = await _entryService.getEntryById(id, true); + let entry = await _entryManager.getUpdatedEntry(id); res.status(200).json(entry); } diff --git a/backend/services/entryManager.js b/backend/services/entryManager.js index 496497f..cf19943 100644 --- a/backend/services/entryManager.js +++ b/backend/services/entryManager.js @@ -366,6 +366,44 @@ class EntryManager { return refreshedEntry; } + /** + * Retrieves an entry by its ID and refreshes it if needed. + * + * An entry is refreshed if: + * - It has never been fetched before (`last_fetch` is null or undefined), or + * - The last fetch was more than 24 hours ago. + * + * If a refresh is required, it logs the operation and calls `refrechEntryById`. + * + * @async + * @function getUpdatedEntry + * @param {number} id - The ID of the entry to retrieve. + * @returns {Promise} The entry object, either current or refreshed. + */ + async getUpdatedEntry(id) { + let entry = await _entryService.getEntryById(id, false); + + let doRefrech = false; + if (!entry.last_fetch) { + doRefrech = true; + } else { + // check if the last fetch was more than 24 hours ago + const lastFetchDate = new Date(entry.last_fetch); + const currentDate = new Date(); + const diffInHours = (currentDate - lastFetchDate) / (1000 * 60 * 60); + if (diffInHours > 24 && !(entry.is_tvseries && entry.status === 'Ended')) { + doRefrech = true; + } + } + + if (doRefrech) { + logToFile(`Refrech Entry: ${entry.id} - ${entry.title}`); + entry = await this.refrechEntryById(id, true); + } + + return entry; + } + /** This function will refresh data of series that have the status not 'ended/canceled' and have not been refreshed today */ async SyncSeriesData() {