Skip to content

Commit b63ec39

Browse files
authored
fix: refetch customer on balance webhook (#78)
Orb ocassionally sends multiple credit notes with the same timestamp at roughly the same time - this leads to possibly persisting an old customer balance. To prevent this, we will query the Orb customer via API to get the latest state.
1 parent 5a5883c commit b63ec39

File tree

2 files changed

+177
-9
lines changed

2 files changed

+177
-9
lines changed

package-lock.json

Lines changed: 167 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/orb-sync-lib/src/orb-sync.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ export class OrbSync {
110110
case 'data_exports.transfer_error': {
111111
break;
112112
}
113-
case 'customer.balance_transaction_created':
114113
case 'customer.created':
115114
case 'customer.edited':
116115
case 'customer.credit_balance_depleted':
@@ -122,6 +121,16 @@ export class OrbSync {
122121
await syncCustomers(this.postgresClient, [webhook.customer], webhook.created_at);
123122
break;
124123
}
124+
case 'customer.balance_transaction_created': {
125+
const webhook = parsedData as CustomerWebhook;
126+
127+
// Orb ocassionally sends multiple credit notes with the same timestamp at roughly the same time - this leads to possibly persisting an old customer balance
128+
// To prevent this, we will query the Orb customer via API to get the latest state
129+
const customer = await this.orb.customers.fetch(webhook.customer.id);
130+
131+
await syncCustomers(this.postgresClient, [customer], new Date().toISOString());
132+
break;
133+
}
125134

126135
case 'subscription.created':
127136
case 'subscription.ended':

0 commit comments

Comments
 (0)