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

Commit

Permalink
#21 cloud functions working for notifying doctor of new patients
Browse files Browse the repository at this point in the history
  • Loading branch information
gkillick committed Apr 11, 2022
1 parent 952d7d4 commit a69e355
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
11 changes: 2 additions & 9 deletions functions/src/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ export const sendNotification = functions.https.onCall(async (_data) => {
return sendNotificationHelper(userId, message);
});

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


// send notification for unread messages
export const sendNotificationForConversation = functions.https.onCall(async (_data) => {
Expand Down Expand Up @@ -78,7 +71,7 @@ const delay = (time:number) => {


// helper function to send notification to user
const sendNotificationHelper = async (recipientID: string, notification: UserNotification) => {
export const sendNotificationHelper = async (recipientID: string, notification: UserNotification) => {
const userRef = db.doc(`users/${recipientID}`);
const userSnap = await userRef.get();
return userSnap.ref.update({
Expand All @@ -87,7 +80,7 @@ const sendNotificationHelper = async (recipientID: string, notification: UserNot
};

// format for notification
interface UserNotification {
export interface UserNotification {
title: string;
message: string;
date: admin.firestore.Timestamp;
Expand Down
24 changes: 23 additions & 1 deletion functions/src/triage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import {sendNotificationHelper, UserNotification} from "./notifications";


const db = admin.firestore();

// add new doctor, add new slots and when I close a case
// to be called when adding new doctor or add new slots and when I close a case
export const dispatchDoctor = functions.https.onCall( (_data)=> {
// get doctor with available slots
const medicalId = _data.medicalID;
Expand All @@ -23,6 +24,17 @@ export const dispatchDoctor = functions.https.onCall( (_data)=> {
batch.update(doc.ref, "doctorName", `${_data.firstName} ${_data.lastName}`);
});
batch.commit().then(()=>{
const numberUpdated = querySnap.docs.length;
const plural = numberUpdated > 1 ? "s" : "";
// send message with number of patients assigned
const message: UserNotification = {
title: `New Patient${plural}`,
message: "Your patient list has been updated",
date: admin.firestore.Timestamp.now(),
read: false,
conversationID: null,
};
sendNotificationHelper(medicalId, message);
// adjust doctor slots
const newFilledSlots = _data.filledSlots + querySnap.docs.length;
const newAvailableSlots = _data.availableSlots - querySnap.docs.length;
Expand Down Expand Up @@ -57,6 +69,16 @@ export const requestDoctor = functions.https.onCall(async (_data, context)=>{
const availableDoc = availableDoctorRef.data();
return userSnap.ref
.update({assignedDoctor: availableDoc.UID, doctorName: `${availableDoc.firstName} ${availableDoc.lastName}`}).then(()=>{
// send notification to doctor
const message: UserNotification = {
title: "New Patient",
message: `${userSnap.data()?.firstName} has been assigned to you`,
date: admin.firestore.Timestamp.now(),
read: false,
conversationID: null,
};
sendNotificationHelper(availableDoc.UID, message);

// decrement available Slots
const newfilledSlots = availableDoctorRef.data().filledSlots +1;
const newAvailableSlots = availableDoctorRef.data().patientSlots - newfilledSlots;
Expand Down

0 comments on commit a69e355

Please sign in to comment.