Modified the cloud function to only act when there has been a change on priority or event on tasks

This commit is contained in:
Mariano Uvalle 2019-04-13 19:36:52 -05:00
parent 7f77d8a86a
commit 74c40c535b

View file

@ -1,7 +1,9 @@
import * as functions from 'firebase-functions'; import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'; import * as admin from 'firebase-admin';
import { DocumentSnapshot } from 'firebase-functions/lib/providers/firestore'; 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(); admin.initializeApp();
@ -10,21 +12,27 @@ const incrementValue = admin.firestore.FieldValue.increment(1);
const decrementValue = admin.firestore.FieldValue.increment(-1); const decrementValue = admin.firestore.FieldValue.increment(-1);
const db = admin.firestore(); 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() const querySnapshot = await db.collection('users').where('username', '==', taskSnapShot.get('ownerUsername')).get()
return querySnapshot.docs[0].ref; 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(); const querySnapshot = await userReference.collection('events').where('name', '==', taskSnapshot.get('event')).get();
return querySnapshot.docs[0].ref; 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, priority: number,
eventReference: DocumentReference, eventReference: DocumentReference,
userReference: DocumentReference, userReference: DocumentReference,
value: FirebaseFirestore.FieldValue, value: FieldValue,
) => { ) => {
let userFieldName: string = ''; let userFieldName: string = '';
let eventFieldName: string = ''; let eventFieldName: string = '';
@ -44,8 +52,16 @@ let incrementFromPriority = (
break; break;
} }
eventReference.update({ [eventFieldName]: value }); await eventReference.update({ [eventFieldName]: value });
userReference.update({ [userFieldName]: 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.Change<FirebaseFirestore.DocumentSnapshot>> = functions.firestore export const pendingTasksUpdater: functions.CloudFunction<functions.Change<FirebaseFirestore.DocumentSnapshot>> = functions.firestore
@ -59,6 +75,10 @@ export const pendingTasksUpdater: functions.CloudFunction<functions.Change<Fireb
let eventDocumentBefore: DocumentReference | null; let eventDocumentBefore: DocumentReference | null;
if (change.after.exists && change.before.exists) { if (change.after.exists && change.before.exists) {
if (before.get('priority') === after.get('priority') || before.get('event') === after.get('event')) {
console.log('Nothing to update, exiting function');
return true;
}
userDocument = await getUserReference(after); userDocument = await getUserReference(after);
eventDocumentBefore = await getEventReference(before, userDocument); eventDocumentBefore = await getEventReference(before, userDocument);
eventDocument = await getEventReference(after, userDocument); eventDocument = await getEventReference(after, userDocument);
@ -75,14 +95,22 @@ export const pendingTasksUpdater: functions.CloudFunction<functions.Change<Fireb
switch (action) { switch (action) {
case 'create': case 'create':
incrementFromPriority(after.get('priority'), eventDocument, userDocument, incrementValue); await incrementFromPriority(after.get('priority'), eventDocument, userDocument, incrementValue);
await incrementPendingTasks(eventDocument, incrementValue);
break; break;
case 'delete': case 'delete':
incrementFromPriority(before.get('priority'), eventDocument, userDocument, decrementValue); await incrementFromPriority(before.get('priority'), eventDocument, userDocument, decrementValue);
await incrementPendingTasks(eventDocument, decrementValue);
break; break;
case 'update': case 'update':
incrementFromPriority(before.get('priority'), eventDocumentBefore!, userDocument, decrementValue); if (before.get('priority') !== after.get('priority')) {
incrementFromPriority(after.get('priority'), eventDocument, userDocument, incrementValue); await incrementFromPriority(before.get('priority'), eventDocumentBefore!, userDocument, decrementValue);
await incrementFromPriority(after.get('priority'), eventDocument, userDocument, incrementValue);
}
if (before.get('event') !== after.get('event')) {
await incrementPendingTasks(eventDocumentBefore!, decrementValue);
await incrementPendingTasks(eventDocument, incrementValue);
}
break; break;
} }