feat(credits): solde IA unique (option M), packs Stripe et polish billing
Un pot de crédits global (BASIC 100 / PRO 1 000 / BUSINESS 4 000) remplace les seaux par fonction côté débit. Packs one-shot S/M/L, admin sans seaux legacy, usage multi-features, hints de coût, anti-flash thème et hydratation admin. Inclut aussi le pipeline slides renforcé et la page publique polishée.
This commit is contained in:
@@ -186,3 +186,60 @@ export async function updateUserSubscription(userId: string, tier: string) {
|
||||
throw new Error('Failed to update subscription')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remet à zéro les crédits IA + anciens compteurs feature du mois pour un utilisateur.
|
||||
* @param feature (optionnel, legacy) — le reset crédits est global ; feature ignorée pour le solde.
|
||||
*/
|
||||
export async function resetUserQuotas(userId: string, feature?: string) {
|
||||
const session = await checkAdmin()
|
||||
|
||||
if (!userId?.trim()) {
|
||||
throw new Error('Identifiant utilisateur manquant')
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { id: true, email: true },
|
||||
})
|
||||
if (!user) {
|
||||
throw new Error('Utilisateur introuvable')
|
||||
}
|
||||
|
||||
// Nouveau système : solde de crédits global
|
||||
const { adminResetCredits } = await import('@/lib/credits')
|
||||
const balance = await adminResetCredits(userId, { clearPurchased: false })
|
||||
|
||||
// Ancien système feature (compat / métriques)
|
||||
const { resetUserUsageForCurrentPeriod } = await import('@/lib/entitlements')
|
||||
const legacy = await resetUserUsageForCurrentPeriod(
|
||||
userId,
|
||||
feature ? { features: [feature] } : undefined,
|
||||
)
|
||||
|
||||
const { logAuditEventAsync } = await import('@/lib/audit-log')
|
||||
await logAuditEventAsync({
|
||||
userId: session.user?.id,
|
||||
action: 'QUOTA_RESET',
|
||||
resource: userId,
|
||||
metadata: {
|
||||
targetUserId: userId,
|
||||
targetEmail: user.email,
|
||||
period: balance.period,
|
||||
creditsRemaining: balance.unlimited ? 'unlimited' : balance.totalRemaining,
|
||||
legacyFeaturesReset: legacy.featuresReset,
|
||||
scope: feature || 'all',
|
||||
},
|
||||
})
|
||||
|
||||
revalidatePath('/admin')
|
||||
revalidatePath('/admin/users')
|
||||
return {
|
||||
success: true as const,
|
||||
period: balance.period,
|
||||
featuresReset: legacy.featuresReset,
|
||||
redisDeleted: legacy.redisDeleted,
|
||||
dbUpdated: legacy.dbUpdated,
|
||||
creditsRemaining: balance.unlimited ? null : balance.totalRemaining,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user