// app.post("/photos/upload", uploadPhoto.single("photo"), (req, res) => { // if (!req.file) { // return res.status(400).json({ // success: false, // error: 'Expect form‑data field "photo" (file)' // }); // } // return res.json({ // success: true, // saved_as: `photos/${req.file.filename}` // }); // }); // app.post("/photos/upload", uploadPhoto.single("photo"), (req, res) => { // if (!req.file) { // return res.status(400).json({ // success: false, // error: 'Expect form‑data field "photo" (file)' // }); // } // // 🔁 Run register.py automatically // const python = spawn("python", ["./python/register.py"]); // python.stdout.on("data", (data) => { // console.log(`✅ register.py: ${data}`); // }); // python.stderr.on("data", (data) => { // console.error(`❌ register.py error: ${data}`); // }); // python.on("close", (code) => { // console.log(`🔁 register.py exited with code ${code}`); // }); // return res.json({ // success: true, // saved_as: `photos/${req.file.filename}`, // message: "Photo uploaded and registration triggered" // }); // }); //verify app.post("/verify", upload.single("photo"), async (req, res) => { if (!req.file) return res.status(400).json({ error: 'Expect form‑data field "photo"' }); try { const result = await runPython( "./python/verify.py", [resolve(req.file.path)] ); res.json(result); } catch (e) { console.error(e); res.status(500).json({ error: "Verification failed", details: String(e) }); } }); //verify2 app.post("/verify", upload.single("photo"), async (req, res) => { if (!req.file) return res.status(400).json({ error: 'Expect form‑data field "photo"' }); // 1️⃣ Run Python verifier let verification; try { verification = await runPython( "./python/verify.py", [resolve(req.file.path)] ); } catch (e) { console.error(e); return res.status(500).json({ error: "Verification failed", details: String(e) }); } // 2️⃣ Prepare async DB helpers const query = promisify(db.query).bind(db); const today = new Date().toISOString().slice(0, 10); // YYYY‑MM‑DD const grpImg = path.basename(req.file.filename); // saved name // 3️⃣ Process each student asynchronously const ops = (verification.results || []).map(async (rec) => { if (!rec.name) return; try { // fetch student_id const rows = await query("SELECT id FROM students WHERE name = ? LIMIT 1", [rec.name]); if (rows.length === 0) { console.warn(`⚠️ No DB student for "${rec.name}"`); return; } const studentId = rows[0].id; // insert/update attendance const sql = ` INSERT INTO attendance (student_id, date, status, image) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE status = VALUES(status), image = VALUES(image), updatedAt = CURRENT_TIMESTAMP `; await query(sql, [ studentId, today, rec.attendance, grpImg ]); console.log(`📌 Attendance logged: ${rec.name} -> ${rec.attendance}`); } catch (err) { console.error(`❌ DB op failed for ${rec.name}:`, err); } }); await Promise.all(ops); // wait for every insert before responding // 4️⃣ Send the SAME JSON back to client res.json(verification); }); //verify3 // import { promisify } from "util"; // At top app.post("/verify", upload.single("photo"), async (req, res) => { if (!req.file) return res.status(400).json({ error: 'Expect form‑data field "photo"' }); // 1️⃣ Run Python verifier let verification; try { verification = await runPython( "./python/verify.py", [resolve(req.file.path)] ); } catch (e) { console.error(e); return res.status(500).json({ error: "Verification failed", details: String(e) }); } // 2️⃣ Check if there are any registered students const query = promisify(db.query).bind(db); const today = new Date().toISOString().slice(0, 10); // YYYY‑MM‑DD const grpImg = path.basename(req.file.filename); // saved name let studentRows = []; try { studentRows = await query("SELECT id, name FROM students"); if (studentRows.length === 0) { return res.status(200).json({ image: grpImg, message: "No registered student found in database", class_strength: 0, present: 0, absent: 0, results: [] }); } } catch (err) { console.error("❌ MySQL query failed:", err); return res.status(500).json({ error: "Database error", details: String(err) }); } // 3️⃣ Process attendance logging const ops = (verification.results || []).map(async (rec) => { if (!rec.name) return; try { // fetch student_id const rows = await query("SELECT id FROM students WHERE name = ? LIMIT 1", [rec.name]); if (rows.length === 0) { console.warn(`⚠️ No DB student for "${rec.name}"`); return; } const studentId = rows[0].id; // insert/update attendance const sql = ` INSERT INTO attendance (student_id, date, status, image) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE status = VALUES(status), image = VALUES(image), updatedAt = CURRENT_TIMESTAMP `; await query(sql, [studentId, today, rec.attendance, grpImg]); console.log(`📌 Attendance logged: ${rec.name} -> ${rec.attendance}`); } catch (err) { console.error(`❌ DB op failed for ${rec.name}:`, err); } }); await Promise.all(ops); // 4️⃣ Send the same enriched response res.json(verification); });