From d1e9cab3a63fb478f5db334a151f452fdc6bb132 Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Tue, 23 Dec 2025 17:22:33 -0600 Subject: [PATCH] commit before changing for new version --- .DS_Store | Bin 0 -> 6148 bytes agenda.js | 8 +++---- index.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e14fde147f959274863cbf8ec83ff343329e1781 GIT binary patch literal 6148 zcmeHKy-veG47Srq1i_Gz3EjX9^bJB4HnxfxRDxQhgj9m=c@W-$XF#lsY%uZ+Ong3{ zN}|w;2?4St-*@rPw=$3(=VMKL5wi70^zMw=ME5b=xlBnlRBvaC@~hvSR7n)f2j z@LL&>vx{k;rgTNOo%7?n_qMO9^L$d)6WEfI-PvKDJ|1=b#ajP-)_OMvPe`S0x~7^6 zx}-Cz{A@ubu5Q}^?dsBaDU+UwQ}vM{@mwx;@kU<_w3b{gEKJ(jDbJT z0BW{KvZZLFF<=ZB14{yPu762H->;-e_B_ziM%mOxw@IY8oftt#p z#b8Z`-G#U;V56w%#G(1%aAyu33j4cbedoi8GesMX0b`)cz(6lkssG2T`~Pl|T^R$$ zz`tUE<9wWt@JOMy_8v}Zt%IIJMI^3KT&7^+OEF@l6z@R2!0u!M%mOxwut4lbz|&xZ JG4P`dd;lEUQv3h_ literal 0 HcmV?d00001 diff --git a/agenda.js b/agenda.js index 26dd21d..78779b0 100644 --- a/agenda.js +++ b/agenda.js @@ -81,10 +81,10 @@ const initializeAgenda = async (mongoUri, pool, io) => { // Now accepts pgPool return rest; }); - const posWithoutNumber = changedPosList.map(pos => { - const { periphNum, ...rest } = pos; - return rest; - }); + // const posWithoutNumber = changedPosList.map(pos => { + // const { periphNum, ...rest } = pos; + // return rest; + // }); for (const pos of changedPosList) { const result = await sharedPgPool.query("update peripherals set last_pos=$1, last_set=$2 where id=$3 and user_id=$4", diff --git a/index.js b/index.js index 83d1aab..28c952b 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const socketIo = require('socket.io'); const connectDB = require('./db'); // Your Mongoose DB connection const { initializeAgenda } = require('./agenda'); // Agenda setup const format = require('pg-format'); +const cronParser = require('cron-parser'); const app = express(); const port = 3000; @@ -533,4 +534,66 @@ app.post('/delete_peripheral', authenticateToken, async (req, res) => { server.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); -}); \ No newline at end of file +}); + +app.post('/periph_schedule_list', authenticateToken, async (req, res) => { + try { + console.log("Schedule List request for user:", req.user); + const { periphId } = req.body; + + if (!periphId) { + return res.status(400).json({ error: 'periphId is required in the request body.' }); + } + + // FIX 1: Assign the returned array directly to a variable (e.g., 'jobs') + const jobs = await agenda.jobs({ + 'name': 'posChangeScheduled', + 'data.changedPosList': { $size: 1 }, + 'data.changedPosList.0.periphID': periphId, + 'data.userID': req.user + }); + + // FIX 2: Use .filter() and .map() to handle all cases cleanly. + // This creates a cleaner, more predictable transformation pipeline. + const details = jobs + // Step 1: Filter out any jobs that are not recurring. + .filter(job => job.attrs.repeatInterval) + // Step 2: Map the remaining recurring jobs to our desired format. + .map(job => { + const { repeatInterval, data, repeatTimezone } = job.attrs; + try { + const interval = cronParser.parseExpression(repeatInterval, { + tz: repeatTimezone || undefined + }); + const fields = interval.fields; + + // Make sure to declare the variable + const parsedSchedule = { + minutes: fields.minute, + hours: fields.hour, + daysOfMonth: fields.dayOfMonth, + months: fields.month, + daysOfWeek: fields.dayOfWeek, + }; + + return { + id: job.attrs._id, // It's good practice to return the job ID + schedule: parsedSchedule, + pos: data.changedPosList[0].pos + }; + } catch (err) { + // If parsing fails for a specific job, log it and filter it out. + console.error(`Could not parse "${repeatInterval}" for job ${job.attrs._id}. Skipping.`); + return null; // Return null for now + } + }) + // Step 3: Filter out any nulls that resulted from a parsing error. + .filter(detail => detail !== null); + + res.status(200).json({ scheduledUpdates: details }); + + } catch (error) { // FIX 3: Capture and log the actual error + console.error("Error in /periph_schedule_list:", error); + res.status(500).json({ error: 'Internal Server Error' }); + } +});