chore: fix db instance
This commit is contained in:
parent
82d0fa1061
commit
181e478bc3
@ -1,15 +1,8 @@
|
||||
import {
|
||||
consumeCreditsAction,
|
||||
getCreditsAction,
|
||||
} from '@/actions/credits.action';
|
||||
import { ChartAreaInteractive } from '@/components/dashboard/chart-area-interactive';
|
||||
import { DashboardHeader } from '@/components/dashboard/dashboard-header';
|
||||
import { DataTable } from '@/components/dashboard/data-table';
|
||||
import { SectionCards } from '@/components/dashboard/section-cards';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import React from 'react';
|
||||
import { useState } from 'react';
|
||||
|
||||
import data from './data.json';
|
||||
|
||||
@ -21,44 +14,6 @@ import data from './data.json';
|
||||
*/
|
||||
export default function DashboardPage() {
|
||||
const t = useTranslations();
|
||||
const [credits, setCredits] = useState<number | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// get credits
|
||||
async function fetchCredits() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await getCreditsAction();
|
||||
if (
|
||||
typeof res === 'object' &&
|
||||
res &&
|
||||
'success' in res &&
|
||||
res.success &&
|
||||
'credits' in res
|
||||
)
|
||||
setCredits(res.credits as number);
|
||||
else if (typeof res === 'object' && res && 'error' in res)
|
||||
setError((res.error as string) || 'get credits failed');
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
// consume credits
|
||||
async function consumeCredits() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await consumeCreditsAction({ amount: 10 });
|
||||
if (typeof res === 'object' && res && 'success' in res && res.success)
|
||||
await fetchCredits();
|
||||
else if (typeof res === 'object' && res && 'error' in res)
|
||||
setError((res.error as string) || 'consume credits failed');
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
// first load credits
|
||||
React.useEffect(() => {
|
||||
fetchCredits();
|
||||
}, []);
|
||||
|
||||
const breadcrumbs = [
|
||||
{
|
||||
@ -69,21 +24,8 @@ export default function DashboardPage() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<DashboardHeader
|
||||
breadcrumbs={breadcrumbs}
|
||||
actions={
|
||||
<div className="flex items-center gap-2">
|
||||
<span>当前积分: {credits === null ? '加载中...' : credits}</span>
|
||||
<Button
|
||||
onClick={consumeCredits}
|
||||
disabled={loading || credits === null || credits < 10}
|
||||
>
|
||||
消费10积分
|
||||
</Button>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
{error && <div className="text-red-500 px-4">{error}</div>}
|
||||
<DashboardHeader breadcrumbs={breadcrumbs} />
|
||||
|
||||
<div className="flex flex-1 flex-col">
|
||||
<div className="@container/main flex flex-1 flex-col gap-2">
|
||||
<div className="flex flex-col gap-4 py-4 md:gap-6 md:py-6">
|
||||
|
@ -1,4 +1,4 @@
|
||||
import db from '@/db';
|
||||
import { getDb } from '@/db';
|
||||
import { creditTransaction, userCredit } from '@/db/schema';
|
||||
import { addDays, isAfter } from 'date-fns';
|
||||
import { and, asc, eq, or } from 'drizzle-orm';
|
||||
@ -11,6 +11,7 @@ import {
|
||||
|
||||
// Get user's current credit balance
|
||||
export async function getUserCredits(userId: string): Promise<number> {
|
||||
const db = await getDb();
|
||||
const record = await db
|
||||
.select()
|
||||
.from(userCredit)
|
||||
@ -34,6 +35,7 @@ async function logCreditTransaction(params: {
|
||||
if (!Number.isFinite(params.amount) || params.amount === 0) {
|
||||
throw new Error('Amount must be positive');
|
||||
}
|
||||
const db = await getDb();
|
||||
await db.insert(creditTransaction).values({
|
||||
id: crypto.randomUUID(),
|
||||
userId: params.userId,
|
||||
@ -76,6 +78,7 @@ export async function addCredits({
|
||||
// Process expired credits first
|
||||
await processExpiredCredits(userId);
|
||||
// Update user credit balance
|
||||
const db = await getDb();
|
||||
const current = await db
|
||||
.select()
|
||||
.from(userCredit)
|
||||
@ -138,6 +141,7 @@ export async function consumeCredits({
|
||||
const balance = await getUserCredits(userId);
|
||||
if (balance < amount) throw new Error('Insufficient credits');
|
||||
// FIFO consumption: consume from the earliest unexpired credits first
|
||||
const db = await getDb();
|
||||
const txs = await db
|
||||
.select()
|
||||
.from(creditTransaction)
|
||||
@ -200,6 +204,7 @@ export async function consumeCredits({
|
||||
export async function processExpiredCredits(userId: string) {
|
||||
const now = new Date();
|
||||
// Get all credit transactions without type EXPIRE
|
||||
const db = await getDb();
|
||||
const txs = await db
|
||||
.select()
|
||||
.from(creditTransaction)
|
||||
@ -263,6 +268,7 @@ export async function processExpiredCredits(userId: string) {
|
||||
// Add register gift credits
|
||||
export async function addRegisterGiftCredits(userId: string) {
|
||||
// Check if user has already received register gift credits
|
||||
const db = await getDb();
|
||||
const record = await db
|
||||
.select()
|
||||
.from(creditTransaction)
|
||||
@ -287,6 +293,7 @@ export async function addRegisterGiftCredits(userId: string) {
|
||||
// Add free monthly credits
|
||||
export async function addMonthlyFreeCredits(userId: string) {
|
||||
// Check last refresh time
|
||||
const db = await getDb();
|
||||
const record = await db
|
||||
.select()
|
||||
.from(userCredit)
|
||||
|
Loading…
Reference in New Issue
Block a user