Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud function with Firestore runTransaction call takes around 30 seconds to complete

This function initially makes a Stripe call to charge the user and then creates a transaction to update two different documents - related payment and user. I'm not sure how to optimize this code as I need transaction logic to update documents. How can I optimize this function?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const moment = require('moment')
admin.initializeApp(functions.config().firebase);
const stripe = require('stripe')(functions.config().stripe.testkey)
const db = admin.firestore();

exports.stripeCharge = functions.firestore   
                                .document('users/{userId}/payments/{paymentId}')
                                .onWrite(event => {

const payment = event.data.data();
const userId = event.params.userId;
const paymentId = event.params.paymentId;

if (!payment || payment.charge) return;

const amount = payment.amount;
const duration = payment.duration;
const createdAt = payment.createdAt;
const idempotency_key = paymentId;
const source = payment.token.id;
const currency = 'usd';
const charge = {amount, currency, source};

return stripe.charges.create(charge, { idempotency_key })
    .then(charge => {
        if (!charge) return;

        var userRef = db.collection('users').doc(userId);
        var paymentsRef = userRef.collection('payments').doc(paymentId);
        var transaction = db.runTransaction(t => {
            return t.get(userRef)
                .then(doc => {
                    let expiresAt;
                    if(doc.data().expiresAt
                      && moment(doc.data().expiresAt).isAfter(createdAt)){
                      expiresAt = moment(doc.data().expiresAt).add(duration, 'M').toDate();
                    }else{
                      expiresAt = moment(createdAt).add(duration, 'M').toDate();
                    }
                    t.update(userRef, { 
                        expiresAt: expiresAt, 
                        timestamp: moment().toDate()
                    });
                    t.update(paymentsRef, { charge: charge });
                });
        })
        .then(result => {
            console.log('Successful');
        })
    })
    .catch(err => { console.log(err) })

});

like image 308
tugce Avatar asked Oct 23 '17 03:10

tugce


1 Answers

Returning the transaction itself solved the problem.

So instead of this:

var transaction = db.runTransaction(t => {
        return t.get(userRef)
//...

Do this:

return db.runTransaction(t => {
        return t.get(userRef)
//...
like image 100
tugce Avatar answered Oct 03 '22 11:10

tugce