Skip to content

feat: admin notifications for org sign up #1768

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 7 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ export {
cleanupNotifications,
deliverNotifications,
httpsDeliverNotifications,
httpsDeliverAdminNotifications,
adminNotification,
httpsCleanupNotifications,
updateUserNotificationFrequency
updateUserNotificationFrequency,
deliverOrgUpgradeStatus,
httpsDeliverAdminTestinomyNotifications,
adminTestimonyNotification,
httpsDeliverOrgUpgradeStatus
} from "./notifications"

export {
Expand Down
133 changes: 133 additions & 0 deletions functions/src/notifications/deliverNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,95 @@ const deliverEmailNotifications = async () => {
// Wait for all email documents to be created
await Promise.all(emailPromises)
}
type ProfileData = {
email: string
fullName: string
role: string
}

export const deliverOrgUpgradeStatus = functions.firestore
.document("profiles/{userId}")
.onWrite(async (snapshot, context) => {
const before = snapshot.before.data() as ProfileData
const after = snapshot.after.data() as ProfileData

if (before.role === "rolePending") {
await db.collection("emails").add({
to: [
"[email protected]",
"[email protected]",
"[email protected]"
],
subject: `Organization ${
after.role === "organization" ? "Approved" : "Denied"
}`,
text:
after.role === "organization"
? "Your organization's profile has been approved on MAPLE. Thank you for signing up! You can now post testimony for your community to see!"
: "Unfortunately, your request for an organization profile on MAPLE was denied. We apologize for any confusion. Please email [email protected] for further discussion.",
html:
after.role === "organization"
? "Your organization's profile has been approved on MAPLE. Thank you for signing up! You can now post testimony for your community to see!"
: "Unfortunately, your request for an organization profile on MAPLE was denied. We apologize for any confusion. Please email [email protected] for further discussion.",
createdAt: Timestamp.now()
})
if (!snapshot.after.exists) {
console.error("New snapshot does not exist")
return
}
}
})

export const adminNotification = functions.firestore
.document("profiles/{userId}")
.onCreate(async (snapshot, context) => {
const organization = snapshot.data() as ProfileData

await db.collection("emails").add({
to: [
"[email protected]",
"[email protected]",
"[email protected]"
],
message: {
subject: `${organization.fullName} requests approval as an Organization`,
text: `${organization.fullName} has signed up and now requests approval. Please decide whether to accept or reject their request. Along with responding via the Admin page, you can also contact them at ${organization.email}`,
html: `${organization.fullName} has signed up and now requests approval. Please decide whether to accept or reject their request. Along with responding via the Admin page, you can also contact them at ${organization.email}`
},
createdAt: Timestamp.now()
})
// }
if (!snapshot.exists) {
console.error("New snapshot does not exist")
return
}
})
export const adminTestimonyNotification = functions.firestore
.document("reports/{reportId}")
.onCreate(async (snapshot, context) => {
type Report = {
reportId?: string
testimonyId?: string
}
const testimony = snapshot.data() as Report
await db.collection("emails").add({
to: [
"[email protected]",
"[email protected]",
"[email protected]"
],
message: {
subject: `The testimony report ${testimony.reportId} requests approval`,
text: `The testimony ${testimony.testimonyId} has been reported and requires approval; please respond accordingly for report ${testimony.reportId}.`,
html: `The testimony ${testimony.testimonyId} has been reported and requires approval; please respond accordingly for report ${testimony.reportId}.`
},
createdAt: Timestamp.now()
})
if (!snapshot.exists) {
console.error("New snapshot does not exist")
return
}
})

// TODO: Unit tests
const buildDigestData = async (
Expand Down Expand Up @@ -239,3 +328,47 @@ export const httpsDeliverNotifications = functions.https.onRequest(
}
}
)

export const httpsDeliverOrgUpgradeStatus = functions.https.onRequest(
async (request, response) => {
try {
deliverOrgUpgradeStatus

console.log("DEBUG: deliverOrgUpgradeStatus completed")

response.status(200).send("Successfully deliverOrgUpgradeStatus")
} catch (error) {
console.error("Error in deliverOrgUpgradeStatus:", error)
response.status(500).send("Internal server error")
}
}
)

export const httpsDeliverAdminNotifications = functions.https.onRequest(
async (request, response) => {
try {
adminNotification

console.log("DEBUG: deliverAdminNotifications completed")

response.status(200).send("Successfully delivered admin notifications")
} catch (error) {
console.error("Error in deliverNotifications:", error)
response.status(500).send("Internal server error")
}
}
)

export const httpsDeliverAdminTestinomyNotifications =
functions.https.onRequest(async (request, response) => {
try {
adminTestimonyNotification

console.log("DEBUG: deliverNotifications completed")

response.status(200).send("Successfully delivered notifications")
} catch (error) {
console.error("Error in deliverNotifications:", error)
response.status(500).send("Internal server error")
}
})
16 changes: 14 additions & 2 deletions functions/src/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
} from "./cleanupNotifications"
import {
deliverNotifications,
httpsDeliverNotifications
httpsDeliverNotifications,
httpsDeliverAdminNotifications,
adminNotification,
deliverOrgUpgradeStatus,
httpsDeliverOrgUpgradeStatus,
adminTestimonyNotification,
httpsDeliverAdminTestinomyNotifications
} from "./deliverNotifications"
import { updateUserNotificationFrequency } from "./updateUserNotificationFrequency"

Expand All @@ -20,6 +26,12 @@ export {
cleanupNotifications,
deliverNotifications,
httpsDeliverNotifications,
httpsDeliverAdminNotifications,
adminNotification,
httpsCleanupNotifications,
updateUserNotificationFrequency
updateUserNotificationFrequency,
deliverOrgUpgradeStatus,
httpsDeliverAdminTestinomyNotifications,
adminTestimonyNotification,
httpsDeliverOrgUpgradeStatus
}
1 change: 1 addition & 0 deletions scripts/firebase-admin/runNotificationFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const notificationFunctions: {
[K in FunctionName]?: K
} = {
httpsDeliverNotifications: "httpsDeliverNotifications",
httpsDeliverAdminNotifications: "httpsDeliverAdminNotifications",
httpsCleanupNotifications: "httpsCleanupNotifications"
}

Expand Down