refactor: improve plan filtering in credits logic
This commit is contained in:
parent
52aeb2d61c
commit
f5e639bbc7
@ -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: {
|
||||
|
@ -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;
|
||||
|
@ -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 ||
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user