Skip to content

Commit

Permalink
Functions email setit
Browse files Browse the repository at this point in the history
  • Loading branch information
t0nninseteli committed Apr 13, 2021
1 parent 425616a commit 910f73b
Show file tree
Hide file tree
Showing 6 changed files with 2,737 additions and 0 deletions.
5 changes: 5 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
}
17 changes: 17 additions & 0 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
// "eslint:recommended",
"google",
],
parserOptions: {
ecmaVersion: 8,
},
rules: {
quotes: ["error", "double"],
},
};
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
124 changes: 124 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"use strict";

/* eslint-disable */
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require("nodemailer");
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});

var serviceAccount = require("./lpbd-team-3-firebase-adminsdk-x564k-4f2d230abe.json");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});

// Your company name to include in the emails
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = "Accessor";

// [START sendWelcomeEmail]
/**
* Sends a welcome email to new user.
*/
// [START onCreateTrigger]
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
// [END onCreateTrigger]
// [START eventAttributes]
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
// [END eventAttributes]

return sendWelcomeEmail(email, displayName);
});
// [END sendWelcomeEmail]

// [START sendByeEmail]
/**
* Send an account deleted email confirmation to users who delete their accounts.
*/
// [START onDeleteTrigger]
exports.sendByeEmail = functions.auth.user().onDelete((user) => {
// [END onDeleteTrigger]
const email = user.email;
const displayName = user.displayName;

return sendGoodbyeEmail(email, displayName);
});
// [END sendByeEmail]

exports.sendPostingAlert = functions.firestore
.document("postings/{id}")
.onCreate((snapshot, context) => {
functions.logger.log(
"sendPostingAlert: ",
snapshot._fieldsProto.auditorId.stringValue
);
var db = admin.firestore();
let query = db
.collection("users")
.doc(snapshot._fieldsProto.auditorId.stringValue);
query.get().then((qsnapshot) => {
functions.logger.log("sendPostingAlert: ", qsnapshot);
const email = qsnapshot._fieldsProto.email.stringValue;
const displayName = qsnapshot._fieldsProto.displayName.stringValue;
return sendNewPostingAlert(email, displayName);
});
});

// Sends a welcome email to the given user.
async function sendWelcomeEmail(email, displayName) {
const mailOptions = {
from: `${APP_NAME} <[email protected]>`,
to: email,
};

// The user subscribed to the newsletter.
mailOptions.subject = `Welcome to ${APP_NAME}!`;
mailOptions.text = `Hey ${
displayName || ""
}! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
await mailTransport.sendMail(mailOptions);
functions.logger.log("New welcome email sent to:", email);
return null;
}

// Sends a goodbye email to the given user.
async function sendGoodbyeEmail(email, displayName) {
const mailOptions = {
from: `${APP_NAME} <[email protected]>`,
to: email,
};

// The user unsubscribed to the newsletter.
mailOptions.subject = `Bye!`;
mailOptions.text = `Hey ${
displayName || ""
}!, We confirm that we have deleted your ${APP_NAME} account.`;
await mailTransport.sendMail(mailOptions);
functions.logger.log("Account deletion confirmation email sent to:", email);
return null;
}

async function sendNewPostingAlert(email, displayName) {
const mailOptions = {
from: `${APP_NAME} <[email protected]>`,
to: email,
};

// The user subscribed to the newsletter.
mailOptions.subject = `You have received new evaluation offer!`;
mailOptions.text = `Hey ${
displayName || ""
}! You have received new evaluation offer. See it from https://lpbd-team-3.web.app/`;
await mailTransport.sendMail(mailOptions);
functions.logger.log("New posting alert email sent to:", email);
return null;
}
Loading

0 comments on commit 910f73b

Please sign in to comment.