refactor: rename functions to remove 'IfNeed' suffix for clarity and consistency

This commit is contained in:
javayhu 2025-07-13 00:37:01 +08:00
parent 7af313868c
commit 52aeb2d61c
3 changed files with 11 additions and 11 deletions

View File

@ -397,12 +397,12 @@ export async function addRegisterGiftCredits(userId: string) {
* Add free monthly credits * Add free monthly credits
* @param userId - User ID * @param userId - User ID
*/ */
export async function addMonthlyFreeCreditsIfNeed(userId: string) { export async function addMonthlyFreeCredits(userId: string) {
const freePlan = Object.values(websiteConfig.price.plans).find( const freePlan = Object.values(websiteConfig.price.plans).find(
(plan) => plan.isFree (plan) => plan.isFree
); );
if (!freePlan) { if (!freePlan) {
console.log('addMonthlyFreeCreditsIfNeed, no free plan found'); console.log('addMonthlyFreeCredits, no free plan found');
return; return;
} }
if ( if (
@ -411,7 +411,7 @@ export async function addMonthlyFreeCreditsIfNeed(userId: string) {
!freePlan.credits?.amount !freePlan.credits?.amount
) { ) {
console.log( console.log(
'addMonthlyFreeCreditsIfNeed, plan disabled or credits disabled', 'addMonthlyFreeCredits, plan disabled or credits disabled',
freePlan.id freePlan.id
); );
return; return;
@ -448,7 +448,7 @@ export async function addMonthlyFreeCreditsIfNeed(userId: string) {
}); });
console.log( console.log(
`addMonthlyFreeCreditsIfNeed, ${credits} credits for user ${userId}, date: ${now.getFullYear()}-${now.getMonth() + 1}` `addMonthlyFreeCredits, ${credits} credits for user ${userId}, date: ${now.getFullYear()}-${now.getMonth() + 1}`
); );
} }
} }
@ -496,7 +496,7 @@ export async function addSubscriptionRenewalCredits(
* Add lifetime monthly credits * Add lifetime monthly credits
* @param userId - User ID * @param userId - User ID
*/ */
export async function addLifetimeMonthlyCreditsIfNeed(userId: string) { export async function addLifetimeMonthlyCredits(userId: string) {
const lifetimePlan = Object.values(websiteConfig.price.plans).find( const lifetimePlan = Object.values(websiteConfig.price.plans).find(
(plan) => plan.isLifetime (plan) => plan.isLifetime
); );
@ -601,12 +601,12 @@ export async function distributeCreditsToAllUsers() {
if (pricePlan?.isLifetime) { if (pricePlan?.isLifetime) {
// Lifetime user - add monthly credits // Lifetime user - add monthly credits
await addLifetimeMonthlyCreditsIfNeed(userRecord.userId); await addLifetimeMonthlyCredits(userRecord.userId);
} }
// Note: Subscription renewals are handled by Stripe webhooks, not here // Note: Subscription renewals are handled by Stripe webhooks, not here
} else { } else {
// User has no active subscription - add free monthly credits if enabled // User has no active subscription - add free monthly credits if enabled
await addMonthlyFreeCreditsIfNeed(userRecord.userId); await addMonthlyFreeCredits(userRecord.userId);
} }
processedCount++; processedCount++;

View File

@ -1,6 +1,6 @@
import { websiteConfig } from '@/config/website'; import { websiteConfig } from '@/config/website';
import { import {
addMonthlyFreeCreditsIfNeed, addMonthlyFreeCredits,
addRegisterGiftCredits, addRegisterGiftCredits,
} from '@/credits/credits'; } from '@/credits/credits';
import { getDb } from '@/db/index'; import { getDb } from '@/db/index';
@ -201,7 +201,7 @@ async function onCreateUser(user: User) {
freePlan?.credits?.amount > 0 freePlan?.credits?.amount > 0
) { ) {
try { try {
await addMonthlyFreeCreditsIfNeed(user.id); await addMonthlyFreeCredits(user.id);
const credits = freePlan.credits.amount; const credits = freePlan.credits.amount;
console.log( console.log(
`added free monthly credits for user ${user.id}, credits: ${credits}` `added free monthly credits for user ${user.id}, credits: ${credits}`

View File

@ -2,7 +2,7 @@ import { randomUUID } from 'crypto';
import { websiteConfig } from '@/config/website'; import { websiteConfig } from '@/config/website';
import { import {
addCredits, addCredits,
addLifetimeMonthlyCreditsIfNeed, addLifetimeMonthlyCredits,
addSubscriptionRenewalCredits, addSubscriptionRenewalCredits,
} from '@/credits/credits'; } from '@/credits/credits';
import { getCreditPackageById } from '@/credits/server'; import { getCreditPackageById } from '@/credits/server';
@ -796,7 +796,7 @@ export class StripeProvider implements PaymentProvider {
websiteConfig.price?.plans || {} websiteConfig.price?.plans || {}
).find((plan) => plan.isLifetime && plan.credits?.enable); ).find((plan) => plan.isLifetime && plan.credits?.enable);
if (lifetimePlan?.prices?.some((p) => p.priceId === priceId)) { if (lifetimePlan?.prices?.some((p) => p.priceId === priceId)) {
await addLifetimeMonthlyCreditsIfNeed(userId); await addLifetimeMonthlyCredits(userId);
} }
} }