fix: implement persistent MongoDB connections and resolve race conditions

- Replace connect/disconnect pattern with persistent connection pool
- Add explicit database initialization on server startup
- Remove all dbDisconnect() calls from API endpoints to prevent race conditions
- Fix MongoNotConnectedError when scheduler runs concurrently with API requests
- Add connection pooling with proper MongoDB driver options
- Add safety check for recipes array in favorites utility

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-14 19:53:55 +02:00
parent 08d7d8541b
commit c8e542eec8
28 changed files with 198 additions and 111 deletions

View File

@@ -1,6 +1,6 @@
import type { RequestHandler } from '@sveltejs/kit';
import { RecurringPayment } from '../../../../models/RecurringPayment';
import { dbConnect, dbDisconnect } from '../../../../utils/db';
import { dbConnect } from '../../../../utils/db';
import { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate, validateCronExpression } from '../../../../lib/utils/recurring';
@@ -33,7 +33,7 @@ export const GET: RequestHandler = async ({ locals, url }) => {
console.error('Error fetching recurring payments:', e);
throw error(500, 'Failed to fetch recurring payments');
} finally {
await dbDisconnect();
// Connection will be reused
}
};
@@ -134,6 +134,6 @@ export const POST: RequestHandler = async ({ request, locals }) => {
console.error('Error creating recurring payment:', e);
throw error(500, 'Failed to create recurring payment');
} finally {
await dbDisconnect();
// Connection will be reused
}
};

View File

@@ -1,6 +1,6 @@
import type { RequestHandler } from '@sveltejs/kit';
import { RecurringPayment } from '../../../../../models/RecurringPayment';
import { dbConnect, dbDisconnect } from '../../../../../utils/db';
import { dbConnect } from '../../../../../utils/db';
import { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate, validateCronExpression } from '../../../../../lib/utils/recurring';
import mongoose from 'mongoose';
@@ -30,7 +30,7 @@ export const GET: RequestHandler = async ({ params, locals }) => {
console.error('Error fetching recurring payment:', e);
throw error(500, 'Failed to fetch recurring payment');
} finally {
await dbDisconnect();
// Connection will be reused
}
};
@@ -149,7 +149,7 @@ export const PUT: RequestHandler = async ({ params, request, locals }) => {
}
throw error(500, 'Failed to update recurring payment');
} finally {
await dbDisconnect();
// Connection will be reused
}
};
@@ -179,6 +179,6 @@ export const DELETE: RequestHandler = async ({ params, locals }) => {
console.error('Error deleting recurring payment:', e);
throw error(500, 'Failed to delete recurring payment');
} finally {
await dbDisconnect();
// Connection will be reused
}
};

View File

@@ -2,7 +2,7 @@ import type { RequestHandler } from '@sveltejs/kit';
import { RecurringPayment } from '../../../../../models/RecurringPayment';
import { Payment } from '../../../../../models/Payment';
import { PaymentSplit } from '../../../../../models/PaymentSplit';
import { dbConnect, dbDisconnect } from '../../../../../utils/db';
import { dbConnect } from '../../../../../utils/db';
import { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate } from '../../../../../lib/utils/recurring';
@@ -119,6 +119,6 @@ export const POST: RequestHandler = async ({ request }) => {
console.error('[Cron] Error executing recurring payments:', e);
throw error(500, 'Failed to execute recurring payments');
} finally {
await dbDisconnect();
// Connection will be reused
}
};

View File

@@ -2,9 +2,10 @@ import type { RequestHandler } from '@sveltejs/kit';
import { RecurringPayment } from '../../../../../models/RecurringPayment';
import { Payment } from '../../../../../models/Payment';
import { PaymentSplit } from '../../../../../models/PaymentSplit';
import { dbConnect, dbDisconnect } from '../../../../../utils/db';
import { dbConnect } from '../../../../../utils/db';
import { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate } from '../../../../../lib/utils/recurring';
import { convertToCHF } from '../../../../../lib/utils/currency';
export const POST: RequestHandler = async ({ locals }) => {
const auth = await locals.auth();
@@ -32,12 +33,35 @@ export const POST: RequestHandler = async ({ locals }) => {
for (const recurringPayment of duePayments) {
try {
// Handle currency conversion for execution date
let finalAmount = recurringPayment.amount;
let originalAmount: number | undefined;
let exchangeRate: number | undefined;
if (recurringPayment.currency !== 'CHF') {
try {
const conversion = await convertToCHF(
recurringPayment.amount,
recurringPayment.currency,
now.toISOString()
);
finalAmount = conversion.convertedAmount;
originalAmount = recurringPayment.amount;
exchangeRate = conversion.exchangeRate;
} catch (conversionError) {
console.error(`Currency conversion failed for recurring payment ${recurringPayment._id}:`, conversionError);
// Continue with original amount if conversion fails
}
}
// Create the payment
const payment = await Payment.create({
title: recurringPayment.title,
description: recurringPayment.description,
amount: recurringPayment.amount,
amount: finalAmount,
currency: recurringPayment.currency,
originalAmount,
exchangeRate,
paidBy: recurringPayment.paidBy,
date: now,
category: recurringPayment.category,
@@ -45,15 +69,31 @@ export const POST: RequestHandler = async ({ locals }) => {
createdBy: recurringPayment.createdBy
});
// Create payment splits
const splitPromises = recurringPayment.splits.map((split) => {
return PaymentSplit.create({
// Convert split amounts to CHF if needed
const convertedSplits = recurringPayment.splits.map((split) => {
let convertedAmount = split.amount || 0;
let convertedPersonalAmount = split.personalAmount;
// Convert amounts if we have a foreign currency and exchange rate
if (recurringPayment.currency !== 'CHF' && exchangeRate && split.amount) {
convertedAmount = split.amount * exchangeRate;
if (split.personalAmount) {
convertedPersonalAmount = split.personalAmount * exchangeRate;
}
}
return {
paymentId: payment._id,
username: split.username,
amount: split.amount,
amount: convertedAmount,
proportion: split.proportion,
personalAmount: split.personalAmount
});
personalAmount: convertedPersonalAmount
};
});
// Create payment splits
const splitPromises = convertedSplits.map((split) => {
return PaymentSplit.create(split);
});
await Promise.all(splitPromises);
@@ -99,6 +139,6 @@ export const POST: RequestHandler = async ({ locals }) => {
console.error('Error executing recurring payments:', e);
throw error(500, 'Failed to execute recurring payments');
} finally {
await dbDisconnect();
// Connection will be reused
}
};