refactor: improve plan filtering in credits logic
This commit is contained in:
parent
52aeb2d61c
commit
f5e639bbc7
@ -1,7 +1,7 @@
|
|||||||
'use server';
|
'use server';
|
||||||
|
|
||||||
import { getDb } from '@/db';
|
import { getDb } from '@/db';
|
||||||
import { creditTransaction, user } from '@/db/schema';
|
import { creditTransaction } from '@/db/schema';
|
||||||
import { getSession } from '@/lib/server';
|
import { getSession } from '@/lib/server';
|
||||||
import { and, asc, desc, eq, ilike, or, sql } from 'drizzle-orm';
|
import { and, asc, desc, eq, ilike, or, sql } from 'drizzle-orm';
|
||||||
import { createSafeActionClient } from 'next-safe-action';
|
import { createSafeActionClient } from 'next-safe-action';
|
||||||
@ -60,6 +60,7 @@ export const getCreditTransactionsAction = actionClient
|
|||||||
or(
|
or(
|
||||||
ilike(creditTransaction.type, `%${search}%`),
|
ilike(creditTransaction.type, `%${search}%`),
|
||||||
ilike(creditTransaction.amount, `%${search}%`),
|
ilike(creditTransaction.amount, `%${search}%`),
|
||||||
|
ilike(creditTransaction.remainingAmount, `%${search}%`),
|
||||||
ilike(creditTransaction.paymentId, `%${search}%`),
|
ilike(creditTransaction.paymentId, `%${search}%`),
|
||||||
ilike(creditTransaction.description, `%${search}%`)
|
ilike(creditTransaction.description, `%${search}%`)
|
||||||
)
|
)
|
||||||
@ -76,7 +77,7 @@ export const getCreditTransactionsAction = actionClient
|
|||||||
const sortDirection = sortConfig?.desc ? desc : asc;
|
const sortDirection = sortConfig?.desc ? desc : asc;
|
||||||
|
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
let [items, [{ count }]] = await Promise.all([
|
const [items, [{ count }]] = await Promise.all([
|
||||||
db
|
db
|
||||||
.select({
|
.select({
|
||||||
id: creditTransaction.id,
|
id: creditTransaction.id,
|
||||||
@ -103,14 +104,6 @@ export const getCreditTransactionsAction = actionClient
|
|||||||
.where(where),
|
.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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
|
@ -44,8 +44,13 @@ export const getUsersAction = actionClient
|
|||||||
try {
|
try {
|
||||||
const { pageIndex, pageSize, search, sorting } = parsedInput;
|
const { pageIndex, pageSize, search, sorting } = parsedInput;
|
||||||
|
|
||||||
|
// search by name, email, and customerId
|
||||||
const where = search
|
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;
|
: undefined;
|
||||||
|
|
||||||
const offset = pageIndex * pageSize;
|
const offset = pageIndex * pageSize;
|
||||||
|
@ -399,7 +399,7 @@ export async function addRegisterGiftCredits(userId: string) {
|
|||||||
*/
|
*/
|
||||||
export async function addMonthlyFreeCredits(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 && !plan.disabled
|
||||||
);
|
);
|
||||||
if (!freePlan) {
|
if (!freePlan) {
|
||||||
console.log('addMonthlyFreeCredits, no free plan found');
|
console.log('addMonthlyFreeCredits, no free plan found');
|
||||||
@ -498,7 +498,7 @@ export async function addSubscriptionRenewalCredits(
|
|||||||
*/
|
*/
|
||||||
export async function addLifetimeMonthlyCredits(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 && !plan.disabled
|
||||||
);
|
);
|
||||||
if (
|
if (
|
||||||
!lifetimePlan ||
|
!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
|
// If the plan is lifetime and credits are enabled, add lifetime monthly credits if needed
|
||||||
const lifetimePlan = Object.values(
|
const lifetimePlan = Object.values(
|
||||||
websiteConfig.price?.plans || {}
|
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)) {
|
if (lifetimePlan?.prices?.some((p) => p.priceId === priceId)) {
|
||||||
await addLifetimeMonthlyCredits(userId);
|
await addLifetimeMonthlyCredits(userId);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user