Skip to content

Commit

Permalink
fix: ignore existing invoice in subscription loop (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Jan 8, 2024
1 parent ca8b7cc commit cc566a5
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"esbuild": "^0.14.49",
"eslint": "^8.20.0",
"pino-pretty": "^9.0.0",
"tsx": "^3.8.0",
"tsx": "^4.7.0",
"typescript": "^4.7.4",
"vite": "^4.2.0",
"vitest": "^0.29.3"
Expand Down
15 changes: 1 addition & 14 deletions packages/server/src/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,6 @@ export async function chargeSubscriptions(): Promise<void> {
const billingPeriod = { start: subscription.currentPeriodStart, end: subscription.currentPeriodEnd };

try {
const existingInvoices = await database.invoices.find({
date: { $gte: billingPeriod.start, $lte: billingPeriod.end },
subscription,
});
if (existingInvoices.length > 0) {
log.error(
{ subscriptionId: subscription._id, customerId: customer._id },
'Invoice for period already exists',
);
// TODO: should we just ignore this? currently this sets the subscription to error state?
throw new Error('Invoice for period already exists');
}

customer.invoiceCounter += 1;

const invoice = new Invoice({
Expand All @@ -149,7 +136,7 @@ export async function chargeSubscriptions(): Promise<void> {
customer: subscription.customer,
project,
status: 'pending',
date: new Date(),
date: now, // now = subscription.currentPeriodEnd + 1 hour
});

subscription.cleanupChanges();
Expand Down
Loading

0 comments on commit cc566a5

Please sign in to comment.