diff --git a/functions/src/pending_tasks_updater.ts b/functions/src/pending_tasks_updater.ts index 842ace1..9e9e25e 100644 --- a/functions/src/pending_tasks_updater.ts +++ b/functions/src/pending_tasks_updater.ts @@ -1,7 +1,9 @@ import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; import { DocumentSnapshot } from 'firebase-functions/lib/providers/firestore'; -import { DocumentReference } from '@google-cloud/firestore'; + +type DocumentReference = admin.firestore.DocumentReference; +type FieldValue = admin.firestore.FieldValue; admin.initializeApp(); @@ -10,21 +12,27 @@ const incrementValue = admin.firestore.FieldValue.increment(1); const decrementValue = admin.firestore.FieldValue.increment(-1); const db = admin.firestore(); -let getUserReference = async (taskSnapShot: DocumentSnapshot) => { +/// Gets the user [DocumentReference] for the current task. +const getUserReference = async (taskSnapShot: DocumentSnapshot) => { const querySnapshot = await db.collection('users').where('username', '==', taskSnapShot.get('ownerUsername')).get() return querySnapshot.docs[0].ref; } -let getEventReference = async (taskSnapshot: DocumentSnapshot, userReference: DocumentReference) => { +/// Gets an event document reference for the current task. +const getEventReference = async ( + taskSnapshot: DocumentSnapshot, + userReference: DocumentReference +) => { const querySnapshot = await userReference.collection('events').where('name', '==', taskSnapshot.get('event')).get(); return querySnapshot.docs[0].ref; } -let incrementFromPriority = ( +/// Increments the necessary fields in the event and user documents linked to this task. +const incrementFromPriority = async ( priority: number, eventReference: DocumentReference, userReference: DocumentReference, - value: FirebaseFirestore.FieldValue, + value: FieldValue, ) => { let userFieldName: string = ''; let eventFieldName: string = ''; @@ -44,8 +52,16 @@ let incrementFromPriority = ( break; } - eventReference.update({ [eventFieldName]: value }); - userReference.update({ [userFieldName]: value }); + await eventReference.update({ [eventFieldName]: value }); + await userReference.update({ [userFieldName]: value }); +} + +/// Incrementsj only the 'pendingTasks' field for the provided event. +const incrementPendingTasks = async ( + eventReference: DocumentReference, + value: FieldValue +) => { + await eventReference.update({ 'pendingTasks': value }); } export const pendingTasksUpdater: functions.CloudFunction> = functions.firestore @@ -59,6 +75,10 @@ export const pendingTasksUpdater: functions.CloudFunction