Skip to content

Commit

Permalink
fixed issues with admin notifs
Browse files Browse the repository at this point in the history
  • Loading branch information
bwsarge committed Dec 16, 2024
1 parent fd2b9e6 commit 288a723
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
3 changes: 0 additions & 3 deletions functions/src/definitions/eventHandlers/onMessageUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { onDocumentUpdated } from "firebase-functions/v2/firestore"
import { tabulateVoteStats } from "../common/statistics"
import { logger } from "firebase-functions"

const ADMIN_CHAT_ID = String(process.env.ADMIN_CHAT_ID)

const onMessageUpdateV2 = onDocumentUpdated(
{
document: "messages/{messageId}",
Expand All @@ -35,7 +33,6 @@ const onMessageUpdateV2 = onDocumentUpdated(

// If changes from not assessed to assessed
if (!preChangeSnap.data().isAssessed && messageData.isAssessed) {

await postChangeSnap.ref.update({
assessedTimestamp: Timestamp.fromDate(new Date()),
})
Expand Down
17 changes: 9 additions & 8 deletions functions/src/definitions/eventHandlers/onMessageWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const adminMessageHandler = onDocumentWritten(
)
const machineCategory = docSnap.get("machineCategory")
const isMachineCategorised = docSnap.get("isMachineCategorised")
if (isMachineCategorised) {
if (isMachineCategorised && machineCategory !== null) {
await sendVotingUpdate({
messageId: docSnap.get("adminGroupSentMessageId"),
machineCategory: machineCategory,
Expand All @@ -51,19 +51,15 @@ const adminMessageHandler = onDocumentWritten(
const communityNoteAfter = docSnap.get("communityNote") ?? null
const primaryCategoryBefore = before.get("primaryCategory") ?? null
const primaryCategoryAfter = docSnap.get("primaryCategory") ?? null

if (
communityNoteBefore === null &&
communityNoteAfter !== null
) {
if (communityNoteBefore === null && communityNoteAfter !== null) {
//if community note was newly generated
await sendCommunityNoteNotification(
docSnap.get("communityNote"),
adminMessageId,
docSnap.ref
)
}
if (isAssessedBefore === null && isAssessedAfter) {
if (!isAssessedBefore && isAssessedAfter) {
//if message was newly assessed
await sendVotingUpdate({
messageId: docSnap.get("adminGroupSentMessageId"),
Expand All @@ -81,7 +77,12 @@ const adminMessageHandler = onDocumentWritten(
currentCategory: primaryCategoryAfter,
})
}
if (!communityNoteBefore.downvoted && communityNoteAfter.downvoted) {
if (
communityNoteBefore !== null &&
communityNoteAfter !== null &&
!communityNoteBefore.downvoted &&
communityNoteAfter.downvoted
) {
//if community note got downvoted
await sendVotingUpdate({
messageId: docSnap.get(
Expand Down
2 changes: 1 addition & 1 deletion functions/src/services/admin/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function sendCommunityNoteNotification(
replyId ?? topicId
)
const messageId = response?.data.result.message_id
if (messageId) {
if (messageId && communityNote !== null) {
await messageRef.update({
"communityNote.adminGroupCommunityNoteSentMessageId": messageId,
})
Expand Down

0 comments on commit 288a723

Please sign in to comment.