Skip to content

Commit

Permalink
Added firestore trigger to update when user changes location
Browse files Browse the repository at this point in the history
  • Loading branch information
alromh87 committed Sep 14, 2020
1 parent dc4ad10 commit b287bcc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions functions/src/Integrations/user-triggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as functions from 'firebase-functions'
import { db } from '../Firebase/firestoreDB'

export handleUserChanges = functions.firestore
.document('v3_users/{id}')
.onWrite(async (change, context) => {

const info = change.after.exists ? change.after.data() : null
const prevInfo = change.before.exists ? change.before.data() : null

const prevCountryCode = (prevInfo.location && prevInfo.location.countryCode) ? prevInfo.location.countryCode : null
const newCountryCode = (info.location && info.location.countryCode) ? info.location.countryCode : null
const prevCountry = prevInfo.country ? prevInfo.country : null
const newCountry = info.country ? info.country : null

if(prevCountryCode !== newCountryCode || prevCountry !== newCountry){
const country = newCountryCode ? newCountryCode : newCountry.toLowerCase()
updateHowTosCountry(info._id, country);
}
})

async function updateHowTosCountry(userId, country){
if (!userId || !country) {
console.error('Missing information to set howToCountry')
return false
}
console.log('Update ', userId, ' moved to :', country)

const querySnapshot = await db.collection('v3_howtos').where("_createdBy", "==", userId).get();

if(querySnapshot){
querySnapshot.forEach(doc => {
console.log('Updating howTo ', doc.data()._id, 'to', country);
doc.ref.update({'_creatorCountry': country}).then( () => {
console.log("Document successfully updated!");
return true
}).catch( error => {
console.error("Error updating HowToCountry: ", error);
return false
});
});
} else {
console.log('Error getting user howTos: ', e);
return false
}
return false
}
2 changes: 2 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { weeklyTasks, dailyTasks } from './exports/tasks'
import { DH_Exports } from './DaveHakkensNL'
import * as IntegrationsSlack from './Integrations/firebase-slack'
import * as IntegrationsDiscord from './Integrations/firebase-discord'
import * as UserTriggers from './Integrations/user-triggers'
import { FirebaseUserBackup } from './Integrations/firebase-userBackup'
import * as IntegrationsEmail from './Integrations/firebase-email'
import * as Admin from './admin'
Expand All @@ -21,6 +22,7 @@ exports.notifyNewEvent = IntegrationsSlack.notifyNewEvent
exports.notifyPinAccepted = IntegrationsDiscord.notifyPinAccepted
exports.notifyHowToAccepted = IntegrationsDiscord.notifyHowToAccepted
exports.notifyEventAccepted = IntegrationsDiscord.notifyEventAccepted
exports.handleUserChanges = UserTriggers.handleUserChanges
exports.firebaseUserBackup = FirebaseUserBackup
exports.emailNotificationDemo = IntegrationsEmail.notifyEmailDemo
// CC Note, 2020-04-40
Expand Down

0 comments on commit b287bcc

Please sign in to comment.