Skip to content

Commit

Permalink
fix: prevent subscriptions from failing as of missing payment methods (
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Dec 7, 2023
1 parent f18bee9 commit ca8b7cc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/api/endpoints/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export async function invoiceEndpoints(server: FastifyInstance): Promise<void> {
return reply.code(400).send({ error: 'Missing invoiceId' });
}

const invoice = await database.invoices.findOne({ _id: invoiceId, project }, { populate: ['items'] });
const invoice = await database.invoices.findOne({ _id: invoiceId, project }, { populate: ['items', 'project'] });
if (!invoice) {
return reply.code(404).send({ error: 'Invoice not found' });
}
Expand Down
6 changes: 1 addition & 5 deletions packages/server/src/api/endpoints/payment_method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,13 @@ describe('Payment-method endpoints', () => {
const testData = getFixtures();

const paymentMethodData = <PaymentMethod>{
_id: '123',
_id: '1234',
type: 'credit-card',
name: 'VISA',
customer: testData.customer,
paymentProviderId: '123',
};

const persistAndFlush = vi.fn();
const removeAndFlush = vi.fn();

vi.spyOn(database, 'database', 'get').mockReturnValue({
Expand All @@ -193,7 +192,6 @@ describe('Payment-method endpoints', () => {
},
},
em: {
persistAndFlush,
removeAndFlush,
},
} as unknown as database.Database);
Expand All @@ -217,7 +215,5 @@ describe('Payment-method endpoints', () => {
expect(paymentMethodResponse).toStrictEqual({ ok: true });
expect(removeAndFlush).toBeCalledTimes(1);
expect(removeAndFlush).toHaveBeenCalledWith(paymentMethodData);

expect(persistAndFlush).toBeCalledTimes(1);
});
});
3 changes: 1 addition & 2 deletions packages/server/src/api/endpoints/payment_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ export async function paymentMethodEndpoints(server: FastifyInstance): Promise<v
}

if (paymentMethod.customer.activePaymentMethod?._id === paymentMethod._id) {
paymentMethod.customer.activePaymentMethod = undefined;
await database.em.persistAndFlush(paymentMethod.customer);
return reply.code(400).send({ error: 'Cannot delete active payment-method' });
}

await database.em.removeAndFlush(paymentMethod);
Expand Down
8 changes: 8 additions & 0 deletions packages/server/src/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ export async function chargePendingInvoices(): Promise<void> {
} catch (e) {
log.error('Error while invoice charging:', e);
invoice.status = 'failed';

if (invoice.subscription) {
const { subscription } = invoice;
subscription.status = 'error';
subscription.error = (e as Error)?.message || (e as string);
await database.em.persistAndFlush([subscription]);
}

await database.em.persistAndFlush([invoice]);
}
}
Expand Down

0 comments on commit ca8b7cc

Please sign in to comment.