Add comprehensive recurring payments system with scheduling

- Add RecurringPayment model with flexible scheduling options
- Implement node-cron based scheduler for payment processing
- Create API endpoints for CRUD operations on recurring payments
- Add recurring payments management UI with create/edit forms
- Integrate scheduler initialization in hooks.server.ts
- Enhance payments/add form with progressive enhancement
- Add recurring payments button to main dashboard
- Improve server-side rendering for better performance

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-12 12:41:18 +02:00
parent 701434d532
commit 6ab395e98a
28 changed files with 4412 additions and 94 deletions

View File

@@ -0,0 +1,139 @@
import type { RequestHandler } from '@sveltejs/kit';
import { RecurringPayment } from '../../../../models/RecurringPayment';
import { dbConnect, dbDisconnect } from '../../../../utils/db';
import { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate, validateCronExpression } from '../../../../lib/utils/recurring';
export const GET: RequestHandler = async ({ locals, url }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
const limit = parseInt(url.searchParams.get('limit') || '50');
const offset = parseInt(url.searchParams.get('offset') || '0');
const activeOnly = url.searchParams.get('active') === 'true';
await dbConnect();
try {
const query: any = {};
if (activeOnly) {
query.isActive = true;
}
const recurringPayments = await RecurringPayment.find(query)
.sort({ nextExecutionDate: 1, createdAt: -1 })
.limit(limit)
.skip(offset)
.lean();
return json({ recurringPayments });
} catch (e) {
console.error('Error fetching recurring payments:', e);
throw error(500, 'Failed to fetch recurring payments');
} finally {
await dbDisconnect();
}
};
export const POST: RequestHandler = async ({ request, locals }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
const data = await request.json();
const {
title,
description,
amount,
paidBy,
category,
splitMethod,
splits,
frequency,
cronExpression,
startDate,
endDate
} = data;
if (!title || !amount || !paidBy || !splitMethod || !splits || !frequency) {
throw error(400, 'Missing required fields');
}
if (amount <= 0) {
throw error(400, 'Amount must be positive');
}
if (!['equal', 'full', 'proportional', 'personal_equal'].includes(splitMethod)) {
throw error(400, 'Invalid split method');
}
if (!['daily', 'weekly', 'monthly', 'custom'].includes(frequency)) {
throw error(400, 'Invalid frequency');
}
if (frequency === 'custom') {
if (!cronExpression || !validateCronExpression(cronExpression)) {
throw error(400, 'Valid cron expression required for custom frequency');
}
}
if (category && !['groceries', 'shopping', 'travel', 'restaurant', 'utilities', 'fun', 'settlement'].includes(category)) {
throw error(400, 'Invalid category');
}
// Validate personal + equal split method
if (splitMethod === 'personal_equal' && splits) {
const totalPersonal = splits.reduce((sum: number, split: any) => {
return sum + (parseFloat(split.personalAmount) || 0);
}, 0);
if (totalPersonal > amount) {
throw error(400, 'Personal amounts cannot exceed total payment amount');
}
}
await dbConnect();
try {
const recurringPaymentData = {
title,
description,
amount,
currency: 'CHF',
paidBy,
category: category || 'groceries',
splitMethod,
splits,
frequency,
cronExpression: frequency === 'custom' ? cronExpression : undefined,
startDate: startDate ? new Date(startDate) : new Date(),
endDate: endDate ? new Date(endDate) : undefined,
createdBy: auth.user.nickname,
isActive: true,
nextExecutionDate: new Date() // Will be calculated below
};
// Calculate the next execution date
recurringPaymentData.nextExecutionDate = calculateNextExecutionDate({
...recurringPaymentData,
frequency,
cronExpression
} as any, recurringPaymentData.startDate);
const recurringPayment = await RecurringPayment.create(recurringPaymentData);
return json({
success: true,
recurringPayment: recurringPayment._id
});
} catch (e) {
console.error('Error creating recurring payment:', e);
throw error(500, 'Failed to create recurring payment');
} finally {
await dbDisconnect();
}
};

