feat: initial commit — backend API + student cabinet frontend

- Go backend: auth (JWT), points earn/spend, QR token generation,
  partners, admin grant/stats endpoints with chi router
- Next.js 14 frontend: login, student dashboard, transaction history,
  QR display, partners list
- PostgreSQL migrations (4 tables), Redis cache, Docker Compose
- CORS middleware, role-based route protection, Zustand auth store

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
emil
2026-05-01 10:03:27 +03:00
co-authored by Claude Sonnet 4.6
commit 50b3c4198a
80 changed files with 10579 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
'use client';
import { useRouter } from 'next/navigation';
import { Card, Button } from '@/components/ui';
import { formatPoints } from '@/lib/utils';
interface BalanceCardProps {
balance: number;
name: string;
}
export function BalanceCard({ balance, name }: BalanceCardProps) {
const router = useRouter();
return (
<Card className="bg-gradient-to-br from-blue-700 to-blue-900 text-white ring-0 shadow-lg">
<p className="text-sm text-blue-200">{name}</p>
<p className="mt-2 text-5xl font-bold tracking-tight">{formatPoints(balance)}</p>
<p className="mt-1 text-sm text-blue-300">Доступно поинтов</p>
<Button
variant="secondary"
className="mt-5 bg-white/10 text-white hover:bg-white/20 border border-white/20"
onClick={() => router.push('/qr')}
>
Показать QR
</Button>
</Card>
);
}
+18
View File
@@ -0,0 +1,18 @@
import type { Partner } from '@/lib/types';
import { Card } from '@/components/ui';
interface PartnerCardProps {
partner: Partner;
}
export function PartnerCard({ partner }: PartnerCardProps) {
return (
<Card className="flex flex-col gap-1">
<p className="font-semibold text-white">{partner.name}</p>
<p className="text-sm text-gray-400">{partner.address}</p>
<p className="mt-2 inline-flex items-center self-start rounded-full bg-blue-900/40 px-2.5 py-0.5 text-xs font-medium text-blue-300">
До {partner.max_spend_pct}% поинтами
</p>
</Card>
);
}
+96
View File
@@ -0,0 +1,96 @@
'use client';
import { useState, useEffect, useCallback } from 'react';
import { QRCodeSVG } from 'qrcode.react';
import { Button, Spinner } from '@/components/ui';
import { api } from '@/lib/api';
import type { QRResponse } from '@/lib/types';
const QR_TTL_SECONDS = 300; // 5 minutes, matches backend JWT TTL
export function QRDisplay() {
const [token, setToken] = useState<string | null>(null);
const [secondsLeft, setSecondsLeft] = useState(QR_TTL_SECONDS);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchToken = useCallback(async () => {
setLoading(true);
setError(null);
try {
const data = await api.get<QRResponse>('/api/v1/me/qr');
setToken(data.token);
setSecondsLeft(QR_TTL_SECONDS);
} catch (e) {
setError(e instanceof Error ? e.message : 'Не удалось получить QR-код');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchToken();
}, [fetchToken]);
// Countdown tick — stops at 0.
useEffect(() => {
if (!token || secondsLeft <= 0) return;
const id = setInterval(() => setSecondsLeft((s) => s - 1), 1000);
return () => clearInterval(id);
}, [token, secondsLeft]);
const expired = secondsLeft <= 0;
const minutes = Math.floor(secondsLeft / 60);
const seconds = secondsLeft % 60;
const countdownText = `${minutes}:${String(seconds).padStart(2, '0')}`;
if (loading) {
return (
<div className="flex flex-col items-center gap-4 py-12">
<Spinner size="lg" />
<p className="text-sm text-gray-500">Генерируем QR-код…</p>
</div>
);
}
if (error) {
return (
<div className="flex flex-col items-center gap-4 py-12">
<p className="text-sm text-red-400">{error}</p>
<Button onClick={fetchToken}>Попробовать снова</Button>
</div>
);
}
return (
<div className="flex flex-col items-center gap-5">
{/* QR code always has a white bg — required for scanner contrast */}
<div
className={`rounded-2xl bg-white p-5 shadow-xl transition-opacity ${expired ? 'opacity-20' : ''}`}
>
{token && !expired ? (
<QRCodeSVG value={token} size={220} level="H" includeMargin={false} />
) : (
<div className="flex h-[220px] w-[220px] items-center justify-center">
<p className="text-sm text-gray-400">QR-код истёк</p>
</div>
)}
</div>
{!expired ? (
<p className="text-sm text-gray-400">
Действителен ещё{' '}
<span
className={`font-semibold tabular-nums ${secondsLeft < 60 ? 'text-red-400' : 'text-gray-200'}`}
>
{countdownText}
</span>
</p>
) : (
<Button onClick={fetchToken} isLoading={loading}>
Обновить
</Button>
)}
</div>
);
}
+45
View File
@@ -0,0 +1,45 @@
import type { Transaction } from '@/lib/types';
import { Badge, Spinner } from '@/components/ui';
import { formatDate, formatTransactionAmount } from '@/lib/utils';
interface TransactionListProps {
transactions: Transaction[];
isLoading: boolean;
}
export function TransactionList({ transactions, isLoading }: TransactionListProps) {
if (isLoading) {
return (
<div className="flex justify-center py-10">
<Spinner />
</div>
);
}
if (transactions.length === 0) {
return (
<p className="py-8 text-center text-sm text-gray-500">Пока нет операций</p>
);
}
return (
<ul className="divide-y divide-gray-700">
{transactions.map((tx) => (
<li key={tx.id} className="flex items-center gap-3 py-3">
<Badge type={tx.type} />
<div className="min-w-0 flex-1">
<p className="truncate text-sm font-medium text-gray-100">
{tx.description || (tx.type === 'spend' ? 'Оплата у партнёра' : 'Начисление поинтов')}
</p>
<p className="text-xs text-gray-500">{formatDate(tx.created_at)}</p>
</div>
<span
className={`shrink-0 text-sm font-semibold tabular-nums ${tx.amount >= 0 ? 'text-green-400' : 'text-red-400'}`}
>
{formatTransactionAmount(tx.amount)}
</span>
</li>
))}
</ul>
);
}
+29
View File
@@ -0,0 +1,29 @@
import type { TransactionType } from '@/lib/types';
const TYPE_LABELS: Record<TransactionType, string> = {
earn: 'Начисление',
spend: 'Списание',
admin_grant: 'Начисление',
expire: 'Сгорание',
};
const TYPE_CLASSES: Record<TransactionType, string> = {
earn: 'bg-green-900/50 text-green-400',
spend: 'bg-red-900/50 text-red-400',
admin_grant: 'bg-blue-900/50 text-blue-400',
expire: 'bg-gray-700 text-gray-400',
};
interface BadgeProps {
type: TransactionType;
}
export function Badge({ type }: BadgeProps) {
return (
<span
className={`inline-flex shrink-0 items-center rounded-full px-2 py-0.5 text-xs font-medium ${TYPE_CLASSES[type]}`}
>
{TYPE_LABELS[type]}
</span>
);
}
+36
View File
@@ -0,0 +1,36 @@
import { type ButtonHTMLAttributes } from 'react';
type Variant = 'primary' | 'secondary' | 'ghost';
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
variant?: Variant;
isLoading?: boolean;
}
const VARIANT_CLASSES: Record<Variant, string> = {
primary: 'bg-blue-600 text-white hover:bg-blue-500 disabled:bg-blue-800 disabled:text-blue-400',
secondary: 'bg-gray-700 text-gray-200 hover:bg-gray-600 disabled:opacity-50',
ghost: 'bg-transparent text-blue-400 hover:bg-gray-700 disabled:opacity-50',
};
export function Button({
variant = 'primary',
isLoading = false,
disabled,
children,
className = '',
...props
}: ButtonProps) {
return (
<button
disabled={disabled ?? isLoading}
className={`inline-flex items-center justify-center gap-2 rounded-lg px-4 py-2 text-sm font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-gray-900 ${VARIANT_CLASSES[variant]} ${className}`}
{...props}
>
{isLoading && (
<span className="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent" />
)}
{children}
</button>
);
}
+16
View File
@@ -0,0 +1,16 @@
import { type HTMLAttributes } from 'react';
interface CardProps extends HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
}
export function Card({ children, className = '', ...props }: CardProps) {
return (
<div
className={`rounded-2xl bg-gray-800 p-6 shadow-sm ring-1 ring-gray-700 ${className}`}
{...props}
>
{children}
</div>
);
}
+23
View File
@@ -0,0 +1,23 @@
import { type InputHTMLAttributes, useId } from 'react';
interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
label: string;
error?: string;
}
export function Input({ label, error, className = '', ...props }: InputProps) {
const id = useId();
return (
<div className="flex flex-col gap-1">
<label htmlFor={id} className="text-sm font-medium text-gray-300">
{label}
</label>
<input
id={id}
className={`rounded-lg border bg-gray-700 px-3 py-2 text-sm text-white placeholder-gray-500 outline-none transition-colors focus:ring-2 ${error ? 'border-red-500 focus:border-red-500 focus:ring-red-800' : 'border-gray-600 focus:border-blue-500 focus:ring-blue-900'} ${className}`}
{...props}
/>
{error && <p className="text-xs text-red-400">{error}</p>}
</div>
);
}
+20
View File
@@ -0,0 +1,20 @@
interface SpinnerProps {
size?: 'sm' | 'md' | 'lg';
className?: string;
}
const SIZE_CLASSES = {
sm: 'h-4 w-4 border-2',
md: 'h-8 w-8 border-2',
lg: 'h-12 w-12 border-4',
};
export function Spinner({ size = 'md', className = '' }: SpinnerProps) {
return (
<div
role="status"
aria-label="Загрузка"
className={`animate-spin rounded-full border-blue-600 border-t-transparent ${SIZE_CLASSES[size]} ${className}`}
/>
);
}
+5
View File
@@ -0,0 +1,5 @@
export { Button } from './Button';
export { Input } from './Input';
export { Card } from './Card';
export { Badge } from './Badge';
export { Spinner } from './Spinner';