From f14221a3f75ff949639b24d68bf49f0f2d89eeff Mon Sep 17 00:00:00 2001 From: Aman KUmar Date: Sun, 26 May 2024 18:39:44 +0530 Subject: [PATCH] added email feature --- firebase.json | 51 ++++++++++++++++++++++++++++++++++------- src/index.js | 2 ++ src/modules/db.js | 6 ++++- src/modules/sendMail.js | 33 ++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/modules/sendMail.js diff --git a/firebase.json b/firebase.json index 6415f77..8dc72c6 100644 --- a/firebase.json +++ b/firebase.json @@ -1,12 +1,47 @@ { - "database": { - "rules": "database.rules.json" + "database": { + "rules": "database.rules.json" + }, + "hosting": { + "public": "public", + "ignore": [ + "firebase.json", + "**/.*", + "**/node_modules/**", + "**/src/**" + ], + "rewrites": [ + { + "source": "mail", + "function": "mail" + } + ] + }, + "storage": { + "rules": "storage.rules" + }, + "functions": [ + { + "source": "functions", + "codebase": "default", + "ignore": [ + "node_modules", + ".git", + "firebase-debug.log", + "firebase-debug.*.log" + ], + "predeploy": [ + "npm --prefix \"$RESOURCE_DIR\" run lint" + ] + } + ], + "emulators": { + "functions": { + "port": 5001 }, - "hosting": { - "public": "public", - "ignore": ["firebase.json", "**/.*", "**/node_modules/**", "**/src/**"] + "ui": { + "enabled": true }, - "storage": { - "rules": "storage.rules" - } + "singleProjectMode": true + } } diff --git a/src/index.js b/src/index.js index fbbe260..351e733 100644 --- a/src/index.js +++ b/src/index.js @@ -49,6 +49,8 @@ onAuthStateChanged(auth, user => { } }) if (user) { + window.userDisplayName = user.displayName + window.userEmail = user.email // User is signed in, see docs for a list of available properties // https://firebase.google.com/docs/reference/js/firebase.User // console.log(user) diff --git a/src/modules/db.js b/src/modules/db.js index b302944..56232ac 100644 --- a/src/modules/db.js +++ b/src/modules/db.js @@ -1,4 +1,5 @@ -import { ref, set, update, onValue, get, child, onChildAdded, query, limitToLast, off } from "firebase/database" +import { child, get, limitToLast, off, onChildAdded, onValue, query, ref, set, update } from "firebase/database" +import sendMail from "./sendMail.js" class DBs { constructor(db, uid, hid) { @@ -137,6 +138,8 @@ class DBs { this.cart = { value: 0 } this.writeToPath(`users/${this.uid}/bookingRequests/${this.hallid}/${t}`, clone) clone.uid = this.uid + // send email to admin + sendMail(window.userDisplayName || window.userEmail || "RNSHalls User") this.writeToPath(`admin/bookingRequests/${this.hallid}/${t}`, clone) for (let i = 0; i < this.timeEntry.length; i++) this.timeEntry[i].classList.remove("selected") this.bookingCounter(0) @@ -499,3 +502,4 @@ class DBs { } export { DBs } + diff --git a/src/modules/sendMail.js b/src/modules/sendMail.js new file mode 100644 index 0000000..d715577 --- /dev/null +++ b/src/modules/sendMail.js @@ -0,0 +1,33 @@ +/** + * + * @param {string?} name user's name + * @param {string?} message +// * @param {string} email Email address of the Admin + */ +function sendMail(name = "RNSHalls User", message = "", email = "amankrokx@gmail.com") { + // send email + if (message === "") { + message = `${new Date()}\nNew Hall Booking request for RNS Halls by ${name}\nVisit at https://rnshalls.web.app or https://halls.rnsit.ac.in\n\n RNS Halls` + } + fetch("/mail", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + redirect: "follow", + body: JSON.stringify({ + name, + email, + message, + }), + }).then((response) => { + if (response.ok) { + console.log("Email sent successfully") + } else { + console.error("Error sending email") + alert("Warn: Admin not notified. Please inform them manually if urgent.") + } + }) +} + +export default sendMail