Skip to content

Commit

Permalink
chore: don't defer invoice creation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hazzadous authored May 1, 2023
1 parent 54f7371 commit 30d6c3e
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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 = ''
Expand All @@ -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}`)
}
Expand Down

0 comments on commit 30d6c3e

Please sign in to comment.