View File

@@ -0,0 +1,184 @@
import type { RequestHandler } from '@sveltejs/kit';
import { RecurringPayment } from '../../../../../models/RecurringPayment';
import { dbConnect, dbDisconnect } from '../../../../../utils/db';
import { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate, validateCronExpression } from '../../../../../lib/utils/recurring';
import mongoose from 'mongoose';
export const GET: RequestHandler = async ({ params, locals }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
const { id } = params;
if (!id || !mongoose.Types.ObjectId.isValid(id)) {
throw error(400, 'Invalid payment ID');
}
await dbConnect();
try {
const recurringPayment = await RecurringPayment.findById(id).lean();
if (!recurringPayment) {
throw error(404, 'Recurring payment not found');
}
return json({ recurringPayment });
} catch (e) {
console.error('Error fetching recurring payment:', e);
throw error(500, 'Failed to fetch recurring payment');
} finally {
await dbDisconnect();
}
};
export const PUT: RequestHandler = async ({ params, request, locals }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
const { id } = params;
if (!id || !mongoose.Types.ObjectId.isValid(id)) {
throw error(400, 'Invalid payment ID');
}
const data = await request.json();
const {
title,
description,
amount,
paidBy,
category,
splitMethod,
splits,
frequency,
cronExpression,
startDate,
endDate,
isActive
} = data;
await dbConnect();
try {
const existingPayment = await RecurringPayment.findById(id);
if (!existingPayment) {
throw error(404, 'Recurring payment not found');
}
const updateData: any = {};
if (title !== undefined) updateData.title = title;
if (description !== undefined) updateData.description = description;
if (amount !== undefined) {
if (amount <= 0) {
throw error(400, 'Amount must be positive');
}
updateData.amount = amount;
}
if (paidBy !== undefined) updateData.paidBy = paidBy;
if (category !== undefined) {
if (!['groceries', 'shopping', 'travel', 'restaurant', 'utilities', 'fun', 'settlement'].includes(category)) {
throw error(400, 'Invalid category');
}
updateData.category = category;
}
if (splitMethod !== undefined) {
if (!['equal', 'full', 'proportional', 'personal_equal'].includes(splitMethod)) {
throw error(400, 'Invalid split method');
}
updateData.splitMethod = splitMethod;
}
if (splits !== undefined) {
updateData.splits = splits;
}
if (frequency !== undefined) {
if (!['daily', 'weekly', 'monthly', 'custom'].includes(frequency)) {
throw error(400, 'Invalid frequency');
}
updateData.frequency = frequency;
}
if (cronExpression !== undefined) {
if (frequency === 'custom' && !validateCronExpression(cronExpression)) {
throw error(400, 'Valid cron expression required for custom frequency');
}
updateData.cronExpression = frequency === 'custom' ? cronExpression : undefined;
}
if (startDate !== undefined) updateData.startDate = new Date(startDate);
if (endDate !== undefined) updateData.endDate = endDate ? new Date(endDate) : null;
if (isActive !== undefined) updateData.isActive = isActive;
// Validate personal + equal split method
if (splitMethod === 'personal_equal' && splits && amount) {
const totalPersonal = splits.reduce((sum: number, split: any) => {
return sum + (parseFloat(split.personalAmount) || 0);
}, 0);
if (totalPersonal > amount) {
throw error(400, 'Personal amounts cannot exceed total payment amount');
}
}
// Recalculate next execution date if frequency, cron expression, or start date changed
if (frequency !== undefined || cronExpression !== undefined || startDate !== undefined) {
const updatedPayment = { ...existingPayment.toObject(), ...updateData };
updateData.nextExecutionDate = calculateNextExecutionDate(
updatedPayment,
updateData.startDate || existingPayment.startDate
);
}
const recurringPayment = await RecurringPayment.findByIdAndUpdate(
id,
updateData,
{ new: true, runValidators: true }
);
return json({
success: true,
recurringPayment
});
} catch (e) {
console.error('Error updating recurring payment:', e);
if (e instanceof mongoose.Error.ValidationError) {
throw error(400, e.message);
}
throw error(500, 'Failed to update recurring payment');
} finally {
await dbDisconnect();
}
};
export const DELETE: RequestHandler = async ({ params, locals }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
const { id } = params;
if (!id || !mongoose.Types.ObjectId.isValid(id)) {
throw error(400, 'Invalid payment ID');
}
await dbConnect();
try {
const recurringPayment = await RecurringPayment.findByIdAndDelete(id);
if (!recurringPayment) {
throw error(404, 'Recurring payment not found');
}
return json({ success: true });
} catch (e) {
console.error('Error deleting recurring payment:', e);
throw error(500, 'Failed to delete recurring payment');
} finally {
await dbDisconnect();
}
};

