Modified the cloud function to only act when there has been a change on priority or event on tasks
This commit is contained in:
parent
7f77d8a86a
commit
74c40c535b
1 changed files with 39 additions and 11 deletions
|
|
@ -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.Change<FirebaseFirestore.DocumentSnapshot>> = functions.firestore
|
||||
|
|
@ -59,6 +75,10 @@ export const pendingTasksUpdater: functions.CloudFunction<functions.Change<Fireb
|
|||
let eventDocumentBefore: DocumentReference | null;
|
||||
|
||||
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);
|
||||
eventDocumentBefore = await getEventReference(before, userDocument);
|
||||
eventDocument = await getEventReference(after, userDocument);
|
||||
|
|
@ -75,14 +95,22 @@ export const pendingTasksUpdater: functions.CloudFunction<functions.Change<Fireb
|
|||
|
||||
switch (action) {
|
||||
case 'create':
|
||||
incrementFromPriority(after.get('priority'), eventDocument, userDocument, incrementValue);
|
||||
await incrementFromPriority(after.get('priority'), eventDocument, userDocument, incrementValue);
|
||||
await incrementPendingTasks(eventDocument, incrementValue);
|
||||
break;
|
||||
case 'delete':
|
||||
incrementFromPriority(before.get('priority'), eventDocument, userDocument, decrementValue);
|
||||
await incrementFromPriority(before.get('priority'), eventDocument, userDocument, decrementValue);
|
||||
await incrementPendingTasks(eventDocument, decrementValue);
|
||||
break;
|
||||
case 'update':
|
||||
incrementFromPriority(before.get('priority'), eventDocumentBefore!, userDocument, decrementValue);
|
||||
incrementFromPriority(after.get('priority'), eventDocument, userDocument, incrementValue);
|
||||
if (before.get('priority') !== after.get('priority')) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue