Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Commit

Permalink
#21 refactored notifications
Browse files Browse the repository at this point in the history
- created helper function to be used to send notifications by any cloud function
  • Loading branch information
gkillick authored and Alexialsousa committed Apr 13, 2022
1 parent 6d67f54 commit 6f0461f
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions functions/src/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ export const sendNotification = functions.https.onCall(async (_data) => {
conversationID: null,
};
const userId = _data.userId;
const userRef = db.doc(`users/${userId}`);
const userSnap = await userRef.get();

return userSnap.ref.update({
notifications: admin.firestore.FieldValue.arrayUnion(message),
});
return sendNotificationHelper(userId, message);
});

interface UserNotification {
Expand All @@ -32,9 +27,9 @@ interface UserNotification {
}


// send notification if user is marked as unread in conversation
// send notification for unread messages
export const sendNotificationForConversation = functions.https.onCall(async (_data) => {
// get doctor with available slots
// create message
const message: UserNotification = {
conversationID: _data.conversationID,
title: _data.title,
Expand Down Expand Up @@ -72,14 +67,6 @@ export const sendNotificationForConversation = functions.https.onCall(async (_da
return null;
});

interface UserNotification {
title: string;
message: string;
date: admin.firestore.Timestamp;
read: boolean;
conversationID: string | null;
}

// time is in milliseconds
const delay = (time:number) => {
return new Promise((res) => {
Expand All @@ -88,3 +75,22 @@ const delay = (time:number) => {
}, time);
});
};


// helper function to send notification to user
const sendNotificationHelper = async (recipientID: string, notification: UserNotification) => {
const userRef = db.doc(`users/${recipientID}`);
const userSnap = await userRef.get();
return userSnap.ref.update({
notifications: admin.firestore.FieldValue.arrayUnion(notification),
});
};

// format for notification
interface UserNotification {
title: string;
message: string;
date: admin.firestore.Timestamp;
read: boolean;
conversationID: string | null;
}

0 comments on commit 6f0461f

Please sign in to comment.