View File

@@ -0,0 +1,124 @@
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 { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate } from '../../../../../lib/utils/recurring';
// This endpoint is designed to be called by a cron job or external scheduler
// It processes all recurring payments that are due for execution
export const POST: RequestHandler = async ({ request }) => {
// Optional: Add basic authentication or API key validation here
const authHeader = request.headers.get('authorization');
const expectedToken = process.env.CRON_API_TOKEN;
if (expectedToken && authHeader !== `Bearer ${expectedToken}`) {
throw error(401, 'Unauthorized');
}
await dbConnect();
try {
const now = new Date();
console.log(`[Cron] Starting recurring payments processing at ${now.toISOString()}`);
// Find all active recurring payments that are due
const duePayments = await RecurringPayment.find({
isActive: true,
nextExecutionDate: { $lte: now },
$or: [
{ endDate: { $exists: false } },
{ endDate: null },
{ endDate: { $gte: now } }
]
});
console.log(`[Cron] Found ${duePayments.length} due recurring payments`);
const results = [];
let successCount = 0;
let failureCount = 0;
for (const recurringPayment of duePayments) {
try {
console.log(`[Cron] Processing recurring payment: ${recurringPayment.title} (${recurringPayment._id})`);
// Create the payment
const payment = await Payment.create({
title: `${recurringPayment.title} (Auto)`,
description: `Automatically generated from recurring payment: ${recurringPayment.description || 'No description'}`,
amount: recurringPayment.amount,
currency: recurringPayment.currency,
paidBy: recurringPayment.paidBy,
date: now,
category: recurringPayment.category,
splitMethod: recurringPayment.splitMethod,
createdBy: recurringPayment.createdBy
});
// Create payment splits
const splitPromises = recurringPayment.splits.map((split) => {
return PaymentSplit.create({
paymentId: payment._id,
username: split.username,
amount: split.amount,
proportion: split.proportion,
personalAmount: split.personalAmount
});
});
await Promise.all(splitPromises);
// Calculate next execution date
const nextExecutionDate = calculateNextExecutionDate(recurringPayment, now);
// Update the recurring payment
await RecurringPayment.findByIdAndUpdate(recurringPayment._id, {
lastExecutionDate: now,
nextExecutionDate: nextExecutionDate
});
successCount++;
results.push({
recurringPaymentId: recurringPayment._id,
paymentId: payment._id,
title: recurringPayment.title,
amount: recurringPayment.amount,
nextExecution: nextExecutionDate,
success: true
});
console.log(`[Cron] Successfully processed: ${recurringPayment.title}, next execution: ${nextExecutionDate.toISOString()}`);
} catch (paymentError) {
console.error(`[Cron] Error processing recurring payment ${recurringPayment._id}:`, paymentError);
failureCount++;
results.push({
recurringPaymentId: recurringPayment._id,
title: recurringPayment.title,
amount: recurringPayment.amount,
success: false,
error: paymentError instanceof Error ? paymentError.message : 'Unknown error'
});
}
}
console.log(`[Cron] Completed processing. Success: ${successCount}, Failures: ${failureCount}`);
return json({
success: true,
timestamp: now.toISOString(),
processed: duePayments.length,
successful: successCount,
failed: failureCount,
results: results
});
} catch (e) {
console.error('[Cron] Error executing recurring payments:', e);
throw error(500, 'Failed to execute recurring payments');
} finally {
await dbDisconnect();
}
};

