import express from "express"; import dotenv from "dotenv"; import cron from "node-cron"; import sgMail from "@sendgrid/mail"; // 1. Import SendGrid mail import cors from "cors"; // 2. Import cors package import connectDB from "./config/db.js"; import productRoutes from "./routes/productRoutes.js"; import userRoutes from "./routes/userRoutes.js"; import Product from "./models/productModel.js"; dotenv.config(); connectDB(); const app = express(); // 3. Use the cors middleware (cleaner than manual headers) app.use(cors()); app.use(express.json()); app.get("/", (req, res) => { res.send("API is running..."); }); app.use("/api/products", productRoutes); app.use("/api/users", userRoutes); // Cron Job for Low Stock Alerts cron.schedule("*/30 * * * * *", async () => { console.log("Running cron job: Checking for low-stock products..."); try { const lowStockProducts = await Product.find({ $expr: { $lte: ["$quantity", "$lowStockThreshold"] }, }); if (lowStockProducts.length > 0) { // --- Start of SendGrid Logic --- sgMail.setApiKey(process.env.SENDGRID_API_KEY); const productList = lowStockProducts .map( (p) => `
  • ${p.name} (SKU: ${p.sku}) - Quantity: ${p.quantity}, Threshold: ${p.lowStockThreshold}
  • ` ) .join(""); const msg = { to: process.env.EMAIL_TO, // The recipient from: process.env.EMAIL_USER, // Your verified sender in SendGrid subject: "🔴 Low Stock Alert!", html: `

    The following products are running low on stock:

    Please restock them soon.

    `, }; try { await sgMail.send(msg); console.log("Low stock alert email sent successfully via SendGrid."); } catch (sendGridError) { console.error("Error sending email via SendGrid:", sendGridError); } // --- End of SendGrid Logic --- } else { console.log("No low-stock products found."); } } catch (error) { console.error("Error in cron job:", error); } }); const PORT = process.env.PORT || 5000; app.listen( PORT, "0.0.0.0", // Important for Docker networking console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`) ); //////////////////////////////////////////////////////////////////////////////// // import express from "express"; // import dotenv from "dotenv"; // import cron from "node-cron"; // 1. Import cron // import nodemailer from "nodemailer"; // 2. Import nodemailer // import connectDB from "./config/db.js"; // import productRoutes from "./routes/productRoutes.js"; // import userRoutes from "./routes/userRoutes.js"; // import Product from "./models/productModel.js"; // 3. Import Product Model // dotenv.config(); // connectDB(); // const app = express(); // // CORS middleware for production // app.use((req, res, next) => { // res.header("Access-Control-Allow-Origin", "*"); // res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // res.header( // "Access-Control-Allow-Headers", // "Origin, X-Requested-With, Content-Type, Accept, Authorization" // ); // if (req.method === "OPTIONS") { // res.sendStatus(200); // } else { // next(); // } // }); // // 1. Middleware to accept JSON data // app.use(express.json()); // app.get("/", (req, res) => { // res.send("API is running..."); // }); // // 2. USE THE ROUTES // app.use("/api/products", productRoutes); // app.use("/api/users", userRoutes); // // 3. Add the Cron Job for Low Stock Alerts '*/10 * * * * *'(10 sec) / "* * * * *"(1 min) // cron.schedule("*/30 * * * *", async () => { // console.log("Running cron job: Checking for low-stock products..."); // try { // const lowStockProducts = await Product.find({ // $expr: { $lte: ["$quantity", "$lowStockThreshold"] }, // }); // if (lowStockProducts.length > 0) { // const transporter = nodemailer.createTransport({ // service: "gmail", // auth: { // user: process.env.EMAIL_USER, // pass: process.env.EMAIL_PASS, // }, // }); // const productList = lowStockProducts // .map( // (p) => // `
  • ${p.name} (SKU: ${p.sku}) - Quantity: ${p.quantity}, Threshold: ${p.lowStockThreshold}
  • ` // ) // .join(""); // const mailOptions = { // from: process.env.EMAIL_USER, // to: process.env.EMAIL_TO, // subject: "🔴 Low Stock Alert!", // html: ` //

    The following products are running low on stock:

    // //

    Please restock them soon.

    // `, // }; // transporter.sendMail(mailOptions, (error, info) => { // if (error) { // console.error("Error sending email:", error); // } else { // console.log("Low stock alert email sent:", info.response); // } // }); // } else { // console.log("No low-stock products found."); // } // } catch (error) { // console.error("Error in cron job:", error); // } // }); // const PORT = process.env.PORT || 5000; // app.listen( // PORT, // console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`) // );