chore: update credit related functions (2)

This commit is contained in:
javayhu 2025-05-29 01:21:04 +08:00
parent dae7a3b0e8
commit b30355dfe5

View File

@ -28,6 +28,12 @@ async function logCreditTransaction(params: {
paymentId?: string; paymentId?: string;
expirationDate?: Date; expirationDate?: Date;
}) { }) {
if (!params.userId || !params.type || !params.description) {
throw new Error('Invalid params');
}
if (!Number.isFinite(params.amount) || params.amount === 0) {
throw new Error('Amount must be positive');
}
await db.insert(creditTransaction).values({ await db.insert(creditTransaction).values({
id: crypto.randomUUID(), id: crypto.randomUUID(),
userId: params.userId, userId: params.userId,
@ -58,6 +64,15 @@ export async function addCredits({
paymentId?: string; paymentId?: string;
expireDays?: number; expireDays?: number;
}) { }) {
if (!userId || !type || !description) {
throw new Error('Invalid params');
}
if (!Number.isFinite(amount) || amount <= 0) {
throw new Error('Amount must be positive');
}
if (!Number.isFinite(expireDays) || expireDays <= 0) {
throw new Error('expireDays must be positive');
}
// Process expired credits first // Process expired credits first
await processExpiredCredits(userId); await processExpiredCredits(userId);
// Update user credit balance // Update user credit balance
@ -111,6 +126,12 @@ export async function consumeCredits({
amount: number; amount: number;
description: string; description: string;
}) { }) {
if (!userId || !description) {
throw new Error('Invalid params');
}
if (!Number.isFinite(amount) || amount <= 0) {
throw new Error('Amount must be positive');
}
// Process expired credits first // Process expired credits first
await processExpiredCredits(userId); await processExpiredCredits(userId);
// Check balance // Check balance