diff --git a/backend/.env b/backend/.env deleted file mode 100644 index 8916796..0000000 --- a/backend/.env +++ /dev/null @@ -1,17 +0,0 @@ -# Database connection -PG_HOST=localhost -PG_PORT=5432 -PG_USER=laziest -PG_PASSWORD= -PG_DATABASE=pbms - -# JWT Secret Key -JWT_SECRET=supersecretkey - -# Email (for forgot password) -EMAIL_USER=youremail@gmail.com -EMAIL_PASS=your_app_password -FRONTEND_URL=http://localhost:5173 - -# Port -PORT=5001 diff --git a/backend/controllers/galleryController.js b/backend/controllers/galleryController.js new file mode 100644 index 0000000..29f5ea8 --- /dev/null +++ b/backend/controllers/galleryController.js @@ -0,0 +1,38 @@ +const pool = require("../db"); +const galleryTemplate = require("../emails/galleryTemp"); +const { sendGalleryPublishedEmail } = require("../services/emailService"); + +exports.publishGallery = async (req, res) => { + try { + const { userEmail, userName, galleryName } = req.body; + + if (!userEmail || !userName) { + return res.status(400).json({ message: "Missing user information" }); + } + + console.log(`📧 Sending email to: ${userEmail}`); + console.log(`👤 User: ${userName}`); + console.log(`🖼️ Gallery: ${galleryName}`); + + await sendGalleryPublishedEmail( + userEmail, + userName, + galleryName || "Test Gallery", + galleryTemplate + ); + + console.log("✅ Email sent successfully!"); + + return res.json({ + message: "Gallery published and email sent", + sentTo: userEmail, + galleryName: galleryName + }); + } catch (err) { + console.error("❌ Publish error:", err); + res.status(500).json({ + message: "Failed to send email", + error: err.message + }); + } +}; \ No newline at end of file diff --git a/backend/emails/galleryTemp.js b/backend/emails/galleryTemp.js new file mode 100644 index 0000000..043ce87 --- /dev/null +++ b/backend/emails/galleryTemp.js @@ -0,0 +1,15 @@ +module.exports = function galleryPublishedTemplate(clientName, galleryName) { + return ` +
Hello ${clientName},
+Your gallery ${galleryName} has been published.
+Please log in to your dashboard to view and download your photos.
+ + Log In + +Thank you for choosing our services.
+No users detected.
: -The admin has sent you a new notification.
+This is a test email sent from PBMS.
+ `; + + // Send email through SendGrid + const response = await fetch("https://api.sendgrid.com/v3/mail/send", { + method: "POST", + headers: { + Authorization: `Bearer ${SENDGRID_KEY}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + personalizations: [{ to: [{ email }] }], + from: { email: "admin@pbms.com" }, // MUST be verified in SendGrid + subject, + content: [{ type: "text/html", value: html }], + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + return new Response( + JSON.stringify({ error: "Failed to send email", details: errorText }), + { status: 500 } + ); + } + + return new Response(JSON.stringify({ message: "Email sent!" }), { + status: 200, + }); + } catch (err) { + return new Response( + JSON.stringify({ + error: "Server error", + details: err.message ?? "Unknown error", + }), + { status: 500 } + ); + } +});