From 30d6c3e53c6edd203b13be60ab118442ac0122af Mon Sep 17 00:00:00 2001 From: Harry Waye Date: Mon, 1 May 2023 14:52:46 +0000 Subject: [PATCH] chore: don't defer invoice creation I'm not 100% on why we needed to defer invoice creation, but it seems unnecessary. It was causing issues with pushing large invoices to Kafka. An alternative might be to push the invoices to PostgreSQL as a temp storage, but this seems simpler. --- index.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/index.ts b/index.ts index b278217..03b0604 100644 --- a/index.ts +++ b/index.ts @@ -25,24 +25,22 @@ export async function setupPlugin({ config, global, storage }) { } } -export const jobs = { - saveInvoices: async (invoices, { global, storage, cache }) => { - for (const invoice of invoices) { - if (!invoice.customer) { - console.warn(`Invoice ${invoice.id} does not have a customer associated with it in Stripe, skipping...`) - continue - } +const saveInvoices = async (invoices, { global, storage }: Meta) => { + for (const invoice of invoices) { + if (!invoice.customer) { + console.warn(`Invoice ${invoice.id} does not have a customer associated with it in Stripe, skipping...`) + continue + } - const customer = await getOrSaveCustomer(invoice, invoice.customer, storage, global) + const customer = await getOrSaveCustomer(invoice, invoice.customer, storage, global) - if (customer || global.saveUsersIfNotMatched) { - const groupAddition = customer?.group_key ? { $groups: { [global.groupType]: customer.group_key } } : {} + if (customer || global.saveUsersIfNotMatched) { + const groupAddition = customer?.group_key ? { $groups: { [global.groupType]: customer.group_key } } : {} - if (invoice.subscription) { - await sendSubscriptionEvent(invoice.subscription, customer, storage, groupAddition) - } - await sendInvoiceEvent(invoice, customer, global, storage, groupAddition) + if (invoice.subscription) { + await sendSubscriptionEvent(invoice.subscription, customer, storage, groupAddition) } + await sendInvoiceEvent(invoice, customer, global, storage, groupAddition) } } } @@ -216,8 +214,8 @@ async function asyncFilter(arr, callback) { ) } -export async function runEveryMinute({ storage, jobs, global }: Meta) { - const TEN_MINUTES = 1000 * 60 * 10 +export async function runEveryMinute(meta: Meta) { + const { storage, global } = meta let paginationParam = await storage.get('paginationParam', '') if (!paginationParam) { paginationParam = '' @@ -240,7 +238,7 @@ export async function runEveryMinute({ storage, jobs, global }: Meta) { if (newInvoices.length > 0) { console.log(`Trying to save ${newInvoices.length} new invoices, pagination "${paginationParam}"`) - await jobs.saveInvoices(newInvoices).runNow() + await saveInvoices(newInvoices, meta) } else { console.log(`Page has no unseen invoices, pagination ${paginationParam}`) }