finishing first function and no errors, but need to test

This commit is contained in:
Arjun Patel
2022-01-28 00:36:25 -08:00
parent 133d5f02d0
commit 9eff310e7f
5 changed files with 95 additions and 29 deletions
+13 -1
View File
@@ -8,6 +8,18 @@
"npm --prefix functions run build", "npm --prefix functions run build",
"npm --prefix \"$RESOURCE_DIR\" run lint", "npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build" "npm --prefix \"$RESOURCE_DIR\" run build"
] ],
"source": "functions"
},
"emulators": {
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"ui": {
"enabled": true
}
} }
} }
+1 -1
View File
@@ -30,6 +30,6 @@ module.exports = {
rules: { rules: {
quotes: ["error", "double"], quotes: ["error", "double"],
// "import/no-unresolved": 0, // "import/no-unresolved": 0,
// "no-unused-vars": "disabled", "no-unused-vars": 0,
}, },
} }
+2 -2
View File
@@ -3,13 +3,13 @@
"scripts": { "scripts": {
"lint": "eslint --ext .js,.ts .", "lint": "eslint --ext .js,.ts .",
"build": "npm run lint && tsc", "build": "npm run lint && tsc",
"serve": "npm run build && firebase emulators:start --only functions & tsc --watch", "serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell", "shell": "npm run build && firebase functions:shell",
"start": "npm run shell", "start": "npm run shell",
"deploy": "firebase deploy --only functions", "deploy": "firebase deploy --only functions",
"logs": "firebase functions:log", "logs": "firebase functions:log",
"hotReload": "tsc --watch", "hotReload": "tsc --watch",
"copyModels": "cp -r ../../web/models /models" "copyModels": "cp -r ../../web/models ./models"
}, },
"engines": { "engines": {
"node": "16" "node": "16"
+2 -2
View File
@@ -14,5 +14,5 @@ export const helloWorld = functions.https.onRequest((request, response) => {
import { agoraToken } from "./agora"; import { agoraToken } from "./agora";
exports.agoraToken = agoraToken; exports.agoraToken = agoraToken;
// import { inviteUserToTeam } from "./notifications"; import { inviteUserToTeam } from "./notifications";
// exports.inviteUserToTeam = inviteUserToTeam; exports.inviteUserToTeam = inviteUserToTeam;
+77 -23
View File
@@ -1,8 +1,8 @@
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
import * as functions from "firebase-functions"; import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import * as sgMail from "@sendgrid/mail"; import * as sgMail from "@sendgrid/mail";
import { MailDataRequired } from "@sendgrid/mail";
import { TeamMember, TeamMemberStatus } from "../../../web/models/teamMember";
sgMail.setApiKey( sgMail.setApiKey(
"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" "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"
@@ -12,38 +12,92 @@ enum SendgridTemplateId {
inviteTeamMember = "d-4953372ac7b94d3fa9419bad4073840f", inviteTeamMember = "d-4953372ac7b94d3fa9419bad4073840f",
} }
class SendGridEmailMessage { const SENDER_EMAIL = "[email protected]"
to: string;
from: string;
templateId: SendgridTemplateId;
dynamic_template_data: {};
constructor( // class SendGridEmailMessage {
_to: string, // to: string;
_from: string, // from: string = "[email protected]"
_templateId: SendgridTemplateId, // templateId: SendgridTemplateId;
_templateData: {} // dynamic_template_data: {};
) {
this.to = _to; // constructor(
this.from = _from; // _to: string,
this.templateId = _templateId; // _templateId: SendgridTemplateId,
this.dynamic_template_data = _templateData; // _templateData: {}
} // ) {
// this.to = _to;
// this.templateId = _templateId;
// this.dynamic_template_data = _templateData;
// }
// }
type Team = {
id: string;
name: string;
status: string;
allowedUserCount: number;
companySite: string;
createdByUserId: string;
} }
type TeamMember = {
id: string;
userId: string;
teamId: string;
inviteEmailAddress: string;
invitedByUserId: string;
role: string
status: string
}
// onCreate of team invite to a member -> email
export const inviteUserToTeam = functions.firestore export const inviteUserToTeam = functions.firestore
.document("teamMembers/{teamMemberId}") .document("teamMembers/{teamMemberId}")
.onCreate(async (snap, context) => { .onCreate(async (snap, context) => {
const newTeamMember: TeamMember = snap.data() as TeamMember; const newTeamMember = snap.data() as TeamMember;
if (newTeamMember.status == TeamMemberStatus.invited) {
if (!newTeamMember || !newTeamMember.inviteEmailAddress) {
functions.logger.error("No team member email available")
return
}
// only if invited will be sending invite
if (newTeamMember.status == "invited") {
functions.logger.info( functions.logger.info(
"New team member invited...sending email" + "New team member invited...sending email" +
newTeamMember.inviteEmailAddress, newTeamMember.inviteEmailAddress,
{ structuredData: true } { structuredData: true }
); )
// send email to people // get team information from team id
const snapshot = await admin.firestore().collection("teams").doc(newTeamMember.teamId).get()
if (!snapshot.exists) {
functions.logger.error("No team exists")
return
}
const team: Team = snapshot.data() as Team;
return; // send email to invited person, based on the email address
const message: MailDataRequired = {
from: SENDER_EMAIL,
to: newTeamMember.inviteEmailAddress,
templateId: SendgridTemplateId.inviteTeamMember,
dynamicTemplateData: {
teamName: team.name,
subject: "Your Team is On Nirvana - " + team.name,
joinTeamUrl: "https://usenirvana.com/teams/login",
landingPageUrl: "https://usenirvana.com"
}
}
return await sgMail.send(message)
} }
return
}) })