mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
🐛Remove stripe_invoices API caching
This commit is contained in:
parent
c25c5e8f97
commit
7620ba00ad
1 changed files with 26 additions and 23 deletions
|
@ -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) ->
|
||||
|
|
Loading…
Reference in a new issue