From f5e639bbc70750bc5b0b9013243be76e4c468831 Mon Sep 17 00:00:00 2001 From: javayhu Date: Sun, 13 Jul 2025 00:53:31 +0800 Subject: [PATCH] refactor: improve plan filtering in credits logic --- src/actions/get-credit-transactions.ts | 13 +++---------- src/actions/get-users.ts | 7 ++++++- src/credits/credits.ts | 4 ++-- src/payment/provider/stripe.ts | 4 +++- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/actions/get-credit-transactions.ts b/src/actions/get-credit-transactions.ts index f346b9e..ded99f0 100644 --- a/src/actions/get-credit-transactions.ts +++ b/src/actions/get-credit-transactions.ts @@ -1,7 +1,7 @@ 'use server'; import { getDb } from '@/db'; -import { creditTransaction, user } from '@/db/schema'; +import { creditTransaction } from '@/db/schema'; import { getSession } from '@/lib/server'; import { and, asc, desc, eq, ilike, or, sql } from 'drizzle-orm'; import { createSafeActionClient } from 'next-safe-action'; @@ -60,6 +60,7 @@ export const getCreditTransactionsAction = actionClient or( ilike(creditTransaction.type, `%${search}%`), ilike(creditTransaction.amount, `%${search}%`), + ilike(creditTransaction.remainingAmount, `%${search}%`), ilike(creditTransaction.paymentId, `%${search}%`), ilike(creditTransaction.description, `%${search}%`) ) @@ -76,7 +77,7 @@ export const getCreditTransactionsAction = actionClient const sortDirection = sortConfig?.desc ? desc : asc; const db = await getDb(); - let [items, [{ count }]] = await Promise.all([ + const [items, [{ count }]] = await Promise.all([ db .select({ id: creditTransaction.id, @@ -103,14 +104,6 @@ export const getCreditTransactionsAction = actionClient .where(where), ]); - // hide user data in demo website - if (process.env.NEXT_PUBLIC_DEMO_WEBSITE === 'true') { - items = items.map((item) => ({ - ...item, - paymentId: item.paymentId ? 'pi_demo123456' : null, - })); - } - return { success: true, data: { diff --git a/src/actions/get-users.ts b/src/actions/get-users.ts index f01702a..afd68c7 100644 --- a/src/actions/get-users.ts +++ b/src/actions/get-users.ts @@ -44,8 +44,13 @@ export const getUsersAction = actionClient try { const { pageIndex, pageSize, search, sorting } = parsedInput; + // search by name, email, and customerId const where = search - ? or(ilike(user.name, `%${search}%`), ilike(user.email, `%${search}%`)) + ? or( + ilike(user.name, `%${search}%`), + ilike(user.email, `%${search}%`), + ilike(user.customerId, `%${search}%`) + ) : undefined; const offset = pageIndex * pageSize; diff --git a/src/credits/credits.ts b/src/credits/credits.ts index fb9cb86..00be186 100644 --- a/src/credits/credits.ts +++ b/src/credits/credits.ts @@ -399,7 +399,7 @@ export async function addRegisterGiftCredits(userId: string) { */ export async function addMonthlyFreeCredits(userId: string) { const freePlan = Object.values(websiteConfig.price.plans).find( - (plan) => plan.isFree + (plan) => plan.isFree && !plan.disabled ); if (!freePlan) { console.log('addMonthlyFreeCredits, no free plan found'); @@ -498,7 +498,7 @@ export async function addSubscriptionRenewalCredits( */ export async function addLifetimeMonthlyCredits(userId: string) { const lifetimePlan = Object.values(websiteConfig.price.plans).find( - (plan) => plan.isLifetime + (plan) => plan.isLifetime && !plan.disabled ); if ( !lifetimePlan || diff --git a/src/payment/provider/stripe.ts b/src/payment/provider/stripe.ts index 90cf270..c917527 100644 --- a/src/payment/provider/stripe.ts +++ b/src/payment/provider/stripe.ts @@ -794,7 +794,9 @@ export class StripeProvider implements PaymentProvider { // If the plan is lifetime and credits are enabled, add lifetime monthly credits if needed const lifetimePlan = Object.values( websiteConfig.price?.plans || {} - ).find((plan) => plan.isLifetime && plan.credits?.enable); + ).find( + (plan) => plan.isLifetime && !plan.disabled && plan.credits?.enable + ); if (lifetimePlan?.prices?.some((p) => p.priceId === priceId)) { await addLifetimeMonthlyCredits(userId); }