View File

@@ -0,0 +1,104 @@
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 { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate } from '../../../../../lib/utils/recurring';
export const POST: RequestHandler = async ({ locals }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
await dbConnect();
try {
const now = new Date();
// Find all active recurring payments that are due
const duePayments = await RecurringPayment.find({
isActive: true,
nextExecutionDate: { $lte: now },
$or: [
{ endDate: { $exists: false } },
{ endDate: null },
{ endDate: { $gte: now } }
]
});
const results = [];
for (const recurringPayment of duePayments) {
try {
// Create the payment
const payment = await Payment.create({
title: recurringPayment.title,
description: recurringPayment.description,
amount: recurringPayment.amount,
currency: recurringPayment.currency,
paidBy: recurringPayment.paidBy,
date: now,
category: recurringPayment.category,
splitMethod: recurringPayment.splitMethod,
createdBy: recurringPayment.createdBy
});
// Create payment splits
const splitPromises = recurringPayment.splits.map((split) => {
return PaymentSplit.create({
paymentId: payment._id,
username: split.username,
amount: split.amount,
proportion: split.proportion,
personalAmount: split.personalAmount
});
});
await Promise.all(splitPromises);
// Calculate next execution date
const nextExecutionDate = calculateNextExecutionDate(recurringPayment, now);
// Update the recurring payment
await RecurringPayment.findByIdAndUpdate(recurringPayment._id, {
lastExecutionDate: now,
nextExecutionDate: nextExecutionDate
});
results.push({
recurringPaymentId: recurringPayment._id,
paymentId: payment._id,
title: recurringPayment.title,
amount: recurringPayment.amount,
nextExecution: nextExecutionDate,
success: true
});
} catch (paymentError) {
console.error(`Error executing recurring payment ${recurringPayment._id}:`, paymentError);
results.push({
recurringPaymentId: recurringPayment._id,
title: recurringPayment.title,
amount: recurringPayment.amount,
success: false,
error: paymentError instanceof Error ? paymentError.message : 'Unknown error'
});
}
}
return json({
success: true,
executed: results.filter(r => r.success).length,
failed: results.filter(r => !r.success).length,
results
});
} catch (e) {
console.error('Error executing recurring payments:', e);
throw error(500, 'Failed to execute recurring payments');
} finally {
await dbDisconnect();
}
};

View File

@@ -0,0 +1,57 @@
import type { RequestHandler } from '@sveltejs/kit';
import { error, json } from '@sveltejs/kit';
import { recurringPaymentScheduler } from '../../../../../lib/server/scheduler';
export const GET: RequestHandler = async ({ locals }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
try {
const status = recurringPaymentScheduler.getStatus();
return json({
success: true,
scheduler: status,
message: status.isScheduled ? 'Scheduler is running' : 'Scheduler is stopped'
});
} catch (e) {
console.error('Error getting scheduler status:', e);
throw error(500, 'Failed to get scheduler status');
}
};
export const POST: RequestHandler = async ({ request, locals }) => {
const auth = await locals.auth();
if (!auth || !auth.user?.nickname) {
throw error(401, 'Not logged in');
}
try {
const body = await request.json();
const { action } = body;
switch (action) {
case 'execute':
console.log(`[API] Manual execution requested by ${auth.user.nickname}`);
await recurringPaymentScheduler.executeNow();
return json({
success: true,
message: 'Manual execution completed'
});
case 'status':
const status = recurringPaymentScheduler.getStatus();
return json({
success: true,
scheduler: status
});
default:
throw error(400, 'Invalid action. Use "execute" or "status"');
}
} catch (e) {
console.error('Error in scheduler API:', e);
throw error(500, 'Scheduler operation failed');
}
};