Skip to content

Instantly share code, notes, and snippets.

@nwaughachukwuma
Last active October 8, 2019 10:35
Show Gist options
  • Save nwaughachukwuma/66dc25a6ee1e7d856bad4f2a3b1b788f to your computer and use it in GitHub Desktop.
Save nwaughachukwuma/66dc25a6ee1e7d856bad4f2a3b1b788f to your computer and use it in GitHub Desktop.
user updated trigger
/**
* User updated trigger
*/
import * as functions from 'firebase-functions'
import { createStripeCustomer } from './stripe';
// listen for changes when user record is updated
export const userUpdatedTrigger = functions.firestore
.document('users/{userId}')
.onUpdate(async (change, context) => {
const prevSnap = change.before
const newSnap = change.after
// ensure that it's the first login
if (prevSnap.exists) {
if (prevSnap.data() && !prevSnap.data().lastLoginAt) {
// we have eliminated the chances that's it's a returning verified-user
try {
// the user record exists, create the customer on stripe
if (newSnap.exists) {
await createStripeCustomer(newSnap)
}
} catch (err) {
console.error(err)
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment