From 7620ba00ad3b6dbe9fe1fb4bc428b25b6250e622 Mon Sep 17 00:00:00 2001 From: Matt Lott Date: Thu, 9 Jul 2015 13:28:55 -0700 Subject: [PATCH] :bug:Remove stripe_invoices API caching --- server/payments/subscription_handler.coffee | 49 +++++++++++---------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/server/payments/subscription_handler.coffee b/server/payments/subscription_handler.coffee index 99e0cb5e4..fae135bb8 100644 --- a/server/payments/subscription_handler.coffee +++ b/server/payments/subscription_handler.coffee @@ -38,36 +38,39 @@ class SubscriptionHandler extends Handler # console.log 'subscription_handler getStripeEvents', req.body?.options return @sendForbiddenError(res) unless req.user?.isAdmin() stripe.events.list req.body.options, (err, events) => - return done(err) if err + return @sendDatabaseError(res, err) if err @sendSuccess(res, events) getStripeInvoices: (req, res) -> # console.log 'subscription_handler getStripeInvoices' return @sendForbiddenError(res) unless req.user?.isAdmin() - @oldInvoices ?= {} - buildInvoicesFromCache = (newInvoices) => - data = (invoice for invoiceID, invoice of @oldInvoices) - data = data.concat(newInvoices) - data.sort (a, b) -> if a.date > b.date then -1 else 1 - {has_more: false, data: data} - oldInvoiceCutoffDays = 16 # Dependent on Stripe subscription payment retries - oldInvoiceCutoffDate = new Date() - oldInvoiceCutoffDate.setUTCDate(oldInvoiceCutoffDate.getUTCDate() - oldInvoiceCutoffDays) + + # TODO: this caching mechanism doesn't work in production (multiple calls or app servers?) + # TODO: cache older invoices in the analytics database instead + # @oldInvoices ?= {} + # buildInvoicesFromCache = (newInvoices) => + # data = (invoice for invoiceID, invoice of @oldInvoices) + # data = data.concat(newInvoices) + # data.sort (a, b) -> if a.date > b.date then -1 else 1 + # {has_more: false, data: data} + # oldInvoiceCutoffDays = 16 # Dependent on Stripe subscription payment retries + # oldInvoiceCutoffDate = new Date() + # oldInvoiceCutoffDate.setUTCDate(oldInvoiceCutoffDate.getUTCDate() - oldInvoiceCutoffDays) stripe.invoices.list req.body.options, (err, invoices) => return @sendDatabaseError(res, err) if err - newInvoices = [] - for invoice, i in invoices.data - if new Date(invoice.date * 1000) < oldInvoiceCutoffDate - if invoice.id of @oldInvoices - # Rest of the invoices should be cached, return from cache - cachedInvoices = buildInvoicesFromCache(newInvoices) - return @sendSuccess(res, cachedInvoices) - else - # Cache older invoices - @oldInvoices[invoice.id] = invoice - else - # Keep track of new invoices for this page of invoices - newInvoices.push(invoice) + # newInvoices = [] + # for invoice, i in invoices.data + # if new Date(invoice.date * 1000) < oldInvoiceCutoffDate + # if invoice.id of @oldInvoices + # # Rest of the invoices should be cached, return from cache + # cachedInvoices = buildInvoicesFromCache(newInvoices) + # return @sendSuccess(res, cachedInvoices) + # else + # # Cache older invoices + # @oldInvoices[invoice.id] = invoice + # else + # # Keep track of new invoices for this page of invoices + # newInvoices.push(invoice) @sendSuccess(res, invoices) getStripeSubscriptions: (req, res) ->