Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added email feature updated sw.js #12

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ dotenv.config()
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
// credentials from firebase
// credentials for email sending
// configure gmail or use SMTP for other services
user: process.env.EMAIL,
pass: process.env.PASSWORD,
},
Expand All @@ -33,7 +34,8 @@ exports.mail = onRequest(
if (request.method !== "POST") {
response.status(405).send("Method not allowed")
}
const { name, email, message } = request.body
// default values for email of Admin used if not provided
const { name, email = process.env.ADMIN_EMAIL || "[email protected]", message } = request.body

// validate request
if (!name || !email || !message) {
Expand All @@ -47,7 +49,7 @@ exports.mail = onRequest(
}

const mailOptions = {
from: process.env.EMAIL,
from: process.env.EMAIL_FROM || process.env.EMAIL,
to: email,
subject: `New Hall Booking request for RNS Halls by ${name}`,
text: message,
Expand Down
1 change: 1 addition & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// version development 1.1.0
// halls selection now available ! !
// added email sending feature
// added email to requestee feature
self.addEventListener("install", function (e) {
e.waitUntil(
caches.open("rnsHalls").then(function (cache) {
Expand Down
6 changes: 6 additions & 0 deletions src/modules/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ class DBs {
}
}

async getUser(uid) {
const data = await get(ref(this.db, `users/${uid}/profile`))
return data.val()
}

enableAdminFeatures() {
// Enable notifications and booking requests
const q = query(ref(this.db, `notifications/`), limitToLast(15))
Expand Down Expand Up @@ -384,6 +389,7 @@ class DBs {
if (success) {
document.querySelector(`#brq-${snapshot.key}`).remove()
toast(`Booking ${data.key} Accepted !`)
this.getUser(data.uid) .then(({ email, displayName }) => sendMail(displayName, `Your Booking Request for RNS Halls has been Approved !`, email))
} else toast("Some Error Occured !")
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/sendMail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
*
* @param {string?} name user's name
* @param {string?} message
// * @param {string} email Email address of the Admin
// * @param {string?} email Email address of the Admin
*/
function sendMail(name = "RNSHalls User", message = "", email = "[email protected]") {
function sendMail(name = "RNSHalls User", message = "", email) {
// 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`
Expand All @@ -17,7 +17,7 @@ function sendMail(name = "RNSHalls User", message = "", email = "amankrokx@gmail
redirect: "follow",
body: JSON.stringify({
name,
email,
email: email || undefined,
message,
}),
}).then((response) => {
Expand Down
Loading