Last active
October 8, 2019 10:35
-
-
Save nwaughachukwuma/66dc25a6ee1e7d856bad4f2a3b1b788f to your computer and use it in GitHub Desktop.
user updated trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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