Skip to content

Commit

Permalink
Update functions to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kiminkim724 committed Dec 18, 2024
1 parent 3873f05 commit 21521a6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
// Creates a notification document in the user's notification feed for each active subscription.

// Import necessary Firebase modules
import * as functions from "firebase-functions"
import * as admin from "firebase-admin"
import { Timestamp } from "../firebase"
import { BillHistoryUpdateNotification } from "./types"
import { onDocumentWritten } from "firebase-functions/v2/firestore"

// Get a reference to the Firestore database
const db = admin.firestore()

// Define the populateBillNotificationEvents function
export const populateBillHistoryNotificationEvents = functions.firestore
.document("/generalCourts/{court}/bills/{billId}")
.onWrite(async (snapshot, context) => {
if (!snapshot.after.exists) {
export const populateBillHistoryNotificationEvents = onDocumentWritten(
"/generalCourts/{court}/bills/{billId}",
async event => {
if (!event.data?.after.exists) {
console.error("New snapshot does not exist")
return
}

const documentCreated = !snapshot.before.exists
const documentCreated = !event.data?.before.exists

const oldData = snapshot.before.data()
const newData = snapshot.after.data()
const oldData = event.data?.before.data()
const newData = event.data?.after.data()

const { court } = context.params
const { court } = event.params

// Create a notification event
const createNotificationEvent = async (
Expand Down Expand Up @@ -102,4 +102,5 @@ export const populateBillHistoryNotificationEvents = functions.firestore
await createNotificationEvent(newData)
}
}
})
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
// Creates a notification document in the user's notification feed for each active subscription.

// Import necessary Firebase modules
import * as functions from "firebase-functions"
import * as admin from "firebase-admin"
import { Timestamp } from "../firebase"
import { TestimonySubmissionNotification } from "./types"
import { onDocumentWritten } from "firebase-functions/v2/firestore"

// Get a reference to the Firestore database
const db = admin.firestore()

// Define the populateOrgNotificationEvents function
export const populateTestimonySubmissionNotificationEvents = functions.firestore
.document("/users/{userId}/publishedTestimony/{testimonyId}")
.onWrite(async (snapshot, context) => {
if (!snapshot.after.exists) {
export const populateTestimonySubmissionNotificationEvents = onDocumentWritten(
"/users/{userId}/publishedTestimony/{testimonyId}",
async event => {
if (!event.data?.after.exists) {
console.error("New snapshot does not exist")
return
}

const documentCreated = !snapshot.before.exists
const documentCreated = !event.data?.before.exists

const oldData = snapshot.before.data()
const newData = snapshot.after.data()
const oldData = event.data?.before.data()
const newData = event.data?.after.data()

// New testimony added
if (documentCreated) {
Expand All @@ -39,7 +39,7 @@ export const populateTestimonySubmissionNotificationEvents = functions.firestore

userId: newData?.authorUid,
userRole: newData?.authorRole,
testimonyId: context.params.testimonyId,
testimonyId: event.params.testimonyId,
testimonyUser: newData?.fullName,
testimonyPosition: newData?.position,
testimonyContent: newData?.content,
Expand Down Expand Up @@ -89,4 +89,5 @@ export const populateTestimonySubmissionNotificationEvents = functions.firestore
})
}
}
})
}
)
13 changes: 7 additions & 6 deletions functions/src/subscriptions/followUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ import * as functions from "firebase-functions"
import { subscribeToTestimonyTopic } from "./subscribeToTestimonyTopic"
import { getAuth, UserRecord } from "firebase-admin/auth"
import { getFirestore, Firestore } from "firebase-admin/firestore"
import { onCall } from "firebase-functions/v2/https"

export const followUser = functions.https.onCall(async (data, context) => {
export const followUser = onCall(async request => {
// Debug: Log the received data
console.log("Debug: Data received in followOrg:", data)
console.log("Debug: Data received in followOrg:", request.data)

// Check for authentication
if (!context.auth) {
if (!request.auth) {
throw new functions.https.HttpsError(
"failed-precondition",
"The function must be called while authenticated."
)
}

// Runtime check for 'userLookup' property
if (!data.hasOwnProperty("userLookup")) {
if (!request.data.hasOwnProperty("userLookup")) {
throw new functions.https.HttpsError(
"failed-precondition",
"userLookup must be provided."
)
}

const user: UserRecord = await getAuth().getUser(context.auth.uid)
const userLookup = data.userLookup
const user: UserRecord = await getAuth().getUser(request.auth.uid)
const userLookup = request.data.userLookup
const db: Firestore = getFirestore()

try {
Expand Down
13 changes: 7 additions & 6 deletions functions/src/subscriptions/unfollowUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ import * as functions from "firebase-functions"
import { unsubscribeToTestimonyTopic } from "./unsubscribeToTestimonyTopic"
import { getAuth, UserRecord } from "firebase-admin/auth"
import { getFirestore, Firestore } from "firebase-admin/firestore"
import { onCall } from "firebase-functions/v2/https"

export const unfollowUser = functions.https.onCall(async (data, context) => {
export const unfollowUser = onCall(async request => {
// Debug: Log the received data
console.log("Debug: Data received in unfollowUser:", data)
console.log("Debug: Data received in unfollowUser:", request.data)

// Check for authentication
if (!context.auth) {
if (!request.auth) {
throw new functions.https.HttpsError(
"failed-precondition",
"The function must be called while authenticated."
)
}

// Runtime check for 'userLookup' property
if (!data.hasOwnProperty("userLookup")) {
if (!request.data.hasOwnProperty("userLookup")) {
throw new functions.https.HttpsError(
"failed-precondition",
"userLookup must be provided."
)
}

const user: UserRecord = await getAuth().getUser(context.auth.uid)
const userLookup = data.userLookup
const user: UserRecord = await getAuth().getUser(request.auth.uid)
const userLookup = request.data.userLookup
const db: Firestore = getFirestore()

try {
Expand Down

0 comments on commit 21521a6

Please sign in to comment.