refactor: update credit eligibility checks to use canAddCreditsByType function for improved clarity and maintainability

This commit is contained in:
javayhu 2025-08-24 10:03:35 +08:00
parent afdaeba2be
commit 2d0392db61

View File

@ -5,6 +5,7 @@ import { findPlanByPriceId, getAllPricePlans } from '@/lib/price-plan';
import { PlanIntervals } from '@/payment/types'; import { PlanIntervals } from '@/payment/types';
import { addDays } from 'date-fns'; import { addDays } from 'date-fns';
import { and, eq, gt, inArray, isNull, lt, not, or, sql } from 'drizzle-orm'; import { and, eq, gt, inArray, isNull, lt, not, or, sql } from 'drizzle-orm';
import { canAddCreditsByType } from './credits';
import { CREDIT_TRANSACTION_TYPE } from './types'; import { CREDIT_TRANSACTION_TYPE } from './types';
/** /**
@ -218,7 +219,6 @@ export async function batchAddMonthlyFreeCredits(userIds: string[]) {
const userCredits = await tx const userCredits = await tx
.select({ .select({
userId: userCredit.userId, userId: userCredit.userId,
lastRefreshAt: userCredit.lastRefreshAt,
currentCredits: userCredit.currentCredits, currentCredits: userCredit.currentCredits,
}) })
.from(userCredit) .from(userCredit)
@ -229,19 +229,17 @@ export async function batchAddMonthlyFreeCredits(userIds: string[]) {
userCredits.map((record) => [record.userId, record]) userCredits.map((record) => [record.userId, record])
); );
// Filter users who can receive credits // Check which users can receive credits based on transaction history
const eligibleUserIds = userIds.filter((userId) => { const eligibleUserIds: string[] = [];
const record = userCreditMap.get(userId); for (const userId of userIds) {
if (!record?.lastRefreshAt) { const canAdd = await canAddCreditsByType(
return true; // never added credits before userId,
} CREDIT_TRANSACTION_TYPE.MONTHLY_REFRESH
// different month or year means new month
const last = new Date(record.lastRefreshAt);
return (
now.getMonth() !== last.getMonth() ||
now.getFullYear() !== last.getFullYear()
); );
}); if (canAdd) {
eligibleUserIds.push(userId);
}
}
if (eligibleUserIds.length === 0) { if (eligibleUserIds.length === 0) {
console.log('batchAddMonthlyFreeCredits, no eligible users'); console.log('batchAddMonthlyFreeCredits, no eligible users');
@ -280,7 +278,6 @@ export async function batchAddMonthlyFreeCredits(userIds: string[]) {
id: randomUUID(), id: randomUUID(),
userId, userId,
currentCredits: credits, currentCredits: credits,
lastRefreshAt: now,
createdAt: now, createdAt: now,
updatedAt: now, updatedAt: now,
})); }));
@ -297,7 +294,6 @@ export async function batchAddMonthlyFreeCredits(userIds: string[]) {
.update(userCredit) .update(userCredit)
.set({ .set({
currentCredits: newBalance, currentCredits: newBalance,
lastRefreshAt: now,
updatedAt: now, updatedAt: now,
}) })
.where(eq(userCredit.userId, userId)); .where(eq(userCredit.userId, userId));
@ -362,7 +358,6 @@ export async function batchAddLifetimeMonthlyCredits(
const userCredits = await tx const userCredits = await tx
.select({ .select({
userId: userCredit.userId, userId: userCredit.userId,
lastRefreshAt: userCredit.lastRefreshAt,
currentCredits: userCredit.currentCredits, currentCredits: userCredit.currentCredits,
}) })
.from(userCredit) .from(userCredit)
@ -373,19 +368,17 @@ export async function batchAddLifetimeMonthlyCredits(
userCredits.map((record) => [record.userId, record]) userCredits.map((record) => [record.userId, record])
); );
// Filter users who can receive credits // Check which users can receive credits based on transaction history
const eligibleUserIds = userIdsForPrice.filter((userId: string) => { const eligibleUserIds: string[] = [];
const record = userCreditMap.get(userId); for (const userId of userIdsForPrice) {
if (!record?.lastRefreshAt) { const canAdd = await canAddCreditsByType(
return true; // never added credits before userId,
} CREDIT_TRANSACTION_TYPE.LIFETIME_MONTHLY
// different month or year means new month
const last = new Date(record.lastRefreshAt);
return (
now.getMonth() !== last.getMonth() ||
now.getFullYear() !== last.getFullYear()
); );
}); if (canAdd) {
eligibleUserIds.push(userId);
}
}
if (eligibleUserIds.length === 0) { if (eligibleUserIds.length === 0) {
console.log( console.log(
@ -426,7 +419,6 @@ export async function batchAddLifetimeMonthlyCredits(
id: randomUUID(), id: randomUUID(),
userId, userId,
currentCredits: credits, currentCredits: credits,
lastRefreshAt: now,
createdAt: now, createdAt: now,
updatedAt: now, updatedAt: now,
})); }));
@ -443,7 +435,6 @@ export async function batchAddLifetimeMonthlyCredits(
.update(userCredit) .update(userCredit)
.set({ .set({
currentCredits: newBalance, currentCredits: newBalance,
lastRefreshAt: now,
updatedAt: now, updatedAt: now,
}) })
.where(eq(userCredit.userId, userId)); .where(eq(userCredit.userId, userId));
@ -508,7 +499,6 @@ export async function batchAddYearlyUsersMonthlyCredits(
const userCredits = await tx const userCredits = await tx
.select({ .select({
userId: userCredit.userId, userId: userCredit.userId,
lastRefreshAt: userCredit.lastRefreshAt,
currentCredits: userCredit.currentCredits, currentCredits: userCredit.currentCredits,
}) })
.from(userCredit) .from(userCredit)
@ -519,19 +509,17 @@ export async function batchAddYearlyUsersMonthlyCredits(
userCredits.map((record) => [record.userId, record]) userCredits.map((record) => [record.userId, record])
); );
// Filter users who can receive credits // Check which users can receive credits based on transaction history
const eligibleUserIds = userIds.filter((userId) => { const eligibleUserIds: string[] = [];
const record = userCreditMap.get(userId); for (const userId of userIds) {
if (!record?.lastRefreshAt) { const canAdd = await canAddCreditsByType(
return true; // never added credits before userId,
} CREDIT_TRANSACTION_TYPE.SUBSCRIPTION_RENEWAL
// different month or year means new month
const last = new Date(record.lastRefreshAt);
return (
now.getMonth() !== last.getMonth() ||
now.getFullYear() !== last.getFullYear()
); );
}); if (canAdd) {
eligibleUserIds.push(userId);
}
}
if (eligibleUserIds.length === 0) { if (eligibleUserIds.length === 0) {
console.log( console.log(
@ -572,7 +560,6 @@ export async function batchAddYearlyUsersMonthlyCredits(
id: randomUUID(), id: randomUUID(),
userId, userId,
currentCredits: credits, currentCredits: credits,
lastRefreshAt: now,
createdAt: now, createdAt: now,
updatedAt: now, updatedAt: now,
})); }));
@ -589,7 +576,6 @@ export async function batchAddYearlyUsersMonthlyCredits(
.update(userCredit) .update(userCredit)
.set({ .set({
currentCredits: newBalance, currentCredits: newBalance,
lastRefreshAt: now,
updatedAt: now, updatedAt: now,
}) })
.where(eq(userCredit.userId, userId)); .where(eq(userCredit.userId, userId));