Compare commits

..
49 changed files with 1887 additions and 2727 deletions

No files matched your search

Regular → Executable
BIN
View File
Binary file not shown.
View File
Whitespace-only changes.
+3 -4
View File
@@ -1,11 +1,10 @@
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
moduleNameMapping: {
'^@/(.*)$': '<rootDir>/src/$1',
},
testMatch: [
'**/__tests__/**/*.(ts|tsx|js)',
'**/*.(test|spec).(ts|tsx|js)',
+1 -1
View File
@@ -1 +1 @@
require('@testing-library/jest-dom')
import '@testing-library/jest-dom'
+8 -780
View File
File diff suppressed because it is too large. Load diff
+1 -5
View File
@@ -26,11 +26,10 @@
"autoprefixer": "^10.4.16",
"axios": "^1.13.2",
"bcryptjs": "^3.0.3",
"better-sqlite3": "12.4.1",
"better-sqlite3": "^11.10.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"date-fns": "^4.1.0",
"dotenv": "^17.2.3",
"drizzle-orm": "^0.44.7",
"lucide-react": "^0.553.0",
"next": "^16.0.1",
@@ -50,15 +49,12 @@
"devDependencies": {
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0",
"@types/jest": "^30.0.0",
"@types/node": "^24.10.0",
"@types/react": "19.2.2",
"@types/react-dom": "^19.2.2",
"eslint": "^9.39.1",
"eslint-config-next": "^16.0.1",
"jest": "^30.2.0",
"jest-environment-jsdom": "^30.2.0",
"ts-jest": "^29.4.6",
"typescript": "^5.9.3"
}
}
-14
View File
@@ -1,14 +0,0 @@
import { getDatabase } from '../src/lib/database';
import { DrizzleDatabase } from '../src/lib/database/drizzle';
async function checkUserRole() {
const db = await getDatabase();
const users = await db.getAllUsers();
console.log('Users found:', users.length);
users.forEach(u => {
console.log(`User: ${u.email}, Role: ${u.role}, ID: ${u.id}`);
});
}
checkUserRole().catch(console.error);
-15
View File
@@ -1,15 +0,0 @@
import { getDatabase } from '../src/lib/database';
async function makeAdmin(email: string) {
const db = await getDatabase();
const user = await db.getUserByEmail(email);
if (!user) {
console.log('User not found');
return;
}
await db.updateUser(user.id, { role: 'admin' });
console.log(`User ${email} updated to admin`);
}
makeAdmin('taratur@gmail.com').catch(console.error);
-57
View File
@@ -1,57 +0,0 @@
import 'dotenv/config';
import { getDatabase } from '../src/lib/database';
import { clerkClient } from '@clerk/nextjs/server';
async function syncAllUsers() {
console.log('Starting full user sync...');
const db = await getDatabase();
// Fetch all users from Clerk
// Note: pagination might be needed for large user bases, but for prototype this is fine.
const client = await clerkClient();
const response = await client.users.getUserList({ limit: 100 });
const clerkUsers = response.data;
console.log(`Found ${clerkUsers.length} users in Clerk.`);
for (const clerkUser of clerkUsers) {
const userId = clerkUser.id;
const email = clerkUser.emailAddresses[0]?.emailAddress;
if (!email) {
console.warn(`User ${userId} has no email, skipping.`);
continue;
}
const existingUser = await db.getUserById(userId);
if (existingUser) {
console.log(`User ${email} (${userId}) already exists.`);
// Optional: Update details if needed
continue;
}
// Check by email to handle ID mismatches (e.g. from seeding)
const existingByEmail = await db.getUserByEmail(email);
if (existingByEmail) {
console.log(`User ${email} found with old ID ${existingByEmail.id}. Migrating to ${userId}...`);
await db.migrateUserId(existingByEmail.id, userId);
continue;
}
console.log(`Creating new user ${email} (${userId})...`);
await db.createUser({
id: userId,
email,
firstName: clerkUser.firstName || '',
lastName: clerkUser.lastName || '',
password: '', // Managed by Clerk
phone: clerkUser.phoneNumbers[0]?.phoneNumber || undefined,
role: (clerkUser.publicMetadata.role as 'admin' | 'client' | 'superAdmin') || 'client'
});
}
console.log('Sync complete.');
}
syncAllUsers().catch(console.error);
-15
View File
@@ -1,15 +0,0 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "node",
"allowImportingTsExtensions": true,
"noEmit": true
},
"ts-node": {
"transpileOnly": true,
"compilerOptions": {
"module": "CommonJS"
}
}
}
-81
View File
@@ -1,81 +0,0 @@
import { getDatabase } from '../src/lib/database/index.ts';
async function verifyDatabase() {
console.log('Starting database verification...');
const db = await getDatabase();
await db.connect();
try {
// 1. Create User
console.log('Creating test user...');
const userId = `test-user-${Date.now()}`;
const user = await db.createUser({
id: userId,
email: `test-${Date.now()}@example.com`,
firstName: 'Test',
lastName: 'User',
password: 'password123',
role: 'client',
phone: '1234567890'
});
console.log('User created:', user.id);
// 2. Create Client
console.log('Creating test client...');
const client = await db.createClient({
userId: user.id,
membershipType: 'basic',
membershipStatus: 'active',
joinDate: new Date()
});
console.log('Client created:', client.id);
// 3. Create Fitness Profile
console.log('Creating fitness profile...');
const profile = await db.createFitnessProfile({
userId: user.id,
height: '180',
weight: '75',
age: '30',
gender: 'male',
activityLevel: 'moderately_active',
fitnessGoals: ['weight_loss'],
exerciseHabits: 'None',
dietHabits: 'None',
medicalConditions: 'None'
});
console.log('Fitness profile created for:', profile.userId);
// 4. Attendance Check-in
console.log('Checking in...');
const checkIn = await db.checkIn(user.id, 'gym', 'Test check-in');
console.log('Checked in:', checkIn.id);
// 5. Verify Active Check-in
const activeCheckIn = await db.getActiveCheckIn(user.id);
if (!activeCheckIn || activeCheckIn.id !== checkIn.id) {
throw new Error('Active check-in verification failed');
}
console.log('Active check-in verified');
// 6. Attendance Check-out
console.log('Checking out...');
const checkOut = await db.checkOut(checkIn.id);
console.log('Checked out:', checkOut?.checkOutTime);
// 7. Cleanup
console.log('Cleaning up...');
await db.deleteUser(user.id);
console.log('Cleanup complete');
console.log('✅ Verification successful!');
} catch (error) {
console.error('❌ Verification failed:', error);
process.exit(1);
} finally {
await db.disconnect();
}
}
verifyDatabase();
@@ -1,137 +0,0 @@
/**
* @jest-environment node
*/
import { POST as checkIn } from '../check-in/route'
import { POST as checkOut } from '../check-out/route'
import { GET as history } from '../history/route'
import { NextRequest } from 'next/server'
// Mock dependencies
jest.mock('@clerk/nextjs/server', () => ({
auth: jest.fn(() => Promise.resolve({ userId: 'test_user_id' })),
currentUser: jest.fn(() => Promise.resolve({ id: 'test_user_id', emailAddresses: [{ emailAddress: 'test@example.com' }] }))
}))
jest.mock('@/lib/sync-user', () => ({
ensureUserSynced: jest.fn()
}))
const mockDb = {
checkIn: jest.fn(),
checkOut: jest.fn(),
getAttendanceHistory: jest.fn(),
getActiveCheckIn: jest.fn(),
getUserById: jest.fn(),
createUser: jest.fn(),
getClientByUserId: jest.fn(),
createClient: jest.fn(),
getFitnessProfileByUserId: jest.fn(),
createFitnessProfile: jest.fn(),
}
jest.mock('@/lib/database', () => ({
getDatabase: jest.fn(() => Promise.resolve(mockDb))
}))
describe('Attendance API', () => {
beforeEach(() => {
jest.clearAllMocks()
})
describe('POST /api/attendance/check-in', () => {
it('should successfully check in', async () => {
mockDb.getUserById.mockResolvedValue({ id: 'test_user_id' })
mockDb.getActiveCheckIn.mockResolvedValue(null)
mockDb.checkIn.mockResolvedValue({
id: 'attendance_id',
userId: 'test_user_id',
checkInTime: new Date(),
type: 'gym'
})
const req = new NextRequest('http://localhost/api/attendance/check-in', {
method: 'POST',
body: JSON.stringify({ type: 'gym', notes: 'Test check-in' })
})
const res = await checkIn(req)
const data = await res.json()
expect(res.status).toBe(200)
expect(data.id).toBe('attendance_id')
expect(data.userId).toBe('test_user_id')
expect(mockDb.checkIn).toHaveBeenCalledWith('test_user_id', 'gym', 'Test check-in')
})
it('should fail if already checked in', async () => {
mockDb.getUserById.mockResolvedValue({ id: 'test_user_id' })
mockDb.getActiveCheckIn.mockResolvedValue({ id: 'existing_id' })
const req = new NextRequest('http://localhost/api/attendance/check-in', {
method: 'POST',
body: JSON.stringify({ type: 'gym' })
})
const res = await checkIn(req)
const text = await res.text()
expect(res.status).toBe(400)
expect(text).toBe('Already checked in')
})
})
describe('POST /api/attendance/check-out', () => {
it('should successfully check out', async () => {
mockDb.getActiveCheckIn.mockResolvedValue({ id: 'attendance_id' })
mockDb.checkOut.mockResolvedValue({
id: 'attendance_id',
checkOutTime: new Date()
})
const req = new NextRequest('http://localhost/api/attendance/check-out', {
method: 'POST'
})
const res = await checkOut(req)
const data = await res.json()
expect(res.status).toBe(200)
expect(data.id).toBe('attendance_id')
expect(data.checkOutTime).toBeDefined()
expect(mockDb.checkOut).toHaveBeenCalledWith('attendance_id')
})
it('should fail if not checked in', async () => {
mockDb.getActiveCheckIn.mockResolvedValue(null)
const req = new NextRequest('http://localhost/api/attendance/check-out', {
method: 'POST'
})
const res = await checkOut(req)
const text = await res.text()
expect(res.status).toBe(404)
expect(text).toBe('No active check-in found')
})
})
describe('GET /api/attendance/history', () => {
it('should return attendance history', async () => {
const historyData = [
{ id: '1', checkInTime: new Date() },
{ id: '2', checkInTime: new Date() }
]
mockDb.getUserById.mockResolvedValue({ id: 'test_user_id' })
mockDb.getAttendanceHistory.mockResolvedValue(historyData)
const req = new NextRequest('http://localhost/api/attendance/history')
const res = await history(req)
const data = await res.json()
expect(res.status).toBe(200)
expect(data).toEqual(JSON.parse(JSON.stringify(historyData))) // Handle date serialization
expect(mockDb.getAttendanceHistory).toHaveBeenCalledWith('test_user_id')
})
})
})
@@ -172,9 +172,9 @@ export async function POST(req: Request) {
const recommendation = await db.createRecommendation({
id: crypto.randomUUID(),
userId,
fitnessProfileId: profile.id,
fitnessProfileId: profile.userId,
type: 'ai_plan',
recommendationText: parsedResponse.recommendationText,
content: parsedResponse.recommendationText,
activityPlan: parsedResponse.activityPlan,
dietPlan: parsedResponse.dietPlan,
status: 'pending'
@@ -76,7 +76,7 @@ export async function POST(request: Request) {
userId,
fitnessProfileId,
type: 'ai_plan',
recommendationText: recommendationText,
content: recommendationText,
activityPlan,
dietPlan,
status: status || 'pending'
@@ -90,7 +90,7 @@ export async function POST(request: Request) {
id: crypto.randomUUID(),
userId,
type,
recommendationText: content,
content,
status: status || 'pending'
})
return NextResponse.json(recommendation)
@@ -122,8 +122,8 @@ export async function PUT(request: Request) {
const updated = await db.updateRecommendation(id, {
...(status && { status }),
...(recommendationText && { recommendationText }),
...(content && { recommendationText: content }),
...(recommendationText && { content: recommendationText }), // Map legacy field
...(content && { content }),
...(activityPlan && { activityPlan }),
...(dietPlan && { dietPlan })
})
+1 -1
View File
@@ -26,7 +26,7 @@ export default function RootLayout({
<ClerkProvider>
<html lang="en">
<body className={inter.className}>
<div className="flex min-h-screen bg-slate-50">
<div className="flex min-h-screen bg-gradient-to-br from-white via-blue-50 to-slate-50">
<Sidebar />
<main className="flex-1 ml-64 p-8">
{children}
+122 -48
View File
@@ -1,10 +1,10 @@
"use client";
import { useEffect, useState } from "react";
import { Users, CreditCard, CalendarCheck, TrendingUp } from "lucide-react";
import { Users, Activity, Zap, TrendingUp, Trophy, Target } from "lucide-react";
import { StatsCard } from "@/components/ui/StatsCard";
import { UserManagement } from "@/components/users/UserManagement";
import { AnalyticsDashboard } from "@/components/analytics/AnalyticsDashboard";
import { UserManagement } from "@/components/users/UserManagement";
import axios from "axios";
interface DashboardStats {
@@ -46,56 +46,130 @@ export default function Home() {
};
return (
<div className="space-y-8">
<div>
<h2 className="text-3xl font-bold text-slate-900">Dashboard</h2>
<p className="text-slate-500 mt-2">Welcome back, here's what's happening today.</p>
<div className="min-h-screen bg-gradient-to-br from-white via-slate-50 to-blue-50">
{/* Premium Header */}
<div className="bg-gradient-to-r from-slate-900 via-blue-900 to-slate-900 shadow-2xl">
<div className="max-w-7xl mx-auto px-8 py-12">
<div className="flex items-end justify-between">
<div className="flex items-end gap-6">
<div className="w-16 h-16 bg-gradient-to-br from-blue-400 to-cyan-400 rounded-3xl flex items-center justify-center shadow-lg">
<Trophy className="w-9 h-9 text-white" />
</div>
<div>
<h1 className="text-5xl font-black text-white tracking-tight">FitAI</h1>
<p className="text-blue-200 text-sm font-semibold mt-2">Premium Sports Management Platform</p>
</div>
</div>
<div className="text-right">
<p className="text-blue-100 text-sm">Welcome</p>
<p className="text-2xl font-bold text-white">Admin Control</p>
</div>
</div>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
<StatsCard
title="Total Users"
value={loading ? "..." : stats.totalUsers}
change="+12%" // Placeholder for now as we don't track historical growth yet
trend="up"
icon={Users}
color="blue"
/>
<StatsCard
title="Active Clients"
value={loading ? "..." : stats.activeClients}
change="+5%"
trend="up"
icon={CalendarCheck}
color="green"
/>
<StatsCard
title="Revenue"
value={loading ? "..." : formatCurrency(stats.totalRevenue)}
change={`${stats.revenueGrowth > 0 ? "+" : ""}${stats.revenueGrowth}%`}
trend={stats.revenueGrowth >= 0 ? "up" : "down"}
icon={CreditCard}
color="purple"
/>
<StatsCard
title="Growth"
value="24%" // Placeholder
change="-2%"
trend="down"
icon={TrendingUp}
color="orange"
/>
</div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
<div className="lg:col-span-2 bg-white rounded-xl shadow-sm border border-slate-100 p-6">
<h3 className="text-xl font-bold text-slate-900 mb-6">Recent Activity</h3>
<UserManagement />
{/* Main Content with Proper Spacing */}
<div className="max-w-7xl mx-auto px-8 py-16">
{/* Section: Performance Metrics */}
<div className="mb-20">
<div className="mb-10">
<h2 className="text-3xl font-black text-slate-900">Performance Metrics</h2>
<p className="text-slate-600 mt-2 font-medium">Real-time insights into your platform</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-8">
<StatsCard
title="Total Members"
value={loading ? "..." : stats.totalUsers}
change="+12% this month"
trend="up"
icon={Users}
color="blue"
/>
<StatsCard
title="Active Athletes"
value={loading ? "..." : stats.activeClients}
change="+8% this week"
trend="up"
icon={Activity}
color="green"
/>
<StatsCard
title="Revenue"
value={loading ? "..." : formatCurrency(stats.totalRevenue)}
change={`${stats.revenueGrowth > 0 ? "+" : ""}${stats.revenueGrowth}% growth`}
trend={stats.revenueGrowth >= 0 ? "up" : "down"}
icon={Zap}
color="amber"
/>
<StatsCard
title="Growth"
value="24%"
change="+3% vs last month"
trend="up"
icon={TrendingUp}
color="cyan"
/>
</div>
</div>
<div className="bg-white rounded-xl shadow-sm border border-slate-100 p-6">
<h3 className="text-xl font-bold text-slate-900 mb-6">Quick Analytics</h3>
<AnalyticsDashboard />
{/* Section: Member Management */}
<div className="mb-20">
<div className="mb-8">
<h2 className="text-3xl font-black text-slate-900">Members Directory</h2>
<p className="text-slate-600 mt-2 font-medium">View detailed member information and analytics</p>
</div>
<div className="bg-white rounded-3xl shadow-lg border border-slate-100 p-10">
<UserManagement />
</div>
</div>
{/* Section: Key Insights */}
<div className="mb-20">
<div className="mb-8">
<h2 className="text-3xl font-black text-slate-900">Key Insights</h2>
<p className="text-slate-600 mt-2 font-medium">Quick stats overview</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-8">
<div className="bg-white rounded-3xl shadow-lg border border-slate-100 p-8">
<div className="p-6 bg-gradient-to-br from-blue-50 via-blue-100 to-blue-50 rounded-2xl border border-blue-200 hover:shadow-md transition-shadow">
<p className="text-xs text-blue-700 uppercase font-black tracking-wider">Members</p>
<p className="text-3xl font-black text-blue-900 mt-4 truncate">{loading ? "..." : stats.totalUsers}</p>
<p className="text-sm text-blue-700 mt-2 font-semibold">Active subscriptions</p>
</div>
</div>
<div className="bg-white rounded-3xl shadow-lg border border-slate-100 p-8">
<div className="p-6 bg-gradient-to-br from-emerald-50 via-emerald-100 to-emerald-50 rounded-2xl border border-emerald-200 hover:shadow-md transition-shadow">
<p className="text-xs text-emerald-700 uppercase font-black tracking-wider">Training Now</p>
<p className="text-3xl font-black text-emerald-900 mt-4 truncate">{loading ? "..." : stats.activeClients}</p>
<p className="text-sm text-emerald-700 mt-2 font-semibold">Currently active</p>
</div>
</div>
<div className="bg-white rounded-3xl shadow-lg border border-slate-100 p-8">
<div className="p-6 bg-gradient-to-br from-amber-50 via-amber-100 to-amber-50 rounded-2xl border border-amber-200 hover:shadow-md transition-shadow">
<p className="text-xs text-amber-700 uppercase font-black tracking-wider">Revenue</p>
<p className="text-2xl font-black text-amber-900 mt-4 truncate">{loading ? "..." : formatCurrency(stats.totalRevenue)}</p>
<p className="text-sm text-amber-700 mt-2 font-semibold">This period</p>
</div>
</div>
</div>
</div>
{/* Section: Detailed Report - Full Width */}
<div className="mb-20">
<div className="mb-8">
<h2 className="text-3xl font-black text-slate-900">Detailed Report</h2>
<p className="text-slate-600 mt-2 font-medium">Analytics Dashboard</p>
</div>
<div className="bg-white rounded-3xl shadow-lg border border-slate-100 p-10">
<div className="bg-slate-50 rounded-2xl p-8">
<AnalyticsDashboard />
</div>
</div>
</div>
</div>
</div>
+1 -1
View File
@@ -219,7 +219,7 @@ export default function RecommendationsPage() {
</div>
<div className="space-y-2 text-sm mb-4">
<div>
<span className="font-semibold">Advice:</span> {rec.recommendationText}
<span className="font-semibold">Advice:</span> {rec.content}
</div>
<div>
<span className="font-semibold">Activity:</span> {rec.activityPlan}
+15 -14
View File
@@ -65,14 +65,15 @@ export function Sidebar() {
];
return (
<aside className="w-64 bg-slate-900 text-white h-screen fixed left-0 top-0 flex flex-col border-r border-slate-800">
<div className="p-6 border-b border-slate-800">
<h1 className="text-2xl font-bold bg-gradient-to-r from-blue-400 to-indigo-400 bg-clip-text text-transparent">
FitAI Admin
<aside className="w-64 bg-gradient-to-b from-white to-slate-50 h-screen fixed left-0 top-0 flex flex-col border-r border-slate-200/50">
<div className="p-6 border-b border-slate-200">
<h1 className="text-2xl font-black bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">
FitAI
</h1>
<p className="text-xs text-slate-600 font-medium mt-1">Sports Admin</p>
</div>
<nav className="flex-1 p-4 space-y-2">
<nav className="flex-1 p-4 space-y-1">
{menuItems.map((item) => {
const Icon = item.icon;
const isActive = pathname === item.href;
@@ -81,25 +82,25 @@ export function Sidebar() {
<Link
key={item.href}
href={item.href}
className={`flex items-center gap-3 px-4 py-3 rounded-lg transition-all duration-200 group ${isActive
? "bg-blue-600 text-white shadow-lg shadow-blue-900/20"
: "text-slate-400 hover:bg-slate-800 hover:text-white"}`}
className={`flex items-center gap-3 px-4 py-3 rounded-2xl transition-all duration-200 group font-medium ${isActive
? "bg-gradient-to-r from-blue-500 to-cyan-500 text-white shadow-md shadow-blue-500/20"
: "text-slate-700 hover:bg-slate-100 hover:text-blue-600"}`}
>
<Icon size={20} className={isActive ? "text-white" : "text-slate-500 group-hover:text-white"} />
<span className="font-medium">{label}</span>
<Icon size={20} className={isActive ? "text-white" : "text-slate-500 group-hover:text-blue-600"} />
<span className="text-sm">{label}</span>
</Link>
);
})}
</nav>
<div className="p-4 border-t border-slate-800">
<div className="flex items-center gap-3 px-4 py-3 rounded-lg bg-slate-800/50">
<div className="p-4 border-t border-slate-200">
<div className="flex items-center gap-3 px-4 py-3 rounded-2xl bg-slate-100">
<UserButton afterSignOutUrl="/" />
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-white truncate">
<p className="text-sm font-semibold text-slate-900 truncate">
{user?.fullName || "Admin User"}
</p>
<p className="text-xs text-slate-400 truncate">
<p className="text-xs text-slate-600 truncate">
{user?.primaryEmailAddress?.emailAddress}
</p>
</div>
+54 -33
View File
@@ -1,5 +1,4 @@
import { LucideIcon } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
interface StatsCardProps {
title: string;
@@ -7,45 +6,67 @@ interface StatsCardProps {
change?: string;
trend?: "up" | "down" | "neutral";
icon: LucideIcon;
color?: "blue" | "green" | "purple" | "orange";
color?: "blue" | "green" | "amber" | "cyan";
}
export function StatsCard({ title, value, change, trend, icon: Icon, color = "blue" }: StatsCardProps) {
const colorStyles = {
blue: "bg-blue-50 text-blue-600",
green: "bg-green-50 text-green-600",
purple: "bg-purple-50 text-purple-600",
orange: "bg-orange-50 text-orange-600",
blue: {
bg: "from-blue-50 via-blue-100 to-blue-50",
text: "text-blue-950",
icon: "bg-gradient-to-br from-blue-500 to-blue-600",
border: "border-blue-200",
label: "text-blue-700"
},
green: {
bg: "from-emerald-50 via-emerald-100 to-emerald-50",
text: "text-emerald-950",
icon: "bg-gradient-to-br from-emerald-500 to-emerald-600",
border: "border-emerald-200",
label: "text-emerald-700"
},
amber: {
bg: "from-amber-50 via-amber-100 to-amber-50",
text: "text-amber-950",
icon: "bg-gradient-to-br from-amber-500 to-amber-600",
border: "border-amber-200",
label: "text-amber-700"
},
cyan: {
bg: "from-cyan-50 via-cyan-100 to-cyan-50",
text: "text-cyan-950",
icon: "bg-gradient-to-br from-cyan-500 to-cyan-600",
border: "border-cyan-200",
label: "text-cyan-700"
},
};
const style = colorStyles[color];
return (
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium text-muted-foreground">
{title}
</CardTitle>
<div className={`p-2 rounded-lg ${colorStyles[color]}`}>
<Icon size={16} />
</div>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{value}</div>
{change && (
<p className="text-xs text-muted-foreground mt-1">
<span
className={`font-medium ${trend === "up"
? "text-green-600"
: trend === "down"
? "text-red-600"
: "text-slate-600"
}`}
>
{change}
</span>{" "}
vs last month
<div className={`bg-gradient-to-br ${style.bg} rounded-3xl border ${style.border} p-6 hover:shadow-xl transition-all duration-300 cursor-default group`}>
<div className="flex items-start justify-between mb-4 gap-3">
<div className="flex-1 min-w-0">
<p className={`text-xs ${style.label} uppercase font-black tracking-widest truncate`}>
{title}
</p>
)}
</CardContent>
</Card>
</div>
<div className={`p-3 ${style.icon} rounded-2xl flex items-center justify-center shadow-lg group-hover:scale-110 transition-transform flex-shrink-0`}>
<Icon size={20} className="text-white" />
</div>
</div>
<div className={`text-4xl font-black ${style.text} mb-3 truncate`}>
{value}
</div>
{change && (
<div className="flex items-center gap-1 truncate">
<span className={`text-xs font-black uppercase tracking-wide ${style.label} truncate`}>
{trend === "up" ? "↑" : trend === "down" ? "↓" : "→"} {change}
</span>
</div>
)}
</div>
);
}
+19 -52
View File
@@ -1,56 +1,23 @@
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import React from 'react'
import { cn } from "@/lib/utils";
const buttonVariants = cva(
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
},
);
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'primary' | 'secondary'
children: React.ReactNode
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button";
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
);
},
);
Button.displayName = "Button";
export function Button({ variant = 'primary', children, className = '', ...props }: ButtonProps) {
const baseClasses = 'px-4 py-2 rounded-md font-medium transition-colors'
const variantClasses = {
primary: 'bg-blue-600 text-white hover:bg-blue-700',
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300',
}
export { Button, buttonVariants };
return (
<button
className={`${baseClasses} ${variantClasses[variant]} ${className}`}
{...props}
>
{children}
</button>
)
}
+33 -71
View File
@@ -1,76 +1,38 @@
import * as React from "react"
import React from 'react'
import { cn } from "@/lib/utils"
interface CardProps {
children: React.ReactNode
className?: string
}
const Card = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
"rounded-xl border bg-card text-card-foreground shadow",
className
)}
{...props}
/>
))
Card.displayName = "Card"
export function Card({ children, className = '' }: CardProps) {
return (
<div className={`bg-white rounded-lg shadow-md p-6 ${className}`}>
{children}
</div>
)
}
const CardHeader = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex flex-col space-y-1.5 p-6", className)}
{...props}
/>
))
CardHeader.displayName = "CardHeader"
export function CardHeader({ children, className = '' }: CardProps) {
return (
<div className={`mb-4 ${className}`}>
{children}
</div>
)
}
const CardTitle = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
<h3
ref={ref}
className={cn("font-semibold leading-none tracking-tight", className)}
{...props}
/>
))
CardTitle.displayName = "CardTitle"
export function CardContent({ children, className = '' }: CardProps) {
return (
<div className={className}>
{children}
</div>
)
}
const CardDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<p
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
CardDescription.displayName = "CardDescription"
const CardContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("p-6 pt-0", className)} {...props} />
))
CardContent.displayName = "CardContent"
const CardFooter = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex items-center p-6 pt-0", className)}
{...props}
/>
))
CardFooter.displayName = "CardFooter"
export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }
export function CardTitle({ children, className = '' }: CardProps) {
return (
<div className={`text-sm font-medium ${className}`}>
{children}
</div>
)
}
@@ -8,7 +8,7 @@ interface Recommendation {
id: string;
userId: string;
type: "short_term" | "medium_term" | "long_term" | "ai_plan";
recommendationText: string;
content: string;
activityPlan?: string;
dietPlan?: string;
status: "pending" | "completed" | "approved" | "rejected";
@@ -97,7 +97,7 @@ export function Recommendations({ userId }: RecommendationsProps) {
}`}
>
<div className="w-full">
<p className="text-gray-700">{rec.recommendationText}</p>
<p className="text-sm font-medium">{rec.content}</p>
{rec.type === 'ai_plan' && (
<div className="mt-2 text-xs text-gray-600 space-y-1">
{rec.activityPlan && <p><span className="font-semibold">Activity:</span> {rec.activityPlan}</p>}
+3 -3
View File
@@ -224,14 +224,14 @@ export function UserGrid({
const gridRef = React.useRef<AgGridReact<User>>(null);
const gridOptions = {
theme: "legacy" as const,
theme: "legacy",
columnDefs,
defaultColDef,
rowData: users,
rowSelection: "multiple" as const,
rowSelection: "multiple",
onSelectionChanged: () => {
const selectedNodes = gridRef.current?.api.getSelectedNodes();
const selectedData = selectedNodes?.map((node) => node.data).filter((u): u is User => !!u) || [];
const selectedData = selectedNodes?.map((node) => node.data) || [];
setSelectedUsers(selectedData);
if (selectedData.length === 1 && onUserSelect) {
onUserSelect(selectedData[0]);
+273 -183
View File
@@ -2,9 +2,8 @@
import { useState, useEffect } from "react";
import { UserGrid } from "@/components/users/UserGrid";
// import { Button } from "@/components/ui/Button";
import { Card, CardHeader, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Card, CardHeader, CardContent } from "@/components/ui/card";
interface User {
id: string;
@@ -35,6 +34,7 @@ export function UserManagement() {
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [isEditing, setIsEditing] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [activeTab, setActiveTab] = useState<"users" | "details">("users");
const [editForm, setEditForm] = useState<{
firstName: string;
lastName: string;
@@ -206,104 +206,228 @@ export function UserManagement() {
};
return (
<div className="space-y-6">
<div className="flex justify-between items-center">
<h2 className="text-2xl font-bold">User Management</h2>
<div className="flex gap-2">
<Button
variant={filter === "all" ? "default" : "outline"}
onClick={() => setFilter("all")}
>
All Users
</Button>
<Button
variant={filter === "client" ? "default" : "outline"}
onClick={() => selectedUser && handleEditUser(selectedUser)}
disabled={!selectedUser}
>
Edit User
</Button>
<Button
variant={filter === "client" ? "default" : "outline"}
onClick={() => {
setEditForm({
firstName: "",
lastName: "",
email: "",
role: "client",
phone: "",
});
setSelectedUser(null);
setIsEditing(true);
}}
>
Invite User
</Button>
<Button
variant={filter === "client" ? "default" : "outline"}
onClick={() => selectedUser && handleDeleteUser(selectedUser)}
disabled={!selectedUser}
>
Delete User
</Button>
<Button
variant={filter === "client" ? "default" : "outline"}
onClick={() => setFilter("client")}
>
Clientsa
</Button>
<Button
variant={filter === "trainer" ? "default" : "outline"}
onClick={() => setFilter("trainer")}
>
Trainers
</Button>
<Button
variant={filter === "admin" ? "default" : "outline"}
onClick={() => setFilter("admin")}
>
Admins
</Button>
<Button
variant={filter === "superAdmin" ? "default" : "outline"}
onClick={() => setFilter("superAdmin")}
>
Super Admins
</Button>
<div className="space-y-4">
{/* Header with Count */}
<div className="flex items-center justify-between">
<h3 className="text-lg font-black text-slate-900">
{users.length} {users.length === 1 ? 'user' : 'users'}
</h3>
{selectedUser && (
<p className="text-sm text-blue-600 font-semibold">
Selected: {selectedUser.firstName} {selectedUser.lastName}
</p>
)}
</div>
{/* Compact Controls - Always Visible but Organized */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-3 p-4 bg-gradient-to-r from-slate-50 to-blue-50 rounded-lg border border-slate-200">
{/* Filter Row */}
<div>
<p className="text-xs font-black uppercase tracking-wider text-slate-600 mb-2">Roles</p>
<div className="flex flex-wrap gap-2">
<button
onClick={() => setFilter("all")}
className={`px-2 py-1 text-xs font-semibold rounded-lg transition-all ${
filter === "all"
? "bg-blue-500 text-white shadow-md"
: "bg-white text-slate-700 hover:bg-slate-100 border border-slate-200"
}`}
>
All
</button>
<button
onClick={() => setFilter("client")}
className={`px-2 py-1 text-xs font-semibold rounded-lg transition-all ${
filter === "client"
? "bg-blue-500 text-white shadow-md"
: "bg-white text-slate-700 hover:bg-slate-100 border border-slate-200"
}`}
>
Clients
</button>
<button
onClick={() => setFilter("trainer")}
className={`px-2 py-1 text-xs font-semibold rounded-lg transition-all ${
filter === "trainer"
? "bg-blue-500 text-white shadow-md"
: "bg-white text-slate-700 hover:bg-slate-100 border border-slate-200"
}`}
>
Trainers
</button>
<button
onClick={() => setFilter("admin")}
className={`px-2 py-1 text-xs font-semibold rounded-lg transition-all ${
filter === "admin"
? "bg-blue-500 text-white shadow-md"
: "bg-white text-slate-700 hover:bg-slate-100 border border-slate-200"
}`}
>
Admins
</button>
<button
onClick={() => setFilter("superAdmin")}
className={`px-2 py-1 text-xs font-semibold rounded-lg transition-all ${
filter === "superAdmin"
? "bg-blue-500 text-white shadow-md"
: "bg-white text-slate-700 hover:bg-slate-100 border border-slate-200"
}`}
>
Super Admins
</button>
</div>
</div>
{/* Action Buttons Row */}
<div>
<p className="text-xs font-black uppercase tracking-wider text-slate-600 mb-2">Quick Actions</p>
<div className="flex flex-wrap gap-2">
<button
onClick={() => {
setEditForm({
firstName: "",
lastName: "",
email: "",
role: "client",
phone: "",
});
setSelectedUser(null);
setIsEditing(true);
}}
className="px-2 py-1 text-xs font-semibold rounded-lg bg-blue-500 text-white hover:bg-blue-600"
>
+ Invite
</button>
<button
onClick={() => selectedUser && handleEditUser(selectedUser)}
disabled={!selectedUser}
className="px-2 py-1 text-xs font-semibold rounded-lg bg-white text-slate-700 hover:bg-slate-100 border border-slate-200 disabled:opacity-50"
>
Edit
</button>
<button
onClick={() => selectedUser && handleDeleteUser(selectedUser)}
disabled={!selectedUser}
className="px-2 py-1 text-xs font-semibold rounded-lg bg-white text-slate-700 hover:bg-slate-100 border border-slate-200 disabled:opacity-50"
>
Delete
</button>
<button onClick={handleRefresh} className="px-2 py-1 text-xs font-semibold rounded-lg bg-white text-slate-700 hover:bg-slate-100 border border-slate-200">
Refresh
</button>
<button onClick={handleExport} className="px-2 py-1 text-xs font-semibold rounded-lg bg-white text-slate-700 hover:bg-slate-100 border border-slate-200">
Export
</button>
</div>
</div>
</div>
<div className="flex justify-between items-center">
<div className="text-sm text-gray-600">
Showing {users.length} users
{selectedUser && (
<span className="ml-4 text-blue-600">
Selected: {selectedUser.firstName} {selectedUser.lastName}
</span>
)}
</div>
<div className="flex gap-2">
<Button variant="default" onClick={handleRefresh}>
Refresh
</Button>
<Button variant="default" onClick={handleExport}>
Export CSV
</Button>
</div>
{/* Tab Navigation */}
<div className="flex gap-2 border-b border-slate-200">
<button
onClick={() => setActiveTab("users")}
className={`px-4 py-3 text-sm font-semibold border-b-2 transition-all ${
activeTab === "users"
? "border-blue-500 text-blue-600"
: "border-transparent text-slate-600 hover:text-slate-900"
}`}
>
User List ({users.length})
</button>
<button
onClick={() => setActiveTab("details")}
disabled={!selectedUser}
className={`px-4 py-3 text-sm font-semibold border-b-2 transition-all ${
activeTab === "details"
? "border-blue-500 text-blue-600"
: "border-transparent text-slate-600 hover:text-slate-900"
} ${!selectedUser ? "opacity-50 cursor-not-allowed" : ""}`}
>
User Details
</button>
</div>
<Card>
<CardContent className="p-0">
<UserGrid
users={users}
onUserSelect={(user) => handleUserSelect(user)}
onEditUser={handleEditUser}
onDeleteUser={handleDeleteUser}
onBulkDelete={handleBulkDelete}
loading={loading}
/>
</CardContent>
</Card>
{/* Tab Content */}
{activeTab === "users" && (
<Card className="border border-slate-200">
<CardContent className="p-0">
<UserGrid
users={users}
onUserSelect={(user) => {
handleUserSelect(user);
setActiveTab("details");
}}
onEditUser={handleEditUser}
onDeleteUser={handleDeleteUser}
onBulkDelete={handleBulkDelete}
loading={loading}
/>
</CardContent>
</Card>
)}
{activeTab === "details" && selectedUser && (
<div className="p-6 bg-white rounded-xl border border-slate-200">
<div className="mb-6">
<h3 className="text-xl font-black text-slate-900">
{selectedUser.firstName} {selectedUser.lastName}
</h3>
<p className="text-sm text-slate-600 mt-1">{selectedUser.email}</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div className="space-y-2">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Role</p>
<p className="text-base font-semibold text-slate-900">{selectedUser.role}</p>
</div>
<div className="space-y-2">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Joined</p>
<p className="text-base font-semibold text-slate-900">{new Date(selectedUser.createdAt).toLocaleDateString()}</p>
</div>
{selectedUser.client && (
<>
<div className="space-y-2">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Membership</p>
<p className="text-base font-semibold text-slate-900">{selectedUser.client.membershipType}</p>
</div>
<div className="space-y-2">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Status</p>
<p className="text-base font-semibold text-slate-900">{selectedUser.client.membershipStatus}</p>
</div>
</>
)}
<div className="space-y-2">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Check-ins This Month</p>
<p className="text-2xl font-black text-blue-600">{selectedUser.checkInsThisMonth || 0}</p>
</div>
</div>
<div className="flex gap-3 mt-8 pt-6 border-t border-slate-200">
<button
onClick={() => selectedUser && handleEditUser(selectedUser)}
className="px-5 py-2 bg-blue-50 text-blue-600 rounded-lg font-semibold text-sm hover:bg-blue-100 transition-all"
>
Edit User
</button>
<button
onClick={() => selectedUser && handleDeleteUser(selectedUser)}
className="px-5 py-2 bg-red-50 text-red-600 rounded-lg font-semibold text-sm hover:bg-red-100 transition-all"
>
Delete User
</button>
<a
href={`/users/${selectedUser.id}`}
className="px-5 py-2 bg-gradient-to-r from-blue-500 to-cyan-500 text-white rounded-lg font-semibold text-sm hover:shadow-lg transition-all"
>
Full Profile
</a>
</div>
</div>
)}
{isEditing && editForm && (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
@@ -439,98 +563,64 @@ export function UserManagement() {
)}
{selectedUser && (
<Card>
<CardHeader className="flex flex-row items-center justify-between">
<h3 className="text-lg font-semibold">User Details</h3>
<div className="mt-8 pt-8 border-t border-slate-200">
<div className="mb-6">
<h3 className="text-lg font-black text-slate-900">
{selectedUser.firstName} {selectedUser.lastName}
</h3>
<p className="text-sm text-slate-600 mt-1">{selectedUser.email}</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div className="space-y-3">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Role</p>
<p className="text-base font-semibold text-slate-900">{selectedUser.role}</p>
<p className="text-xs font-black uppercase tracking-wider text-slate-600 mt-4">Joined</p>
<p className="text-base font-semibold text-slate-900">{new Date(selectedUser.createdAt).toLocaleDateString()}</p>
</div>
{selectedUser.client && (
<div className="space-y-3">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Membership</p>
<p className="text-base font-semibold text-slate-900">{selectedUser.client.membershipType}</p>
<p className="text-xs font-black uppercase tracking-wider text-slate-600 mt-4">Status</p>
<p className="text-base font-semibold text-slate-900">{selectedUser.client.membershipStatus}</p>
</div>
)}
<div className="space-y-3">
<p className="text-xs font-black uppercase tracking-wider text-slate-600">Check-ins This Month</p>
<p className="text-2xl font-black text-blue-600">{selectedUser.checkInsThisMonth || 0}</p>
<p className="text-xs font-black uppercase tracking-wider text-slate-600 mt-4">Last Check-in</p>
<p className="text-sm text-slate-900">
{selectedUser.lastCheckInTime
? new Date(selectedUser.lastCheckInTime).toLocaleDateString()
: "Never"}
</p>
</div>
</div>
<div className="flex gap-3 mt-6">
<button
onClick={() => selectedUser && handleEditUser(selectedUser)}
className="px-5 py-2 bg-blue-50 text-blue-600 rounded-lg font-semibold text-sm hover:bg-blue-100 transition-all"
>
Edit
</button>
<button
onClick={() => selectedUser && handleDeleteUser(selectedUser)}
className="px-5 py-2 bg-red-50 text-red-600 rounded-lg font-semibold text-sm hover:bg-red-100 transition-all"
>
Delete
</button>
<a
href={`/users/${selectedUser.id}`}
className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm font-medium"
className="px-5 py-2 bg-gradient-to-r from-blue-500 to-cyan-500 text-white rounded-lg font-semibold text-sm hover:shadow-lg transition-all"
>
View Full Profile & Recommendations
Full Profile
</a>
</CardHeader>
<CardContent>
<div className="grid grid-cols-3 gap-4">
<div>
<h4 className="font-medium mb-2">Basic Information</h4>
<div className="space-y-1 text-sm">
<p>
<span className="font-medium">Name:</span>{" "}
{selectedUser.firstName} {selectedUser.lastName}
</p>
<p>
<span className="font-medium">Email:</span>{" "}
{selectedUser.email}
</p>
<p>
<span className="font-medium">Phone:</span>{" "}
{selectedUser.phone || "N/A"}
</p>
<p>
<span className="font-medium">Role:</span>{" "}
{selectedUser.role}
</p>
<p>
<span className="font-medium">Joined:</span>{" "}
{new Date(selectedUser.createdAt).toLocaleDateString()}
</p>
</div>
</div>
{selectedUser.client && (
<div>
<h4 className="font-medium mb-2">Client Information</h4>
<div className="space-y-1 text-sm">
<p>
<span className="font-medium">Membership:</span>{" "}
{selectedUser.client.membershipType}
</p>
<p>
<span className="font-medium">Status:</span>{" "}
{selectedUser.client.membershipStatus}
</p>
<p>
<span className="font-medium">Member Since:</span>{" "}
{new Date(
selectedUser.client.joinDate,
).toLocaleDateString()}
</p>
<p>
<span className="font-medium">Last Visit:</span>{" "}
{selectedUser.client.lastVisit
? new Date(
selectedUser.client.lastVisit,
).toLocaleDateString()
: "Never"}
</p>
</div>
</div>
)}
<div>
<h4 className="font-medium mb-2">Check-In Statistics</h4>
<div className="space-y-1 text-sm">
<p>
<span className="font-medium">Last Check-In:</span>{" "}
{selectedUser.lastCheckInTime
? new Date(
selectedUser.lastCheckInTime,
).toLocaleString()
: "Never"}
</p>
<p>
<span className="font-medium">This Week:</span>{" "}
{selectedUser.checkInsThisWeek || 0} check-ins
</p>
<p>
<span className="font-medium">This Month:</span>{" "}
{selectedUser.checkInsThisMonth || 0} check-ins
</p>
</div>
</div>
</div>
</CardContent>
</Card>
</div>
</div>
)}
</div>
);
+9 -3
View File
@@ -1,11 +1,17 @@
import { getDatabase } from '@/lib/database';
import type { FitnessGoal, FitnessProfile, Recommendation } from '@/lib/database/types';
import type { FitnessGoal } from '@/lib/database/types';
// Import types from database package
import type {
FitnessProfile as DBFitnessProfile,
Recommendation as DBRecommendation,
} from '@fitai/database';
export interface AIContext {
profile: FitnessProfile;
profile: DBFitnessProfile;
activeGoals: FitnessGoal[];
completedGoals: FitnessGoal[];
recentRecommendations: Recommendation[];
recentRecommendations: DBRecommendation[];
progressSummary: {
goalsCompleted: number;
goalsActive: number;
+2 -2
View File
@@ -32,7 +32,7 @@ export function buildEnhancedPrompt(context: AIContext): string {
- Weight: ${profile.weight || 'Not specified'} kg
- Age: ${profile.age || 'Not specified'}
- Gender: ${profile.gender || 'Not specified'}
- Primary Goal: ${profile.fitnessGoals?.[0] || 'General fitness'}
- Primary Goal: ${profile.fitnessGoal || 'General fitness'}
- Activity Level: ${profile.activityLevel || 'Not specified'}
- Medical Conditions: ${profile.medicalConditions || 'None'}
- Allergies: ${profile.allergies || 'None'}
@@ -77,7 +77,7 @@ Generate a detailed daily recommendation for a user with the following profile:
- Weight: ${profile.weight} kg
- Age: ${profile.age}
- Gender: ${profile.gender}
- Goal: ${profile.fitnessGoals?.[0]}
- Goal: ${profile.fitnessGoal}
- Activity Level: ${profile.activityLevel}
- Medical Conditions: ${profile.medicalConditions || "None"}
- Injuries: ${profile.injuries || "None"}
@@ -1,181 +0,0 @@
/**
* @jest-environment node
*/
import { DrizzleDatabase } from '../drizzle'
import { DatabaseConfig } from '../types'
import Database from 'better-sqlite3'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import * as schema from '@fitai/database'
describe('DrizzleDatabase', () => {
let db: DrizzleDatabase
let sqlite: Database.Database
let drizzleDb: ReturnType<typeof drizzle>
beforeEach(async () => {
// Create in-memory SQLite database
sqlite = new Database(':memory:')
drizzleDb = drizzle(sqlite, { schema })
const config: DatabaseConfig = {
type: 'sqlite',
connection: { filename: ':memory:' },
options: { logging: false }
}
// Initialize DrizzleDatabase with the in-memory db
db = new DrizzleDatabase(config, drizzleDb as any)
await db.connect()
})
afterEach(async () => {
await db.disconnect()
sqlite.close()
})
describe('User Operations', () => {
it('should create and retrieve a user', async () => {
const newUser = {
email: 'test@example.com',
firstName: 'Test',
lastName: 'User',
password: 'password123',
role: 'client' as const
}
const createdUser = await db.createUser(newUser)
expect(createdUser).toBeDefined()
expect(createdUser.id).toBeDefined()
expect(createdUser.email).toBe(newUser.email)
const fetchedUser = await db.getUserById(createdUser.id)
expect(fetchedUser).toBeDefined()
expect(fetchedUser?.id).toBe(createdUser.id)
expect(fetchedUser?.email).toBe(createdUser.email)
// SQLite/Drizzle might truncate ms or handle dates differently in test env
expect(new Date(fetchedUser!.createdAt).getTime()).toBeLessThanOrEqual(createdUser.createdAt.getTime())
expect(new Date(fetchedUser!.updatedAt).getTime()).toBeLessThanOrEqual(createdUser.updatedAt.getTime())
})
it('should update a user', async () => {
const user = await db.createUser({
email: 'update@example.com',
firstName: 'Update',
lastName: 'Me',
password: 'password',
role: 'client'
})
const updated = await db.updateUser(user.id, { firstName: 'Updated' })
expect(updated?.firstName).toBe('Updated')
const fetched = await db.getUserById(user.id)
expect(fetched?.firstName).toBe('Updated')
})
it('should delete a user', async () => {
const user = await db.createUser({
email: 'delete@example.com',
firstName: 'Delete',
lastName: 'Me',
password: 'password',
role: 'client'
})
const deleted = await db.deleteUser(user.id)
expect(deleted).toBe(true)
const fetched = await db.getUserById(user.id)
expect(fetched).toBeNull()
})
})
describe('Client Operations', () => {
it('should create and retrieve a client', async () => {
const user = await db.createUser({
email: 'client@example.com',
firstName: 'Client',
lastName: 'User',
password: 'password',
role: 'client'
})
const newClient = {
userId: user.id,
membershipType: 'basic' as const,
membershipStatus: 'active' as const,
joinDate: new Date()
}
const createdClient = await db.createClient(newClient)
expect(createdClient).toBeDefined()
expect(createdClient.userId).toBe(user.id)
const fetchedClient = await db.getClientById(createdClient.id)
expect(fetchedClient).toBeDefined()
expect(fetchedClient?.id).toBe(createdClient.id)
expect(fetchedClient?.userId).toBe(createdClient.userId)
expect(fetchedClient?.membershipType).toBe(createdClient.membershipType)
})
})
describe('Fitness Profile Operations', () => {
it('should create and retrieve a fitness profile', async () => {
const user = await db.createUser({
email: 'profile@example.com',
firstName: 'Profile',
lastName: 'User',
password: 'password',
role: 'client'
})
const newProfile = {
userId: user.id,
height: 180,
weight: 75,
age: 30,
gender: 'male' as const,
activityLevel: 'moderately_active' as const,
fitnessGoals: ['weight_loss'],
exerciseHabits: 'None',
dietHabits: 'None',
medicalConditions: 'None'
}
const createdProfile = await db.createFitnessProfile(newProfile as any)
expect(createdProfile).toBeDefined()
expect(createdProfile.userId).toBe(user.id)
const fetchedProfile = await db.getFitnessProfileByUserId(user.id)
expect(fetchedProfile).toBeDefined()
expect(fetchedProfile?.userId).toBe(user.id)
expect(fetchedProfile?.fitnessGoals).toEqual(expect.arrayContaining(['weight_loss']))
})
})
describe('Attendance Operations', () => {
it('should check in and check out', async () => {
const user = await db.createUser({
email: 'attendance@example.com',
firstName: 'Attendance',
lastName: 'User',
password: 'password',
role: 'client'
})
const checkIn = await db.checkIn(user.id, 'gym')
expect(checkIn).toBeDefined()
expect(checkIn.checkOutTime).toBeUndefined()
const activeCheckIn = await db.getActiveCheckIn(user.id)
expect(activeCheckIn).toBeDefined()
expect(activeCheckIn?.id).toBe(checkIn.id)
const checkOut = await db.checkOut(checkIn.id)
expect(checkOut?.checkOutTime).toBeDefined()
const activeCheckInAfter = await db.getActiveCheckIn(user.id)
expect(activeCheckInAfter).toBeNull()
})
})
})
-555
View File
@@ -1,555 +0,0 @@
import { IDatabase, User, Client, FitnessProfile, Attendance, Recommendation, FitnessGoal, DatabaseConfig } from './types'
import { db as defaultDb, users, clients, fitnessProfiles, attendance, recommendations, fitnessGoals, eq, and, desc, sql } from '@fitai/database'
import { InferSelectModel } from 'drizzle-orm'
export class DrizzleDatabase implements IDatabase {
private config: DatabaseConfig
private db: typeof defaultDb
constructor(config: DatabaseConfig, db?: typeof defaultDb) {
this.config = config
this.db = db || defaultDb
}
async connect(): Promise<void> {
// Drizzle with better-sqlite3 connects synchronously on initialization
// We can just log here if needed
if (this.config.options?.logging) {
console.log('Drizzle database connected')
}
await this.createTables()
}
async disconnect(): Promise<void> {
// better-sqlite3 handle is managed by Drizzle, usually no explicit disconnect needed for connection pooling
// but we can close the underlying sqlite instance if we had access to it.
// For now, we'll assume it's handled.
if (this.config.options?.logging) {
console.log('Drizzle database disconnected')
}
}
private async createTables(): Promise<void> {
// Users table
await this.db.run(sql`
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
password TEXT,
phone TEXT,
role TEXT NOT NULL CHECK (role IN ('superAdmin', 'admin', 'trainer', 'client')),
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
)
`)
// Clients table
await this.db.run(sql`
CREATE TABLE IF NOT EXISTS clients (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
membership_type TEXT NOT NULL CHECK (membership_type IN ('basic', 'premium', 'vip')),
membership_status TEXT NOT NULL CHECK (membership_status IN ('active', 'inactive', 'suspended')),
join_date INTEGER NOT NULL,
last_visit INTEGER,
emergency_contact_name TEXT,
emergency_contact_phone TEXT,
emergency_contact_relationship TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
)
`)
// Fitness profiles table
await this.db.run(sql`
CREATE TABLE IF NOT EXISTS fitness_profiles (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL UNIQUE,
height REAL,
weight REAL,
age INTEGER,
gender TEXT CHECK (gender IN ('male', 'female', 'other', 'prefer_not_to_say')),
activity_level TEXT CHECK (activity_level IN ('sedentary', 'lightly_active', 'moderately_active', 'very_active', 'extremely_active')),
fitness_goals TEXT,
exercise_habits TEXT,
diet_habits TEXT,
medical_conditions TEXT,
allergies TEXT,
injuries TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
)
`)
// Attendance table
await this.db.run(sql`
CREATE TABLE IF NOT EXISTS attendance (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
check_in_time INTEGER NOT NULL,
check_out_time INTEGER,
type TEXT NOT NULL CHECK (type IN ('gym', 'class', 'personal_training')),
notes TEXT,
created_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
)
`)
// Recommendations table
await this.db.run(sql`
CREATE TABLE IF NOT EXISTS recommendations (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
fitness_profile_id TEXT NOT NULL,
recommendation_text TEXT NOT NULL,
activity_plan TEXT NOT NULL,
diet_plan TEXT NOT NULL,
status TEXT NOT NULL CHECK (status IN ('pending', 'approved', 'rejected')) DEFAULT 'pending',
generated_at INTEGER NOT NULL,
approved_at INTEGER,
approved_by TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (fitness_profile_id) REFERENCES fitness_profiles (id) ON DELETE CASCADE
)
`)
// Fitness Goals table
await this.db.run(sql`
CREATE TABLE IF NOT EXISTS fitness_goals (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
fitness_profile_id TEXT,
goal_type TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT,
target_value REAL,
current_value REAL,
unit TEXT,
start_date INTEGER NOT NULL,
target_date INTEGER,
completed_date INTEGER,
status TEXT NOT NULL DEFAULT 'active',
progress REAL DEFAULT 0,
priority TEXT DEFAULT 'medium',
notes TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (fitness_profile_id) REFERENCES fitness_profiles (id) ON DELETE CASCADE
)
`)
}
// User operations
async createUser(userData: Omit<User, "createdAt" | "updatedAt" | "id"> & { id?: string }): Promise<User> {
const id = userData.id || Math.random().toString(36).substr(2, 9)
const now = new Date()
const newUser = {
...userData,
id,
createdAt: now,
updatedAt: now
}
await this.db.insert(users).values(newUser)
return newUser
}
async getUserById(id: string): Promise<User | null> {
const result = await this.db.select().from(users).where(eq(users.id, id)).get()
return result ? this.mapUser(result) : null
}
async getUserByEmail(email: string): Promise<User | null> {
const result = await this.db.select().from(users).where(eq(users.email, email)).get()
return result ? this.mapUser(result) : null
}
async getAllUsers(): Promise<User[]> {
const results = await this.db.select().from(users).orderBy(desc(users.createdAt)).all()
return results.map(this.mapUser)
}
async updateUser(id: string, updates: Partial<User>): Promise<User | null> {
const { id: _, ...updateData } = updates
if (Object.keys(updateData).length === 0) return this.getUserById(id)
await this.db.update(users)
.set({ ...updateData, updatedAt: new Date() })
.where(eq(users.id, id))
.run()
return this.getUserById(id)
}
async deleteUser(id: string): Promise<boolean> {
const result = await this.db.delete(users).where(eq(users.id, id)).run()
return result.changes > 0
}
async migrateUserId(oldId: string, newId: string): Promise<void> {
await this.db.update(users).set({ id: newId }).where(eq(users.id, oldId)).run()
}
// Client operations
async createClient(clientData: Omit<Client, 'id'>): Promise<Client> {
const id = Math.random().toString(36).substr(2, 9)
const newClient = {
id,
...clientData,
createdAt: new Date(),
updatedAt: new Date()
}
await this.db.insert(clients).values(newClient as any)
return this.mapClient(newClient)
}
async getClientById(id: string): Promise<Client | null> {
const result = await this.db.select().from(clients).where(eq(clients.id, id)).get()
return result ? this.mapClient(result) : null
}
async getClientByUserId(userId: string): Promise<Client | null> {
const result = await this.db.select().from(clients).where(eq(clients.userId, userId)).get()
return result ? this.mapClient(result) : null
}
async getAllClients(): Promise<Client[]> {
const results = await this.db.select().from(clients).orderBy(desc(clients.joinDate)).all()
return results.map(this.mapClient)
}
async updateClient(id: string, updates: Partial<Client>): Promise<Client | null> {
const { id: _, ...updateData } = updates
if (Object.keys(updateData).length === 0) return this.getClientById(id)
await this.db.update(clients)
.set({ ...updateData, updatedAt: new Date() } as any)
.where(eq(clients.id, id))
.run()
return this.getClientById(id)
}
async deleteClient(id: string): Promise<boolean> {
const result = await this.db.delete(clients).where(eq(clients.id, id)).run()
return result.changes > 0
}
// Fitness Profile operations
async createFitnessProfile(profileData: Omit<FitnessProfile, 'id' | 'createdAt' | 'updatedAt'>): Promise<FitnessProfile> {
const now = new Date()
const id = Math.random().toString(36).substr(2, 9)
const newProfile = {
id,
...profileData,
createdAt: now,
updatedAt: now
}
await this.db.insert(fitnessProfiles).values(newProfile as any)
return { id, ...profileData, createdAt: now, updatedAt: now }
}
async getFitnessProfileByUserId(userId: string): Promise<FitnessProfile | null> {
const result = await this.db.select().from(fitnessProfiles).where(eq(fitnessProfiles.userId, userId)).get()
return result ? this.mapFitnessProfile(result) : null
}
async getAllFitnessProfiles(): Promise<FitnessProfile[]> {
const results = await this.db.select().from(fitnessProfiles).orderBy(desc(fitnessProfiles.createdAt)).all()
return results.map(this.mapFitnessProfile)
}
async updateFitnessProfile(userId: string, updates: Partial<FitnessProfile>): Promise<FitnessProfile | null> {
const { userId: _, ...updateData } = updates
if (Object.keys(updateData).length === 0) return this.getFitnessProfileByUserId(userId)
const dbUpdates = { ...updateData } as any
if (updateData.fitnessGoals) {
dbUpdates.fitnessGoals = JSON.stringify(updateData.fitnessGoals)
}
await this.db.update(fitnessProfiles)
.set({ ...dbUpdates, updatedAt: new Date() })
.where(eq(fitnessProfiles.userId, userId))
.run()
return this.getFitnessProfileByUserId(userId)
}
async deleteFitnessProfile(userId: string): Promise<boolean> {
const result = await this.db.delete(fitnessProfiles).where(eq(fitnessProfiles.userId, userId)).run()
return result.changes > 0
}
// Attendance operations
async checkIn(userId: string, type: "gym" | "class" | "personal_training", notes?: string): Promise<Attendance> {
const id = Math.random().toString(36).substr(2, 9)
const now = new Date()
const newAttendance = {
id,
userId,
checkInTime: now,
type,
notes,
createdAt: now
}
await this.db.insert(attendance).values(newAttendance as any)
// Update client last visit
const client = await this.getClientByUserId(userId)
if (client) {
await this.updateClient(client.id, { lastVisit: now })
}
return newAttendance
}
async checkOut(attendanceId: string): Promise<Attendance | null> {
const now = new Date()
await this.db.update(attendance)
.set({ checkOutTime: now })
.where(eq(attendance.id, attendanceId))
.run()
return this.getAttendanceById(attendanceId)
}
async getAttendanceById(id: string): Promise<Attendance | null> {
const result = await this.db.select().from(attendance).where(eq(attendance.id, id)).get()
return result ? this.mapAttendance(result) : null
}
async getAttendanceHistory(userId: string): Promise<Attendance[]> {
const results = await this.db.select().from(attendance)
.where(eq(attendance.userId, userId))
.orderBy(desc(attendance.checkInTime))
.all()
return results.map(this.mapAttendance)
}
async getAllAttendance(): Promise<Attendance[]> {
const results = await this.db.select().from(attendance)
.orderBy(desc(attendance.checkInTime))
.all()
return results.map(this.mapAttendance)
}
async getActiveCheckIn(userId: string): Promise<Attendance | null> {
// Drizzle doesn't support IS NULL in where directly with simple syntax sometimes, but eq(col, null) works or isNull(col)
// We need to check how to filter for null checkOutTime.
// In Drizzle, we can filter in JS or use isNull operator if imported.
// Let's fetch recent and filter for now to be safe, or use raw sql if needed, but better to use Drizzle operators.
// Actually, we can just fetch all for user and filter in memory since it's unlikely to be huge for active checkins,
// but correct way is `isNull(attendance.checkOutTime)`.
// Since I didn't import `isNull`, I'll fetch recent history and find first active.
const history = await this.getAttendanceHistory(userId)
return history.find(a => !a.checkOutTime) || null
}
// Recommendation operations
async createRecommendation(data: Omit<Recommendation, "createdAt" | "approvedAt" | "approvedBy">): Promise<Recommendation> {
const now = new Date()
const newRec = {
...data,
createdAt: now,
status: data.status || 'pending'
}
await this.db.insert(recommendations).values(newRec as any)
return newRec as Recommendation
}
async getRecommendationsByUserId(userId: string): Promise<Recommendation[]> {
const results = await this.db.select().from(recommendations)
.where(eq(recommendations.userId, userId))
.orderBy(desc(recommendations.createdAt))
.all()
return results.map(this.mapRecommendation)
}
async getAllRecommendations(): Promise<Recommendation[]> {
const results = await this.db.select().from(recommendations)
.orderBy(desc(recommendations.createdAt))
.all()
return results.map(this.mapRecommendation)
}
async updateRecommendation(id: string, updates: Partial<Recommendation>): Promise<Recommendation | null> {
const { id: _, ...updateData } = updates
if (Object.keys(updateData).length === 0) return this.getRecommendationById(id)
await this.db.update(recommendations)
.set(updateData as any)
.where(eq(recommendations.id, id))
.run()
return this.getRecommendationById(id)
}
async deleteRecommendation(id: string): Promise<boolean> {
const result = await this.db.delete(recommendations).where(eq(recommendations.id, id)).run()
return result.changes > 0
}
async getRecommendationById(id: string): Promise<Recommendation | null> {
const result = await this.db.select().from(recommendations).where(eq(recommendations.id, id)).get()
return result ? this.mapRecommendation(result) : null
}
// Fitness Goals operations
async createFitnessGoal(goalData: Omit<FitnessGoal, "createdAt" | "updatedAt">): Promise<FitnessGoal> {
const now = new Date()
const newGoal = {
...goalData,
createdAt: now,
updatedAt: now
}
await this.db.insert(fitnessGoals).values(newGoal as any)
return newGoal as FitnessGoal
}
async getFitnessGoalById(id: string): Promise<FitnessGoal | null> {
const result = await this.db.select().from(fitnessGoals).where(eq(fitnessGoals.id, id)).get()
return result ? this.mapFitnessGoal(result) : null
}
async getFitnessGoalsByUserId(userId: string, status?: string): Promise<FitnessGoal[]> {
let query = this.db.select().from(fitnessGoals).where(eq(fitnessGoals.userId, userId))
if (status) {
query = this.db.select().from(fitnessGoals).where(and(eq(fitnessGoals.userId, userId), eq(fitnessGoals.status, status as any)))
}
const results = await query.orderBy(desc(fitnessGoals.createdAt)).all()
return results.map(this.mapFitnessGoal)
}
async updateFitnessGoal(id: string, updates: Partial<FitnessGoal>): Promise<FitnessGoal | null> {
const { id: _, ...updateData } = updates
if (Object.keys(updateData).length === 0) return this.getFitnessGoalById(id)
await this.db.update(fitnessGoals)
.set({ ...updateData, updatedAt: new Date() } as any)
.where(eq(fitnessGoals.id, id))
.run()
return this.getFitnessGoalById(id)
}
async deleteFitnessGoal(id: string): Promise<boolean> {
const result = await this.db.delete(fitnessGoals).where(eq(fitnessGoals.id, id)).run()
return result.changes > 0
}
async updateGoalProgress(id: string, currentValue: number): Promise<FitnessGoal | null> {
const goal = await this.getFitnessGoalById(id)
if (!goal) return null
let progress = goal.progress
if (goal.targetValue && goal.targetValue > 0) {
progress = Math.min(100, (currentValue / goal.targetValue) * 100)
}
await this.db.update(fitnessGoals)
.set({ currentValue, progress, updatedAt: new Date() })
.where(eq(fitnessGoals.id, id))
.run()
return this.getFitnessGoalById(id)
}
async completeGoal(id: string): Promise<FitnessGoal | null> {
const now = new Date()
await this.db.update(fitnessGoals)
.set({ status: 'completed', progress: 100, completedDate: now, updatedAt: now })
.where(eq(fitnessGoals.id, id))
.run()
return this.getFitnessGoalById(id)
}
async getDashboardStats(): Promise<{ totalUsers: number; activeClients: number; totalRevenue: number; revenueGrowth: number }> {
// Placeholder implementation as per original sqlite.ts (which didn't implement this either in the viewed snippet,
// but interface requires it. I'll provide a basic implementation or mock).
// The original sqlite.ts snippet ended before showing this method, but the interface has it.
// I'll implement a basic count.
const allUsers = await this.db.select().from(users).all()
const activeClients = await this.db.select().from(clients).where(eq(clients.membershipStatus, 'active')).all()
return {
totalUsers: allUsers.length,
activeClients: activeClients.length,
totalRevenue: 0, // Not tracking payments yet
revenueGrowth: 0
}
}
// Mappers
private mapUser(row: any): User {
return {
...row,
createdAt: new Date(row.createdAt),
updatedAt: new Date(row.updatedAt)
}
}
private mapClient(row: any): Client {
return {
...row,
joinDate: new Date(row.joinDate),
lastVisit: row.lastVisit ? new Date(row.lastVisit) : undefined
}
}
private mapFitnessProfile(row: any): FitnessProfile {
return {
...row,
createdAt: new Date(row.createdAt),
updatedAt: new Date(row.updatedAt)
}
}
private mapAttendance(row: any): Attendance {
return {
...row,
checkInTime: new Date(row.checkInTime),
checkOutTime: row.checkOutTime ? new Date(row.checkOutTime) : undefined,
createdAt: new Date(row.createdAt)
}
}
private mapRecommendation(row: any): Recommendation {
return {
...row,
createdAt: new Date(row.createdAt),
approvedAt: row.approvedAt ? new Date(row.approvedAt) : undefined
}
}
private mapFitnessGoal(row: any): FitnessGoal {
return {
...row,
startDate: new Date(row.startDate),
targetDate: row.targetDate ? new Date(row.targetDate) : undefined,
completedDate: row.completedDate ? new Date(row.completedDate) : undefined,
createdAt: new Date(row.createdAt),
updatedAt: new Date(row.updatedAt)
}
}
}
+8 -8
View File
@@ -1,5 +1,5 @@
import { IDatabase, DatabaseConfig } from './types'
import { DrizzleDatabase } from './drizzle'
import { SQLiteDatabase } from './sqlite'
// Database factory - creates appropriate database instance based on config
export class DatabaseFactory {
@@ -20,28 +20,28 @@ export class DatabaseFactory {
// Create new database instance based on type
switch (config.type) {
case 'sqlite':
this.instance = new DrizzleDatabase(config)
this.instance = new SQLiteDatabase(config)
break
case 'postgresql':
// TODO: Implement PostgreSQLDatabase
throw new Error('PostgreSQL implementation not yet available')
case 'mysql':
// TODO: Implement MySQLDatabase
throw new Error('MySQL implementation not yet available')
case 'mongodb':
// TODO: Implement MongoDBDatabase
throw new Error('MongoDB implementation not yet available')
default:
throw new Error(`Unsupported database type: ${config.type}`)
}
await this.instance.connect()
this.config = config
return this.instance
}
@@ -62,7 +62,7 @@ export class DatabaseFactory {
private static isSameConfig(config: DatabaseConfig): boolean {
if (!this.config) return false
return (
this.config.type === config.type &&
JSON.stringify(this.config.connection) === JSON.stringify(config.connection)
+868
View File
@@ -0,0 +1,868 @@
import Database from 'better-sqlite3'
import path from 'path'
import fs from 'fs'
import { IDatabase, User, Client, FitnessProfile, Attendance, Recommendation, DatabaseConfig } from './types'
export class SQLiteDatabase implements IDatabase {
private db: Database.Database | null = null
private config: DatabaseConfig
constructor(config: DatabaseConfig) {
this.config = config
}
async connect(): Promise<void> {
try {
const dbPath = this.config.connection.filename || path.join(process.cwd(), 'data', 'fitai.db')
// Ensure data directory exists
const dataDir = path.dirname(dbPath)
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true })
}
this.db = new Database(dbPath)
// Enable foreign keys
this.db.exec('PRAGMA foreign_keys = ON')
// Create tables
await this.createTables()
if (this.config.options?.logging) {
console.log('SQLite database connected successfully at:', dbPath)
}
} catch (error) {
console.error('Failed to connect to SQLite database:', error)
throw error
}
}
async disconnect(): Promise<void> {
if (this.db) {
this.db.close()
this.db = null
if (this.config.options?.logging) {
console.log('SQLite database disconnected')
}
}
}
private async createTables(): Promise<void> {
if (!this.db) throw new Error('Database not connected')
// Users table
this.db.exec(`
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
firstName TEXT NOT NULL,
lastName TEXT NOT NULL,
password TEXT NOT NULL,
phone TEXT,
role TEXT NOT NULL CHECK (role IN ('superAdmin', 'admin', 'trainer', 'client')),
createdAt DATETIME NOT NULL,
updatedAt DATETIME NOT NULL
)
`)
// Clients table
this.db.exec(`
CREATE TABLE IF NOT EXISTS clients (
id TEXT PRIMARY KEY,
userId TEXT NOT NULL,
membershipType TEXT NOT NULL CHECK (membershipType IN ('basic', 'premium', 'vip')),
membershipStatus TEXT NOT NULL CHECK (membershipStatus IN ('active', 'inactive', 'expired')),
joinDate DATETIME NOT NULL,
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
)
`)
// Fitness profiles table
this.db.exec(`
CREATE TABLE IF NOT EXISTS fitness_profiles (
userId TEXT PRIMARY KEY,
height TEXT NOT NULL,
weight TEXT NOT NULL,
age TEXT NOT NULL,
gender TEXT NOT NULL CHECK (gender IN ('male', 'female', 'other')),
activityLevel TEXT NOT NULL CHECK (activityLevel IN ('sedentary', 'light', 'moderate', 'active', 'very_active')),
fitnessGoals TEXT NOT NULL, -- JSON array
exerciseHabits TEXT,
dietHabits TEXT,
medicalConditions TEXT,
allergies TEXT,
injuries TEXT,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NOT NULL,
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
)
`)
// Create indexes for better performance
this.db.exec(`
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_clients_userId ON clients(userId);
CREATE INDEX IF NOT EXISTS idx_fitness_profiles_userId ON fitness_profiles(userId);
`)
// Attendance table migration: change from clientId to userId
// Check if old table exists and migrate
const tableInfo = this.db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='attendance'").get() as any;
if (tableInfo) {
// Check if table has clientId column (old schema)
const columns = this.db.prepare("PRAGMA table_info(attendance)").all() as any[];
const hasClientId = columns.some((col: any) => col.name === 'clientId');
if (hasClientId) {
console.log('Migrating attendance table from clientId to userId...');
// Create new table with userId
this.db.exec(`
CREATE TABLE IF NOT EXISTS attendance_new (
id TEXT PRIMARY KEY,
userId TEXT NOT NULL,
checkInTime DATETIME NOT NULL,
checkOutTime DATETIME,
type TEXT NOT NULL CHECK (type IN ('gym', 'class', 'personal_training')),
notes TEXT,
createdAt DATETIME NOT NULL,
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
)
`);
// Migrate data: map clientId to userId via clients table
this.db.exec(`
INSERT INTO attendance_new (id, userId, checkInTime, checkOutTime, type, notes, createdAt)
SELECT a.id, c.userId, a.checkInTime, a.checkOutTime, a.type, a.notes, a.createdAt
FROM attendance a
JOIN clients c ON a.clientId = c.id
`);
// Drop old table and rename new one
this.db.exec(`DROP TABLE attendance`);
this.db.exec(`ALTER TABLE attendance_new RENAME TO attendance`);
console.log('Attendance table migration completed');
}
} else {
// Create new attendance table with userId
this.db.exec(`
CREATE TABLE IF NOT EXISTS attendance (
id TEXT PRIMARY KEY,
userId TEXT NOT NULL,
checkInTime DATETIME NOT NULL,
checkOutTime DATETIME,
type TEXT NOT NULL CHECK (type IN ('gym', 'class', 'personal_training')),
notes TEXT,
createdAt DATETIME NOT NULL,
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
)
`);
}
// Recommendations table
// Removed DROP TABLE to persist data. Schema is now stable.
// this.db.exec(`DROP TABLE IF EXISTS recommendations`)
this.db.exec(`
CREATE TABLE IF NOT EXISTS recommendations (
id TEXT PRIMARY KEY,
userId TEXT NOT NULL,
fitnessProfileId TEXT,
type TEXT NOT NULL,
content TEXT NOT NULL,
activityPlan TEXT,
dietPlan TEXT,
status TEXT NOT NULL CHECK (status IN ('pending', 'approved', 'rejected', 'completed')),
createdAt DATETIME NOT NULL,
approvedAt DATETIME,
approvedBy TEXT,
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (fitnessProfileId) REFERENCES fitness_profiles (userId)
)
`);
this.db.exec(`
CREATE INDEX IF NOT EXISTS idx_recommendations_userId ON recommendations(userId);
`);
this.db.exec(`
CREATE INDEX IF NOT EXISTS idx_attendance_userId ON attendance(userId);
CREATE INDEX IF NOT EXISTS idx_attendance_checkInTime ON attendance(checkInTime);
`);
}
// User operations
async createUser(
userData: Omit<User, "createdAt" | "updatedAt" | "id"> & { id?: string },
): Promise<User> {
if (!this.db) throw new Error('Database not connected')
const id = userData.id || Math.random().toString(36).substr(2, 9)
const now = new Date()
const user: User = {
...userData,
id,
createdAt: now,
updatedAt: now
}
const stmt = this.db.prepare(
`INSERT INTO users(id, email, firstName, lastName, password, phone, role, createdAt, updatedAt)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)`
)
stmt.run(
user.id, user.email, user.firstName, user.lastName, user.password,
user.phone, user.role, user.createdAt.toISOString(), user.updatedAt.toISOString()
)
return user
}
async getUserById(id: string): Promise<User | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM users WHERE id = ?')
const row = stmt.get(id)
return row ? this.mapRowToUser(row) : null
}
async getUserByEmail(email: string): Promise<User | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM users WHERE email = ?')
const row = stmt.get(email)
return row ? this.mapRowToUser(row) : null
}
async getAllUsers(): Promise<User[]> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM users ORDER BY createdAt DESC')
const rows = stmt.all()
return rows.map(row => this.mapRowToUser(row))
}
async updateUser(id: string, updates: Partial<User>): Promise<User | null> {
if (!this.db) throw new Error('Database not connected')
const fields = Object.keys(updates).filter(key => key !== 'id')
if (fields.length === 0) return this.getUserById(id)
const setClause = fields.map(field => `${field} = ?`).join(', ')
const values = fields.map(field => (updates as any)[field])
values.push(new Date().toISOString()) // updatedAt
values.push(id)
const stmt = this.db.prepare(`UPDATE users SET ${setClause}, updatedAt = ? WHERE id = ? `)
stmt.run(values)
return this.getUserById(id)
}
async deleteUser(id: string): Promise<boolean> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('DELETE FROM users WHERE id = ?')
const result = stmt.run(id)
return (result.changes || 0) > 0
}
async migrateUserId(oldId: string, newId: string): Promise<void> {
if (!this.db) throw new Error('Database not connected')
// We need to disable foreign keys temporarily if we want to update ID without cascade (if cascade isn't set)
// But we should try to update and let cascade handle it if possible.
// Since we didn't set ON UPDATE CASCADE, we might need to manually update references or use PRAGMA.
// Simplest way: Update the ID. If it fails due to FK, we have to handle it.
// For the Super Admin seed case, there are no dependencies.
const stmt = this.db.prepare('UPDATE users SET id = ? WHERE id = ?')
stmt.run(newId, oldId)
}
// Client operations
async createClient(clientData: Omit<Client, 'id'>): Promise<Client> {
if (!this.db) throw new Error('Database not connected')
const id = Math.random().toString(36).substr(2, 9)
const client: Client = { id, ...clientData }
const stmt = this.db.prepare(
`INSERT INTO clients(id, userId, membershipType, membershipStatus, joinDate)
VALUES(?, ?, ?, ?, ?)`
)
stmt.run(
client.id, client.userId, client.membershipType,
client.membershipStatus, client.joinDate.toISOString()
)
return client
}
async getClientById(id: string): Promise<Client | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM clients WHERE id = ?')
const row = stmt.get(id)
return row ? this.mapRowToClient(row) : null
}
async getClientByUserId(userId: string): Promise<Client | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM clients WHERE userId = ?')
const row = stmt.get(userId)
return row ? this.mapRowToClient(row) : null
}
async getAllClients(): Promise<Client[]> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM clients ORDER BY joinDate DESC')
const rows = stmt.all()
return rows.map(row => this.mapRowToClient(row))
}
async updateClient(id: string, updates: Partial<Client>): Promise<Client | null> {
if (!this.db) throw new Error('Database not connected')
const fields = Object.keys(updates).filter(key => key !== 'id')
if (fields.length === 0) return this.getClientById(id)
const setClause = fields.map(field => `${field} = ?`).join(', ')
const values = fields.map(field => (updates as any)[field])
values.push(id)
const stmt = this.db.prepare(`UPDATE clients SET ${setClause} WHERE id = ? `)
stmt.run(values)
return this.getClientById(id)
}
async deleteClient(id: string): Promise<boolean> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('DELETE FROM clients WHERE id = ?')
const result = stmt.run(id)
return (result.changes || 0) > 0
}
// Fitness Profile operations
async createFitnessProfile(profileData: Omit<FitnessProfile, 'createdAt' | 'updatedAt'>): Promise<FitnessProfile> {
if (!this.db) throw new Error('Database not connected')
const now = new Date()
const profile: FitnessProfile = {
...profileData,
createdAt: now,
updatedAt: now
}
const stmt = this.db.prepare(
`INSERT INTO fitness_profiles
(userId, height, weight, age, gender, activityLevel, fitnessGoals,
exerciseHabits, dietHabits, medicalConditions, allergies, injuries, createdAt, updatedAt)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
)
stmt.run(
profile.userId, profile.height, profile.weight, profile.age, profile.gender,
profile.activityLevel, JSON.stringify(profile.fitnessGoals), profile.exerciseHabits,
profile.dietHabits, profile.medicalConditions, profile.allergies, profile.injuries,
profile.createdAt.toISOString(), profile.updatedAt.toISOString()
)
return profile
}
async getFitnessProfileByUserId(userId: string): Promise<FitnessProfile | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM fitness_profiles WHERE userId = ?')
const row = stmt.get(userId)
return row ? this.mapRowToFitnessProfile(row) : null
}
async getAllFitnessProfiles(): Promise<FitnessProfile[]> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM fitness_profiles ORDER BY createdAt DESC')
const rows = stmt.all()
return rows.map(row => this.mapRowToFitnessProfile(row))
}
async updateFitnessProfile(userId: string, updates: Partial<FitnessProfile>): Promise<FitnessProfile | null> {
if (!this.db) throw new Error('Database not connected')
const fields = Object.keys(updates).filter(key => key !== 'userId' && key !== 'createdAt')
if (fields.length === 0) return this.getFitnessProfileByUserId(userId)
const setClause = fields.map(field => `${field} = ?`).join(', ')
const values = fields.map(field => {
const value = (updates as any)[field]
return field === 'fitnessGoals' ? JSON.stringify(value) : value
})
values.push(new Date().toISOString()) // updatedAt
values.push(userId)
const stmt = this.db.prepare(`UPDATE fitness_profiles SET ${setClause}, updatedAt = ? WHERE userId = ? `)
stmt.run(values)
return this.getFitnessProfileByUserId(userId)
}
async deleteFitnessProfile(userId: string): Promise<boolean> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('DELETE FROM fitness_profiles WHERE userId = ?')
const result = stmt.run(userId)
return (result.changes || 0) > 0
}
// Attendance operations
async checkIn(userId: string, type: 'gym' | 'class' | 'personal_training', notes?: string): Promise<Attendance> {
if (!this.db) throw new Error('Database not connected')
const id = Math.random().toString(36).substr(2, 9)
const now = new Date()
const attendance: Attendance = {
id,
userId,
checkInTime: now,
type,
notes,
createdAt: now
}
const stmt = this.db.prepare(
`INSERT INTO attendance(id, userId, checkInTime, type, notes, createdAt)
VALUES(?, ?, ?, ?, ?, ?)`
)
stmt.run(
attendance.id, attendance.userId, attendance.checkInTime.toISOString(),
attendance.type, attendance.notes, attendance.createdAt.toISOString()
)
// Update client last visit if user is a client
const client = await this.getClientByUserId(userId);
if (client) {
this.db.prepare('UPDATE clients SET lastVisit = ? WHERE id = ?').run(
now.toISOString(),
client.id
);
}
return attendance
}
async checkOut(attendanceId: string): Promise<Attendance | null> {
if (!this.db) throw new Error('Database not connected')
const now = new Date()
const stmt = this.db.prepare('UPDATE attendance SET checkOutTime = ? WHERE id = ?')
stmt.run(now.toISOString(), attendanceId)
return this.getAttendanceById(attendanceId)
}
async getAttendanceById(id: string): Promise<Attendance | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM attendance WHERE id = ?')
const row = stmt.get(id)
return row ? this.mapRowToAttendance(row) : null
}
async getAttendanceHistory(userId: string): Promise<Attendance[]> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM attendance WHERE userId = ? ORDER BY checkInTime DESC')
const rows = stmt.all(userId)
return rows.map(row => this.mapRowToAttendance(row))
}
async getAllAttendance(): Promise<Attendance[]> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM attendance ORDER BY checkInTime DESC')
const rows = stmt.all()
return rows.map(row => this.mapRowToAttendance(row))
}
async getActiveCheckIn(userId: string): Promise<Attendance | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM attendance WHERE userId = ? AND checkOutTime IS NULL ORDER BY checkInTime DESC LIMIT 1')
const row = stmt.get(userId)
return row ? this.mapRowToAttendance(row) : null
}
// Helper methods to map database rows to entities
private mapRowToUser(row: any): User {
return {
id: row.id,
email: row.email,
firstName: row.firstName,
lastName: row.lastName,
password: row.password,
phone: row.phone,
role: row.role,
createdAt: new Date(row.createdAt),
updatedAt: new Date(row.updatedAt)
}
}
private mapRowToClient(row: any): Client {
return {
id: row.id,
userId: row.userId,
membershipType: row.membershipType,
membershipStatus: row.membershipStatus,
joinDate: new Date(row.joinDate),
lastVisit: row.lastVisit ? new Date(row.lastVisit) : undefined
}
}
private mapRowToFitnessProfile(row: any): FitnessProfile {
return {
userId: row.userId,
height: row.height,
weight: row.weight,
age: row.age,
gender: row.gender,
activityLevel: row.activityLevel,
fitnessGoals: JSON.parse(row.fitnessGoals || '[]'),
exerciseHabits: row.exerciseHabits,
dietHabits: row.dietHabits,
medicalConditions: row.medicalConditions,
allergies: row.allergies,
injuries: row.injuries,
createdAt: new Date(row.createdAt),
updatedAt: new Date(row.updatedAt)
}
}
private mapRowToAttendance(row: any): Attendance {
return {
id: row.id,
userId: row.userId,
checkInTime: new Date(row.checkInTime),
checkOutTime: row.checkOutTime ? new Date(row.checkOutTime) : undefined,
type: row.type,
notes: row.notes,
createdAt: new Date(row.createdAt)
}
}
// Recommendation operations
async createRecommendation(data: Omit<Recommendation, 'createdAt' | 'approvedAt' | 'approvedBy'>): Promise<Recommendation> {
if (!this.db) throw new Error('Database not connected')
const now = new Date()
const recommendation: Recommendation = {
...data,
createdAt: now,
status: data.status || 'pending'
}
const stmt = this.db.prepare(
`INSERT INTO recommendations (
id, userId, fitnessProfileId, type, content,
activityPlan, dietPlan, status, createdAt
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
)
stmt.run(
recommendation.id, recommendation.userId, recommendation.fitnessProfileId,
recommendation.type, recommendation.content, recommendation.activityPlan,
recommendation.dietPlan, recommendation.status,
recommendation.createdAt.toISOString()
)
return recommendation
}
async getRecommendationsByUserId(userId: string): Promise<Recommendation[]> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM recommendations WHERE userId = ? ORDER BY createdAt DESC')
const rows = stmt.all(userId)
return rows.map(row => this.mapRowToRecommendation(row))
}
async getAllRecommendations(): Promise<Recommendation[]> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM recommendations ORDER BY createdAt DESC')
const rows = stmt.all()
return rows.map(row => this.mapRowToRecommendation(row))
}
async updateRecommendation(id: string, updates: Partial<Recommendation>): Promise<Recommendation | null> {
if (!this.db) throw new Error('Database not connected')
const fields = Object.keys(updates).filter(key => key !== 'id' && key !== 'userId')
if (fields.length === 0) {
const stmt = this.db.prepare('SELECT * FROM recommendations WHERE id = ?')
const row = stmt.get(id)
return row ? this.mapRowToRecommendation(row) : null
}
const setClause = fields.map(field => `${field} = ?`).join(', ')
const values = fields.map(field => {
const val = (updates as any)[field]
return val instanceof Date ? val.toISOString() : val
})
values.push(id)
const stmt = this.db.prepare(`UPDATE recommendations SET ${setClause} WHERE id = ?`)
stmt.run(values)
const getStmt = this.db.prepare('SELECT * FROM recommendations WHERE id = ?')
const row = getStmt.get(id)
return row ? this.mapRowToRecommendation(row) : null
}
async deleteRecommendation(id: string): Promise<boolean> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('DELETE FROM recommendations WHERE id = ?')
const result = stmt.run(id)
return (result.changes || 0) > 0
}
private mapRowToRecommendation(row: any): Recommendation {
return {
id: row.id,
userId: row.userId,
fitnessProfileId: row.fitnessProfileId,
type: row.type,
content: row.content,
activityPlan: row.activityPlan,
dietPlan: row.dietPlan,
status: row.status,
createdAt: new Date(row.createdAt),
approvedAt: row.approvedAt ? new Date(row.approvedAt) : undefined,
approvedBy: row.approvedBy
}
}
// Fitness Goals operations
async createFitnessGoal(goalData: Omit<import('./types').FitnessGoal, 'createdAt' | 'updatedAt'>): Promise<import('./types').FitnessGoal> {
if (!this.db) throw new Error('Database not connected')
const now = new Date()
const goal: import('./types').FitnessGoal = {
...goalData,
createdAt: now,
updatedAt: now
}
const stmt = this.db.prepare(
`INSERT INTO fitness_goals (
id, user_id, fitness_profile_id, goal_type, title, description,
target_value, current_value, unit, start_date, target_date,
completed_date, status, progress, priority, notes, created_at, updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
)
stmt.run(
goal.id,
goal.userId,
goal.fitnessProfileId || null,
goal.goalType,
goal.title,
goal.description || null,
goal.targetValue || null,
goal.currentValue || null,
goal.unit || null,
goal.startDate.toISOString(),
goal.targetDate ? goal.targetDate.toISOString() : null,
goal.completedDate ? goal.completedDate.toISOString() : null,
goal.status,
goal.progress,
goal.priority,
goal.notes || null,
goal.createdAt.toISOString(),
goal.updatedAt.toISOString()
)
return goal
}
async getFitnessGoalById(id: string): Promise<import('./types').FitnessGoal | null> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('SELECT * FROM fitness_goals WHERE id = ?')
const row = stmt.get(id)
return row ? this.mapRowToFitnessGoal(row) : null
}
async getFitnessGoalsByUserId(userId: string, status?: string): Promise<import('./types').FitnessGoal[]> {
if (!this.db) throw new Error('Database not connected')
let query = 'SELECT * FROM fitness_goals WHERE user_id = ?'
const params: any[] = [userId]
if (status) {
query += ' AND status = ?'
params.push(status)
}
query += ' ORDER BY created_at DESC'
const stmt = this.db.prepare(query)
const rows = stmt.all(...params)
return rows.map(row => this.mapRowToFitnessGoal(row))
}
async updateFitnessGoal(id: string, updates: Partial<import('./types').FitnessGoal>): Promise<import('./types').FitnessGoal | null> {
if (!this.db) throw new Error('Database not connected')
const fields = Object.keys(updates).filter(key => key !== 'id' && key !== 'createdAt')
if (fields.length === 0) return this.getFitnessGoalById(id)
// Map camelCase to snake_case for database columns
const columnMap: Record<string, string> = {
userId: 'user_id',
fitnessProfileId: 'fitness_profile_id',
goalType: 'goal_type',
targetValue: 'target_value',
currentValue: 'current_value',
startDate: 'start_date',
targetDate: 'target_date',
completedDate: 'completed_date',
updatedAt: 'updated_at'
}
const setClause = fields.map(field => `${columnMap[field] || field} = ?`).join(', ')
const values = fields.map(field => {
const val = (updates as any)[field]
return val instanceof Date ? val.toISOString() : val
})
values.push(new Date().toISOString()) // updatedAt
values.push(id)
const stmt = this.db.prepare(`UPDATE fitness_goals SET ${setClause}, updated_at = ? WHERE id = ?`)
stmt.run(values)
return this.getFitnessGoalById(id)
}
async deleteFitnessGoal(id: string): Promise<boolean> {
if (!this.db) throw new Error('Database not connected')
const stmt = this.db.prepare('DELETE FROM fitness_goals WHERE id = ?')
const result = stmt.run(id)
return (result.changes || 0) > 0
}
async updateGoalProgress(id: string, currentValue: number): Promise<import('./types').FitnessGoal | null> {
if (!this.db) throw new Error('Database not connected')
// Get the goal to calculate progress
const goal = await this.getFitnessGoalById(id)
if (!goal) return null
let progress = goal.progress
if (goal.targetValue && goal.targetValue > 0) {
progress = Math.min(100, (currentValue / goal.targetValue) * 100)
}
const stmt = this.db.prepare(
'UPDATE fitness_goals SET current_value = ?, progress = ?, updated_at = ? WHERE id = ?'
)
stmt.run(currentValue, progress, new Date().toISOString(), id)
return this.getFitnessGoalById(id)
}
async completeGoal(id: string): Promise<import('./types').FitnessGoal | null> {
if (!this.db) throw new Error('Database not connected')
const now = new Date()
const stmt = this.db.prepare(
'UPDATE fitness_goals SET status = ?, progress = ?, completed_date = ?, updated_at = ? WHERE id = ?'
)
stmt.run('completed', 100, now.toISOString(), now.toISOString(), id)
return this.getFitnessGoalById(id)
}
private mapRowToFitnessGoal(row: any): import('./types').FitnessGoal {
return {
id: row.id,
userId: row.user_id,
fitnessProfileId: row.fitness_profile_id,
goalType: row.goal_type,
title: row.title,
description: row.description,
targetValue: row.target_value,
currentValue: row.current_value,
unit: row.unit,
startDate: new Date(row.start_date),
targetDate: row.target_date ? new Date(row.target_date) : undefined,
completedDate: row.completed_date ? new Date(row.completed_date) : undefined,
status: row.status,
progress: row.progress,
priority: row.priority,
notes: row.notes,
createdAt: new Date(row.created_at),
updatedAt: new Date(row.updated_at)
}
}
async getDashboardStats(): Promise<{
totalUsers: number;
activeClients: number;
totalRevenue: number;
revenueGrowth: number;
}> {
if (!this.db) throw new Error('Database not connected')
// Total Users
const userCountStmt = this.db.prepare('SELECT COUNT(*) as count FROM users')
const userCount = (userCountStmt.get() as any).count
// Active Clients
const activeClientCountStmt = this.db.prepare("SELECT COUNT(*) as count FROM clients WHERE membershipStatus = 'active'")
const activeClientCount = (activeClientCountStmt.get() as any).count
// Total Revenue (assuming payments table exists, handling if it's empty)
// Note: We need to create the payments table first if it doesn't exist in createTables
// For now, returning 0 if table doesn't exist or is empty
let totalRevenue = 0
let revenueGrowth = 0
try {
const revenueStmt = this.db.prepare('SELECT SUM(amount) as total FROM payments WHERE status = "completed"')
const revenueResult = revenueStmt.get() as any
totalRevenue = revenueResult?.total || 0
} catch (e) {
// Table might not exist yet
console.warn('Payments table query failed, returning 0 revenue')
}
return {
totalUsers: userCount,
activeClients: activeClientCount,
totalRevenue,
revenueGrowth
}
}
}
+78 -4
View File
@@ -1,12 +1,86 @@
import { User as SharedUser, Client, FitnessProfile, Attendance, Recommendation, FitnessGoal } from "@fitai/shared";
// Database Entity Types
export interface User extends SharedUser {
export interface User {
id: string;
email: string;
firstName: string;
lastName: string;
password: string;
phone?: string;
role: "superAdmin" | "admin" | "trainer" | "client";
createdAt: Date;
updatedAt: Date;
}
export type { Client, FitnessProfile, Attendance, Recommendation, FitnessGoal };
export interface Client {
id: string;
userId: string;
membershipType: "basic" | "premium" | "vip";
membershipStatus: "active" | "inactive" | "expired";
joinDate: Date;
lastVisit?: Date;
}
export interface FitnessProfile {
userId: string;
height: string;
weight: string;
age: string;
gender: "male" | "female" | "other";
activityLevel: "sedentary" | "light" | "moderate" | "active" | "very_active";
fitnessGoals: string[];
exerciseHabits: string;
dietHabits: string;
medicalConditions: string;
allergies?: string;
injuries?: string;
createdAt: Date;
updatedAt: Date;
}
export interface Attendance {
id: string;
userId: string;
checkInTime: Date;
checkOutTime?: Date;
type: "gym" | "class" | "personal_training";
notes?: string;
createdAt: Date;
}
export interface Recommendation {
id: string;
userId: string;
fitnessProfileId?: string;
type: "short_term" | "medium_term" | "long_term" | "ai_plan";
content: string;
activityPlan?: string;
dietPlan?: string;
status: "pending" | "approved" | "rejected" | "completed";
createdAt: Date;
approvedAt?: Date;
approvedBy?: string;
}
export interface FitnessGoal {
id: string;
userId: string;
fitnessProfileId?: string;
goalType: "weight_target" | "strength_milestone" | "endurance_target" | "flexibility_goal" | "habit_building" | "custom";
title: string;
description?: string;
targetValue?: number;
currentValue?: number;
unit?: string;
startDate: Date;
targetDate?: Date;
completedDate?: Date;
status: "active" | "completed" | "abandoned" | "paused";
progress: number;
priority: "low" | "medium" | "high";
notes?: string;
createdAt: Date;
updatedAt: Date;
}
// Database Interface - allows us to swap implementations
export interface IDatabase {
+14 -1
View File
@@ -1 +1,14 @@
export { cn, formatDate } from "@fitai/shared"
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function formatDate(date: string | Date) {
return new Date(date).toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: "numeric",
})
}
+137 -256
View File
@@ -8,7 +8,7 @@
"name": "@fitai/mobile",
"version": "1.0.0",
"dependencies": {
"@clerk/clerk-expo": "^2.19.11",
"@clerk/clerk-expo": "^2.18.3",
"@expo/vector-icons": "^15.0.0",
"@hookform/resolvers": "^3.3.0",
"@react-native-async-storage/async-storage": "^2.2.0",
@@ -19,11 +19,11 @@
"ajv-keywords": "^5.1.0",
"axios": "^1.6.0",
"expo": "~54.0.23",
"expo-auth-session": "^7.0.10",
"expo-auth-session": "^7.0.8",
"expo-barcode-scanner": "^13.0.1",
"expo-camera": "~17.0.9",
"expo-constants": "^18.0.10",
"expo-crypto": "^15.0.8",
"expo-crypto": "^15.0.7",
"expo-font": "~14.0.9",
"expo-haptics": "^15.0.7",
"expo-linear-gradient": "~15.0.7",
@@ -32,9 +32,9 @@
"expo-router": "~6.0.14",
"expo-secure-store": "~15.0.7",
"expo-status-bar": "^3.0.8",
"expo-web-browser": "^15.0.10",
"expo-web-browser": "^15.0.9",
"react": "19.1.0",
"react-dom": "^19.1.0",
"react-dom": "19.1.0",
"react-hook-form": "^7.47.0",
"react-native": "0.81.5",
"react-native-safe-area-context": "~5.6.0",
@@ -1563,15 +1563,15 @@
"license": "MIT"
},
"node_modules/@clerk/clerk-expo": {
"version": "2.19.11",
"resolved": "https://registry.npmjs.org/@clerk/clerk-expo/-/clerk-expo-2.19.11.tgz",
"integrity": "sha512-zwCPDJm/WKe91rMGPequVKgGGWmUR/2OIR0IBqXg0hLaUFjEdce0hoB0QSVdc8TweC8djy+5vKVZIfQ2Tg6Vpg==",
"version": "2.18.3",
"resolved": "https://registry.npmjs.org/@clerk/clerk-expo/-/clerk-expo-2.18.3.tgz",
"integrity": "sha512-lxlDiartLPhVjQiz88biCYa8+ZAv3dOOS0KXQnYn6M79UrO6cCcSO+o3QwEKMvwhiCEp0BcH7wnypU/GOXoB1Q==",
"license": "MIT",
"dependencies": {
"@clerk/clerk-js": "^5.114.1",
"@clerk/clerk-react": "^5.58.1",
"@clerk/shared": "^3.39.0",
"@clerk/types": "^4.101.6",
"@clerk/clerk-js": "^5.105.1",
"@clerk/clerk-react": "^5.53.8",
"@clerk/shared": "^3.31.1",
"@clerk/types": "^4.97.2",
"base-64": "^1.0.0",
"react-native-url-polyfill": "2.0.0",
"tslib": "2.8.1"
@@ -1610,14 +1610,14 @@
}
},
"node_modules/@clerk/clerk-js": {
"version": "5.114.1",
"resolved": "https://registry.npmjs.org/@clerk/clerk-js/-/clerk-js-5.114.1.tgz",
"integrity": "sha512-l3A9UZKWOI7rCz/P9o2bS4hSTRLzPqr9eAPxhBFsfUsGgzlxQjVeyRQ2XgOXfvwsgekmulVHzlbvGZidT7JF6A==",
"version": "5.105.1",
"resolved": "https://registry.npmjs.org/@clerk/clerk-js/-/clerk-js-5.105.1.tgz",
"integrity": "sha512-3ObGAARYsDjN4KS/FmCYvHL4FJtWjUdlDinUX1p1tPkFopPypiK9yhU+HSadpFTseLecc5TBnvuotJAAkQ2gGQ==",
"license": "MIT",
"dependencies": {
"@base-org/account": "2.0.1",
"@clerk/localizations": "^3.30.1",
"@clerk/shared": "^3.39.0",
"@clerk/localizations": "^3.27.0",
"@clerk/shared": "^3.31.1",
"@coinbase/wallet-sdk": "4.3.0",
"@emotion/cache": "11.11.0",
"@emotion/react": "11.11.1",
@@ -1637,7 +1637,8 @@
"dequal": "2.0.3",
"input-otp": "1.4.2",
"qrcode.react": "4.2.0",
"regenerator-runtime": "0.14.1"
"regenerator-runtime": "0.14.1",
"swr": "2.3.4"
},
"engines": {
"node": ">=18.17.0"
@@ -1664,12 +1665,12 @@
"license": "MIT"
},
"node_modules/@clerk/clerk-react": {
"version": "5.58.1",
"resolved": "https://registry.npmjs.org/@clerk/clerk-react/-/clerk-react-5.58.1.tgz",
"integrity": "sha512-jN6mfuqwZakm99CKRQlTahMZEa8qLOpr3Z4lG6XtyJfIcyADleOdRxuOXYoN9sV8ZhNfDDAs+eKjUeusIzPpbg==",
"version": "5.53.8",
"resolved": "https://registry.npmjs.org/@clerk/clerk-react/-/clerk-react-5.53.8.tgz",
"integrity": "sha512-TOiYk31rQUL9JOKZr/fhajf+fQCHicy1J4Rxq7vqtjHseJsnIBjzTigjOap/w8PrDAF28O6dbPC5CA0Tp7Md8w==",
"license": "MIT",
"dependencies": {
"@clerk/shared": "^3.39.0",
"@clerk/shared": "^3.31.1",
"tslib": "2.8.1"
},
"engines": {
@@ -1681,21 +1682,21 @@
}
},
"node_modules/@clerk/localizations": {
"version": "3.30.1",
"resolved": "https://registry.npmjs.org/@clerk/localizations/-/localizations-3.30.1.tgz",
"integrity": "sha512-CHatnmQsGggsBMOO3k82oEh9E7j8Xs/vjFtx+hwGwM/qcJ6CEPmP69QKeRBfAXY5PcJinAuyYmFEqMEFvbyjSw==",
"version": "3.27.0",
"resolved": "https://registry.npmjs.org/@clerk/localizations/-/localizations-3.27.0.tgz",
"integrity": "sha512-XQrzOtON32dXlTHEUwL0utTro6MePSM1gljCjRcY6RKh36I2BcuuG2XVu6K2HIvBL+Ef+nWF8+aU4+zc14nVcQ==",
"license": "MIT",
"dependencies": {
"@clerk/types": "^4.101.6"
"@clerk/types": "^4.97.2"
},
"engines": {
"node": ">=18.17.0"
}
},
"node_modules/@clerk/shared": {
"version": "3.39.0",
"resolved": "https://registry.npmjs.org/@clerk/shared/-/shared-3.39.0.tgz",
"integrity": "sha512-9kqqXGMPAdMQ7SXo5ZwUhbzbLLQeLp/1jdb8FQS5qlhmL0S0bAYKcyDjcmMB8xZPXNc7vZJRT72QHsGSlUAJxw==",
"version": "3.31.1",
"resolved": "https://registry.npmjs.org/@clerk/shared/-/shared-3.31.1.tgz",
"integrity": "sha512-mqxZqlzLJYJxA+ryLzhwFR0eO73teAvRd+wvA8bLUZLYvCRFvaiHsB9dEvbo9Z5bMYdq3NPwnx2uljMuu/tiQw==",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
@@ -1723,12 +1724,12 @@
}
},
"node_modules/@clerk/types": {
"version": "4.101.6",
"resolved": "https://registry.npmjs.org/@clerk/types/-/types-4.101.6.tgz",
"integrity": "sha512-Ah6R65loy5Aq1jBpWo3x01IOlYJfgVn4LVAkSXKVQDtehn8w1bT2uJ7BTa2zH72A8F4K2HivkmMQa+v/37tLFA==",
"version": "4.97.2",
"resolved": "https://registry.npmjs.org/@clerk/types/-/types-4.97.2.tgz",
"integrity": "sha512-xnJq3xzpmuuDnNnWuUMKJLPPkaEaLDM0kiv2Hm0gKIcL1+1P3VaGf2vL9roIhmhLswB2PUwtVvZKBmGjT5yOVw==",
"license": "MIT",
"dependencies": {
"@clerk/shared": "^3.39.0"
"@clerk/shared": "^3.31.1"
},
"engines": {
"node": ">=18.17.0"
@@ -2153,40 +2154,40 @@
}
},
"node_modules/@expo/config": {
"version": "12.0.12",
"resolved": "https://registry.npmjs.org/@expo/config/-/config-12.0.12.tgz",
"integrity": "sha512-X2MW86+ulLpMGvdgfvpl2EOBAKUlwvnvoPwdaZeeyWufGopn1nTUeh4C9gMsplDaP1kXv9sLXVhOoUoX6g9PvQ==",
"version": "12.0.10",
"resolved": "https://registry.npmjs.org/@expo/config/-/config-12.0.10.tgz",
"integrity": "sha512-lJMof5Nqakq1DxGYlghYB/ogSBjmv4Fxn1ovyDmcjlRsQdFCXgu06gEUogkhPtc9wBt9WlTTfqENln5HHyLW6w==",
"license": "MIT",
"dependencies": {
"@babel/code-frame": "~7.10.4",
"@expo/config-plugins": "~54.0.3",
"@expo/config-types": "^54.0.10",
"@expo/json-file": "^10.0.8",
"@expo/config-plugins": "~54.0.2",
"@expo/config-types": "^54.0.8",
"@expo/json-file": "^10.0.7",
"deepmerge": "^4.3.1",
"getenv": "^2.0.0",
"glob": "^13.0.0",
"glob": "^10.4.2",
"require-from-string": "^2.0.2",
"resolve-from": "^5.0.0",
"resolve-workspace-root": "^2.0.0",
"semver": "^7.6.0",
"slugify": "^1.3.4",
"sucrase": "~3.35.1"
"sucrase": "3.35.0"
}
},
"node_modules/@expo/config-plugins": {
"version": "54.0.3",
"resolved": "https://registry.npmjs.org/@expo/config-plugins/-/config-plugins-54.0.3.tgz",
"integrity": "sha512-tBIUZIxLQfCu5jmqTO+UOeeDUGIB0BbK6xTMkPRObAXRQeTLPPfokZRCo818d2owd+Bcmq1wBaDz0VY3g+glfw==",
"version": "54.0.2",
"resolved": "https://registry.npmjs.org/@expo/config-plugins/-/config-plugins-54.0.2.tgz",
"integrity": "sha512-jD4qxFcURQUVsUFGMcbo63a/AnviK8WUGard+yrdQE3ZrB/aurn68SlApjirQQLEizhjI5Ar2ufqflOBlNpyPg==",
"license": "MIT",
"dependencies": {
"@expo/config-types": "^54.0.9",
"@expo/config-types": "^54.0.8",
"@expo/json-file": "~10.0.7",
"@expo/plist": "^0.4.7",
"@expo/sdk-runtime-versions": "^1.0.0",
"chalk": "^4.1.2",
"debug": "^4.3.5",
"getenv": "^2.0.0",
"glob": "^13.0.0",
"glob": "^10.4.2",
"resolve-from": "^5.0.0",
"semver": "^7.5.4",
"slash": "^3.0.0",
@@ -2196,57 +2197,20 @@
}
},
"node_modules/@expo/config-plugins/node_modules/glob": {
"version": "13.0.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-13.0.0.tgz",
"integrity": "sha512-tvZgpqk6fz4BaNZ66ZsRaZnbHvP/jG3uKJvAZOwEVUL4RTA5nJeeLYfyN9/VA8NX/V3IBG+hkeuGpKjvELkVhA==",
"license": "BlueOak-1.0.0",
"version": "10.4.5",
"resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz",
"integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==",
"license": "ISC",
"dependencies": {
"minimatch": "^10.1.1",
"foreground-child": "^3.1.0",
"jackspeak": "^3.1.2",
"minimatch": "^9.0.4",
"minipass": "^7.1.2",
"path-scurry": "^2.0.0"
"package-json-from-dist": "^1.0.0",
"path-scurry": "^1.11.1"
},
"engines": {
"node": "20 || >=22"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/@expo/config-plugins/node_modules/lru-cache": {
"version": "11.2.4",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.2.4.tgz",
"integrity": "sha512-B5Y16Jr9LB9dHVkh6ZevG+vAbOsNOYCX+sXvFWFu7B3Iz5mijW3zdbMyhsh8ANd2mSWBYdJgnqi+mL7/LrOPYg==",
"license": "BlueOak-1.0.0",
"engines": {
"node": "20 || >=22"
}
},
"node_modules/@expo/config-plugins/node_modules/minimatch": {
"version": "10.1.1",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.1.1.tgz",
"integrity": "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ==",
"license": "BlueOak-1.0.0",
"dependencies": {
"@isaacs/brace-expansion": "^5.0.0"
},
"engines": {
"node": "20 || >=22"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/@expo/config-plugins/node_modules/path-scurry": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-2.0.1.tgz",
"integrity": "sha512-oWyT4gICAu+kaA7QWk/jvCHWarMKNs6pXOGWKDTr7cw4IGcUbW+PeTfbaQiLGheFRpjo6O9J0PmyMfQPjH71oA==",
"license": "BlueOak-1.0.0",
"dependencies": {
"lru-cache": "^11.0.0",
"minipass": "^7.1.2"
},
"engines": {
"node": "20 || >=22"
"bin": {
"glob": "dist/esm/bin.mjs"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
@@ -2265,9 +2229,9 @@
}
},
"node_modules/@expo/config-types": {
"version": "54.0.10",
"resolved": "https://registry.npmjs.org/@expo/config-types/-/config-types-54.0.10.tgz",
"integrity": "sha512-/J16SC2an1LdtCZ67xhSkGXpALYUVUNyZws7v+PVsFZxClYehDSoKLqyRaGkpHlYrCc08bS0RF5E0JV6g50psA==",
"version": "54.0.8",
"resolved": "https://registry.npmjs.org/@expo/config-types/-/config-types-54.0.8.tgz",
"integrity": "sha512-lyIn/x/Yz0SgHL7IGWtgTLg6TJWC9vL7489++0hzCHZ4iGjVcfZmPTUfiragZ3HycFFj899qN0jlhl49IHa94A==",
"license": "MIT"
},
"node_modules/@expo/config/node_modules/@babel/code-frame": {
@@ -2280,57 +2244,20 @@
}
},
"node_modules/@expo/config/node_modules/glob": {
"version": "13.0.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-13.0.0.tgz",
"integrity": "sha512-tvZgpqk6fz4BaNZ66ZsRaZnbHvP/jG3uKJvAZOwEVUL4RTA5nJeeLYfyN9/VA8NX/V3IBG+hkeuGpKjvELkVhA==",
"license": "BlueOak-1.0.0",
"version": "10.4.5",
"resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz",
"integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==",
"license": "ISC",
"dependencies": {
"minimatch": "^10.1.1",
"foreground-child": "^3.1.0",
"jackspeak": "^3.1.2",
"minimatch": "^9.0.4",
"minipass": "^7.1.2",
"path-scurry": "^2.0.0"
"package-json-from-dist": "^1.0.0",
"path-scurry": "^1.11.1"
},
"engines": {
"node": "20 || >=22"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/@expo/config/node_modules/lru-cache": {
"version": "11.2.4",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.2.4.tgz",
"integrity": "sha512-B5Y16Jr9LB9dHVkh6ZevG+vAbOsNOYCX+sXvFWFu7B3Iz5mijW3zdbMyhsh8ANd2mSWBYdJgnqi+mL7/LrOPYg==",
"license": "BlueOak-1.0.0",
"engines": {
"node": "20 || >=22"
}
},
"node_modules/@expo/config/node_modules/minimatch": {
"version": "10.1.1",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.1.1.tgz",
"integrity": "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ==",
"license": "BlueOak-1.0.0",
"dependencies": {
"@isaacs/brace-expansion": "^5.0.0"
},
"engines": {
"node": "20 || >=22"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/@expo/config/node_modules/path-scurry": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-2.0.1.tgz",
"integrity": "sha512-oWyT4gICAu+kaA7QWk/jvCHWarMKNs6pXOGWKDTr7cw4IGcUbW+PeTfbaQiLGheFRpjo6O9J0PmyMfQPjH71oA==",
"license": "BlueOak-1.0.0",
"dependencies": {
"lru-cache": "^11.0.0",
"minipass": "^7.1.2"
},
"engines": {
"node": "20 || >=22"
"bin": {
"glob": "dist/esm/bin.mjs"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
@@ -2410,9 +2337,9 @@
}
},
"node_modules/@expo/env": {
"version": "2.0.8",
"resolved": "https://registry.npmjs.org/@expo/env/-/env-2.0.8.tgz",
"integrity": "sha512-5VQD6GT8HIMRaSaB5JFtOXuvfDVU80YtZIuUT/GDhUF782usIXY13Tn3IdDz1Tm/lqA9qnRZQ1BF4t7LlvdJPA==",
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/@expo/env/-/env-2.0.7.tgz",
"integrity": "sha512-BNETbLEohk3HQ2LxwwezpG8pq+h7Fs7/vAMP3eAtFT1BCpprLYoBBFZH7gW4aqGfqOcVP4Lc91j014verrYNGg==",
"license": "MIT",
"dependencies": {
"chalk": "^4.0.0",
@@ -2507,9 +2434,9 @@
}
},
"node_modules/@expo/json-file": {
"version": "10.0.8",
"resolved": "https://registry.npmjs.org/@expo/json-file/-/json-file-10.0.8.tgz",
"integrity": "sha512-9LOTh1PgKizD1VXfGQ88LtDH0lRwq9lsTb4aichWTWSWqy3Ugfkhfm3BhzBIkJJfQQ5iJu3m/BoRlEIjoCGcnQ==",
"version": "10.0.7",
"resolved": "https://registry.npmjs.org/@expo/json-file/-/json-file-10.0.7.tgz",
"integrity": "sha512-z2OTC0XNO6riZu98EjdNHC05l51ySeTto6GP7oSQrCvQgG9ARBwD1YvMQaVZ9wU7p/4LzSf1O7tckL3B45fPpw==",
"license": "MIT",
"dependencies": {
"@babel/code-frame": "~7.10.4",
@@ -2952,27 +2879,6 @@
"integrity": "sha512-F0YfUDjvT+Mtt/R4xdl2X0EYCHMMiJqNLdxHD++jDT5ydEFIyqbCHh51Qx2E211dgZprPKhV7sHmnXKpLuvc5g==",
"license": "MIT"
},
"node_modules/@isaacs/balanced-match": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@isaacs/balanced-match/-/balanced-match-4.0.1.tgz",
"integrity": "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ==",
"license": "MIT",
"engines": {
"node": "20 || >=22"
}
},
"node_modules/@isaacs/brace-expansion": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@isaacs/brace-expansion/-/brace-expansion-5.0.0.tgz",
"integrity": "sha512-ZT55BDLV0yv0RBm2czMiZ+SqCGO7AvmOM3G/w2xhVPH+te0aKgFjmBvGlL1dH+ql2tgGO3MVrbb3jCKyvpgnxA==",
"license": "MIT",
"dependencies": {
"@isaacs/balanced-match": "^4.0.1"
},
"engines": {
"node": "20 || >=22"
}
},
"node_modules/@isaacs/cliui": {
"version": "8.0.2",
"resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz",
@@ -5116,9 +5022,9 @@
"license": "MIT"
},
"node_modules/abitype": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/abitype/-/abitype-1.2.2.tgz",
"integrity": "sha512-4DOIMWscIB3j8hboLAUjLZCE8TMLdgecBpHFumfU4PdO/C1SBCVx4Nu1wPYXaL2iK8B0Jk3tiwnDLCpUtm3fZg==",
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abitype/-/abitype-1.1.1.tgz",
"integrity": "sha512-Loe5/6tAgsBukY95eGaPSDmQHIjRZYQq8PB1MpsNccDIK8WiV+Uw6WzaIXipvaxTEL2yEB0OpEaQv3gs8pkS9Q==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/wevm"
@@ -7257,9 +7163,9 @@
}
},
"node_modules/expo-application": {
"version": "7.0.8",
"resolved": "https://registry.npmjs.org/expo-application/-/expo-application-7.0.8.tgz",
"integrity": "sha512-qFGyxk7VJbrNOQWBbE09XUuGuvkOgFS9QfToaK2FdagM2aQ+x3CvGV2DuVgl/l4ZxPgIf3b/MNh9xHpwSwn74Q==",
"version": "7.0.7",
"resolved": "https://registry.npmjs.org/expo-application/-/expo-application-7.0.7.tgz",
"integrity": "sha512-Jt1/qqnoDUbZ+bK91+dHaZ1vrPDtRBOltRa681EeedkisqguuEeUx4UHqwVyDK2oHWsK6lO3ojetoA4h8OmNcg==",
"license": "MIT",
"peerDependencies": {
"expo": "*"
@@ -7281,16 +7187,16 @@
}
},
"node_modules/expo-auth-session": {
"version": "7.0.10",
"resolved": "https://registry.npmjs.org/expo-auth-session/-/expo-auth-session-7.0.10.tgz",
"integrity": "sha512-XDnKkudvhHSKkZfJ+KkodM+anQcrxB71i+h0kKabdLa5YDXTQ81aC38KRc3TMqmnBDHAu0NpfbzEVd9WDFY3Qg==",
"version": "7.0.9",
"resolved": "https://registry.npmjs.org/expo-auth-session/-/expo-auth-session-7.0.9.tgz",
"integrity": "sha512-mPSwaRWOJYas160lXi5P/7BkLy0xbh+er+IMmAYHqf2+iz2WWs9W/4lMAklQVJG2mCyOZi24XrkffvB2izCa1g==",
"license": "MIT",
"dependencies": {
"expo-application": "~7.0.8",
"expo-constants": "~18.0.11",
"expo-crypto": "~15.0.8",
"expo-linking": "~8.0.10",
"expo-web-browser": "~15.0.10",
"expo-application": "~7.0.7",
"expo-constants": "~18.0.10",
"expo-crypto": "~15.0.7",
"expo-linking": "~8.0.9",
"expo-web-browser": "~15.0.9",
"invariant": "^2.2.4"
},
"peerDependencies": {
@@ -7331,13 +7237,13 @@
}
},
"node_modules/expo-constants": {
"version": "18.0.11",
"resolved": "https://registry.npmjs.org/expo-constants/-/expo-constants-18.0.11.tgz",
"integrity": "sha512-xnfrfZ7lHjb+03skhmDSYeFF7OU2K3Xn/lAeP+7RhkV2xp2f5RCKtOUYajCnYeZesvMrsUxOsbGOP2JXSOH3NA==",
"version": "18.0.10",
"resolved": "https://registry.npmjs.org/expo-constants/-/expo-constants-18.0.10.tgz",
"integrity": "sha512-Rhtv+X974k0Cahmvx6p7ER5+pNhBC0XbP1lRviL2J1Xl4sT2FBaIuIxF/0I0CbhOsySf0ksqc5caFweAy9Ewiw==",
"license": "MIT",
"dependencies": {
"@expo/config": "~12.0.11",
"@expo/env": "~2.0.8"
"@expo/config": "~12.0.10",
"@expo/env": "~2.0.7"
},
"peerDependencies": {
"expo": "*",
@@ -7345,9 +7251,9 @@
}
},
"node_modules/expo-crypto": {
"version": "15.0.8",
"resolved": "https://registry.npmjs.org/expo-crypto/-/expo-crypto-15.0.8.tgz",
"integrity": "sha512-aF7A914TB66WIlTJvl5J6/itejfY78O7dq3ibvFltL9vnTALJ/7LYHvLT4fwmx9yUNS6ekLBtDGWivFWnj2Fcw==",
"version": "15.0.7",
"resolved": "https://registry.npmjs.org/expo-crypto/-/expo-crypto-15.0.7.tgz",
"integrity": "sha512-FUo41TwwGT2e5rA45PsjezI868Ch3M6wbCZsmqTWdF/hr+HyPcrp1L//dsh/hsrsyrQdpY/U96Lu71/wXePJeg==",
"license": "MIT",
"dependencies": {
"base64-js": "^1.3.0"
@@ -7410,12 +7316,12 @@
}
},
"node_modules/expo-linking": {
"version": "8.0.10",
"resolved": "https://registry.npmjs.org/expo-linking/-/expo-linking-8.0.10.tgz",
"integrity": "sha512-0EKtn4Sk6OYmb/5ZqK8riO0k1Ic+wyT3xExbmDvUYhT7p/cKqlVUExMuOIAt3Cx3KUUU1WCgGmdd493W/D5XjA==",
"version": "8.0.9",
"resolved": "https://registry.npmjs.org/expo-linking/-/expo-linking-8.0.9.tgz",
"integrity": "sha512-a0UHhlVyfwIbn8b1PSFPoFiIDJeps2iEq109hVH3CHd0CMKuRxFfNio9Axe2BjXhiJCYWR4OV1iIyzY/GjiVkQ==",
"license": "MIT",
"dependencies": {
"expo-constants": "~18.0.11",
"expo-constants": "~18.0.10",
"invariant": "^2.2.4"
},
"peerDependencies": {
@@ -7556,9 +7462,9 @@
}
},
"node_modules/expo-secure-store": {
"version": "15.0.8",
"resolved": "https://registry.npmjs.org/expo-secure-store/-/expo-secure-store-15.0.8.tgz",
"integrity": "sha512-lHnzvRajBu4u+P99+0GEMijQMFCOYpWRO4dWsXSuMt77+THPIGjzNvVKrGSl6mMrLsfVaKL8BpwYZLGlgA+zAw==",
"version": "15.0.7",
"resolved": "https://registry.npmjs.org/expo-secure-store/-/expo-secure-store-15.0.7.tgz",
"integrity": "sha512-9q7+G1Zxr5P6J5NRIlm86KulvmYwc6UnQlYPjQLDu1drDnerz6AT6l884dPu29HgtDTn4rR0heYeeGFhMKM7/Q==",
"license": "MIT",
"peerDependencies": {
"expo": "*"
@@ -7587,9 +7493,9 @@
}
},
"node_modules/expo-web-browser": {
"version": "15.0.10",
"resolved": "https://registry.npmjs.org/expo-web-browser/-/expo-web-browser-15.0.10.tgz",
"integrity": "sha512-fvDhW4bhmXAeWFNFiInmsGCK83PAqAcQaFyp/3pE/jbdKmFKoRCWr46uZGIfN4msLK/OODhaQ/+US7GSJNDHJg==",
"version": "15.0.9",
"resolved": "https://registry.npmjs.org/expo-web-browser/-/expo-web-browser-15.0.9.tgz",
"integrity": "sha512-Dj8kNFO+oXsxqCDNlUT/GhOrJnm10kAElH++3RplLydogFm5jTzXYWDEeNIDmV+F+BzGYs+sIhxiBf7RyaxXZg==",
"license": "MIT",
"peerDependencies": {
"expo": "*",
@@ -7735,23 +7641,6 @@
"asap": "~2.0.3"
}
},
"node_modules/fdir": {
"version": "6.5.0",
"resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz",
"integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==",
"license": "MIT",
"engines": {
"node": ">=12.0.0"
},
"peerDependencies": {
"picomatch": "^3 || ^4"
},
"peerDependenciesMeta": {
"picomatch": {
"optional": true
}
}
},
"node_modules/file-entry-cache": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
@@ -12897,17 +12786,17 @@
"license": "MIT"
},
"node_modules/sucrase": {
"version": "3.35.1",
"resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.35.1.tgz",
"integrity": "sha512-DhuTmvZWux4H1UOnWMB3sk0sbaCVOoQZjv8u1rDoTV0HTdGem9hkAZtl4JZy8P2z4Bg0nT+YMeOFyVr4zcG5Tw==",
"version": "3.35.0",
"resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.35.0.tgz",
"integrity": "sha512-8EbVDiu9iN/nESwxeSxDKe0dunta1GOlHufmSSXxMD2z2/tMZpDMpvXQGsc+ajGo8y2uYUmixaSRUc/QPoQ0GA==",
"license": "MIT",
"dependencies": {
"@jridgewell/gen-mapping": "^0.3.2",
"commander": "^4.0.0",
"glob": "^10.3.10",
"lines-and-columns": "^1.1.6",
"mz": "^2.7.0",
"pirates": "^4.0.1",
"tinyglobby": "^0.2.11",
"ts-interface-checker": "^0.1.9"
},
"bin": {
@@ -12927,6 +12816,26 @@
"node": ">= 6"
}
},
"node_modules/sucrase/node_modules/glob": {
"version": "10.4.5",
"resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz",
"integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==",
"license": "ISC",
"dependencies": {
"foreground-child": "^3.1.0",
"jackspeak": "^3.1.2",
"minimatch": "^9.0.4",
"minipass": "^7.1.2",
"package-json-from-dist": "^1.0.0",
"path-scurry": "^1.11.1"
},
"bin": {
"glob": "dist/esm/bin.mjs"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@@ -13127,34 +13036,6 @@
"integrity": "sha512-fcwX4mndzpLQKBS1DVYhGAcYaYt7vsHNIvQV+WXMvnow5cgjPphq5CaayLaGsjRdSCKZFNGt7/GYAuXaNOiYCA==",
"license": "MIT"
},
"node_modules/tinyglobby": {
"version": "0.2.15",
"resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.15.tgz",
"integrity": "sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ==",
"license": "MIT",
"dependencies": {
"fdir": "^6.5.0",
"picomatch": "^4.0.3"
},
"engines": {
"node": ">=12.0.0"
},
"funding": {
"url": "https://github.com/sponsors/SuperchupuDev"
}
},
"node_modules/tinyglobby/node_modules/picomatch": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.3.tgz",
"integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==",
"license": "MIT",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/tmpl": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz",
@@ -13548,9 +13429,9 @@
}
},
"node_modules/viem": {
"version": "2.41.2",
"resolved": "https://registry.npmjs.org/viem/-/viem-2.41.2.tgz",
"integrity": "sha512-LYliajglBe1FU6+EH9mSWozp+gRA/QcHfxeD9Odf83AdH5fwUS7DroH4gHvlv6Sshqi1uXrYFA2B/EOczxd15g==",
"version": "2.38.6",
"resolved": "https://registry.npmjs.org/viem/-/viem-2.38.6.tgz",
"integrity": "sha512-aqO6P52LPXRjdnP6rl5Buab65sYa4cZ6Cpn+k4OLOzVJhGIK8onTVoKMFMT04YjDfyDICa/DZyV9HmvLDgcjkw==",
"funding": [
{
"type": "github",
+5 -5
View File
@@ -14,7 +14,7 @@
"test": "jest"
},
"dependencies": {
"@clerk/clerk-expo": "^2.19.11",
"@clerk/clerk-expo": "^2.18.3",
"@expo/vector-icons": "^15.0.0",
"@hookform/resolvers": "^3.3.0",
"@react-native-async-storage/async-storage": "^2.2.0",
@@ -25,11 +25,11 @@
"ajv-keywords": "^5.1.0",
"axios": "^1.6.0",
"expo": "~54.0.23",
"expo-auth-session": "^7.0.10",
"expo-auth-session": "^7.0.8",
"expo-barcode-scanner": "^13.0.1",
"expo-camera": "~17.0.9",
"expo-constants": "^18.0.10",
"expo-crypto": "^15.0.8",
"expo-crypto": "^15.0.7",
"expo-font": "~14.0.9",
"expo-haptics": "^15.0.7",
"expo-linear-gradient": "~15.0.7",
@@ -38,9 +38,9 @@
"expo-router": "~6.0.14",
"expo-secure-store": "~15.0.7",
"expo-status-bar": "^3.0.8",
"expo-web-browser": "^15.0.10",
"expo-web-browser": "^15.0.9",
"react": "19.1.0",
"react-dom": "^19.1.0",
"react-dom": "19.1.0",
"react-hook-form": "^7.47.0",
"react-native": "0.81.5",
"react-native-safe-area-context": "~5.6.0",
@@ -9,7 +9,7 @@ import { theme } from "../../styles/theme";
interface Recommendation {
id: string;
userId: string;
recommendationText: string;
content: string;
activityPlan?: string;
dietPlan?: string;
status: string;
@@ -141,7 +141,7 @@ export default function RecommendationsScreen() {
</View>
<Text style={styles.sectionTitle}>Daily Advice</Text>
<Text style={styles.content}>{item.recommendationText}</Text>
<Text style={styles.content}>{item.content}</Text>
{item.activityPlan && (
<>
+1 -1
View File
@@ -1,5 +1,5 @@
export const API_BASE_URL = __DEV__
? 'https://5e424f097c8b.ngrok-free.app'
? 'https://8679109544e4.ngrok-free.app'
: 'https://your-production-url.com'
export const API_ENDPOINTS = {
+171 -12
View File
@@ -1,15 +1,174 @@
## fitai
# FitAI
# description
Integrated AI solution for fitness houses and their clients with Clerk authentication.
- fitai is integrated ai solution for fitness houses and their clients,
its allow to easy menagment of clients, tracking of payments, usage of resourcess,
attendance, habits etc.
these will be phase one:
solution is composed of a admin app, where we are doing managment tasks, we visualize and
expose importatnt data to menagment and trainers, and a expo/reactnative mobile app for users.
via app we will be tracking attendance and payments, we will be sending notification etc.
## Project Structure
# phase 2
we will be tracking user inputs via manual input and devices, backend will analyze data and propose
excercises etc.
```
fitai/
├── apps/
│ ├── admin/ # Next.js admin dashboard
│ └── mobile/ # React Native mobile app (Expo)
├── packages/
│ └── shared/ # Shared types and utilities
└── AGENTS.md # Development guidelines
```
## Getting Started
### Prerequisites
- Node.js >= 18.0.0
- npm >= 9.0.0
- Clerk account (sign up at https://clerk.com)
### Installation
```bash
# Install root dependencies
npm install
# Install admin dependencies
cd apps/admin && npm install
# Install mobile dependencies
cd apps/mobile && npm install --legacy-peer-deps
```
### Authentication Setup
FitAI uses Clerk for authentication. Follow these steps:
1. **Create a Clerk account** at https://dashboard.clerk.com
2. **Create a new application** in the Clerk dashboard
3. **Copy your API keys** (Publishable Key and Secret Key)
4. **Configure environment variables**:
**Admin App** (`apps/admin/.env.local`):
```env
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_your_key_here
CLERK_SECRET_KEY=sk_test_your_key_here
```
**Mobile App** (`apps/mobile/.env`):
```env
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_your_key_here
```
📖 **See [CLERK_SETUP.md](./CLERK_SETUP.md) for detailed setup instructions**
### Development
**Important**: Set up environment variables before running the apps!
```bash
# Admin dashboard (http://localhost:3000)
cd apps/admin && npm run dev
# Mobile app (http://localhost:8081) - Requires Expo SDK 54
cd apps/mobile && npm start
```
**First-time setup checklist**:
- [ ] Create Clerk account and application
- [ ] Add API keys to `.env.local` (admin) and `.env` (mobile)
- [ ] Verify both apps start without errors
- [ ] Test sign-up and sign-in flows
### Mobile App Setup
- **Expo SDK**: 50 (stable, compatible with Expo Go)
- **Assets**: Placeholder icons and splash screen included
- **Navigation**: Expo Router with tab-based layout
- **Authentication**: Secure storage with expo-secure-store
- **Babel**: babel-preset-expo for proper transpilation
### Known Compatibility Notes
- Use Expo Go with SDK 50 for mobile testing
- For SDK 54, upgrade all dependencies to latest versions
- Current setup prioritizes stability over latest features
### Build & Test
```bash
# Build all apps
npm run build
# Run tests
npm test
# Lint code
npm run lint
# Type checking
npm run typecheck
```
## Features
### Authentication (Clerk)
- 🔐 Secure email/password authentication
- ✉️ Email verification
- 🔄 Session management
- 🎨 Customizable UI components
- 📱 Multi-platform support (Web + Mobile)
- 🛡️ Built-in security features
### Admin Dashboard
- 👥 User management (CRUD operations)
- 📊 Analytics dashboard with charts
- 🎯 Role-based access control
- 📈 Data visualization with AG Grid
- 💳 Payment tracking (coming soon)
- 📅 Attendance monitoring (coming soon)
### Mobile App
- 🔐 Secure sign-in/sign-up
- 👤 User profile management
- 📱 Native mobile experience
- 🔔 Push notifications ready
- ✅ Attendance check-in (coming soon)
- 💰 Payment history (coming soon)
## Tech Stack
### Authentication
- **Clerk**: Complete authentication and user management platform
### Frontend
- **Admin**: Next.js 14 (App Router), React 19, TypeScript, Tailwind CSS
- **Mobile**: React Native, Expo SDK 54, Expo Router, TypeScript
### Backend & Database
- **Database**: SQLite with Drizzle ORM
- **API**: Next.js API Routes (REST)
### Development Tools
- **State Management**: React Query, React Hook Form
- **Validation**: Zod schemas
- **Data Grid**: AG Grid for advanced user management
- **Charts**: AG Charts for analytics and visualization
- **Testing**: Jest, Testing Library (configured)
## Project Structure
```
fitai/
├── apps/
│ ├── admin/ # Next.js admin dashboard
│ │ ├── src/
│ │ │ ├── app/ # App Router pages & API routes
│ │ │ ├── components/
│ │ │ └── lib/ # Database & utilities
│ │ └── .env.local # Admin environment variables
│ │
│ └── mobile/ # Expo React Native app
│ ├── src/
│ │ ├── app/ # Expo Router screens
│ │ │ ├── (auth)/ # Authentication screens
│ │ │ └── (tabs)/ # Main app tabs
│ │ └── components/
│ └── .env # Mobile environment variables
│
├── packages/
│ ├── database/ # Drizzle ORM schemas & DB client
│ └── shared/ # Shared types & utilities
│
└── CLERK_SETUP.md # Detailed authentication setup guide
```
-53
View File
@@ -1,53 +0,0 @@
# Analysis and Next Steps
## Current State Analysis
### Architecture
- **Monorepo Structure**: The project is well-structured as a monorepo with `apps/admin` (Next.js), `apps/mobile` (Expo), and shared packages (`database`, `shared`).
- **Database Inconsistency**: There is a significant architectural inconsistency in database access.
- `packages/database` defines a Drizzle ORM schema and exports a Drizzle instance.
- `apps/admin` implements a custom `SQLiteDatabase` class (`src/lib/database/sqlite.ts`) that uses `better-sqlite3` directly with raw SQL queries and manual object mapping.
- This leads to code duplication, potential schema drift (if Drizzle schema changes but raw SQL doesn't), and loss of Drizzle's type safety and convenience features.
### Code Quality
- **Type Safety**:
- `UserManagement.tsx` defines local interfaces (e.g., `User`) that duplicate the schema definitions.
- Shared types should be utilized from `@fitai/shared` or `@fitai/database`.
- **Configuration**:
- `apps/mobile/src/config/api.ts` contains hardcoded URLs (e.g., ngrok) which is brittle for development and production.
- API routes in `apps/admin` contain `console.log` statements that should be removed or replaced with a proper logging solution.https://5e424f097c8b.ngrok-free.app
- **Error Handling**:
- Basic error handling exists in API routes, but could be standardized (e.g., custom error classes, consistent error response format).
### Testing
- **Missing Tests**: While `jest` is configured in `apps/admin`, there are no actual test files in `src`. The critical business logic in `src/lib/database` and API routes is currently untested.
## Suggested Next Steps
### 1. Unify Database Access (High Priority)
Refactor `apps/admin` to use the Drizzle ORM instance from `@fitai/database` instead of the custom `SQLiteDatabase` class.
- **Benefits**: Removes ~800 lines of raw SQL code, ensures type safety, reduces maintenance burden, and leverages Drizzle's features.
- **Action**: Replace `src/lib/database/sqlite.ts` methods with Drizzle queries using the schema from `@fitai/database`.
### 2. Implement Testing Strategy (High Priority)
Start adding tests for critical paths.
- **Unit Tests**: Add Jest tests for the database logic (if keeping the custom class temporarily) or the service layer.
- **Integration Tests**: Test API routes (`/api/attendance/*`, `/api/users`) to ensure they handle requests correctly.
- **Action**: Create `__tests__` directories in `apps/admin/src` and add initial tests for attendance flows.
### 3. Improve Configuration Management (Medium Priority)
- **Mobile**: Use environment variables (e.g., `EXPO_PUBLIC_API_URL`) for the API base URL in the mobile app instead of hardcoding.
- **Admin**: Ensure all secrets and config values are strictly in `.env` files.
### 4. Enhance Type Safety (Medium Priority)
- **Shared Types**: Move the `User` interface and other shared entities from `UserManagement.tsx` to `@fitai/shared` or use the inferred types from Drizzle schema in `@fitai/database`.
- **Action**: Refactor components to import types from the shared package.
### 5. Cleanup and Standardization (Low Priority)
- Remove `console.log` from production code.
- Standardize API error responses.
## Immediate Action Items
1. **Create a reproduction test case** for the current attendance flow to ensure no regressions during refactoring.
2. **Refactor one module** (e.g., Attendance) to use Drizzle ORM as a proof of concept for the migration.
3. **Extract types** from `UserManagement.tsx` to `@fitai/shared`.
+2 -5
View File
@@ -991,7 +991,6 @@
"integrity": "sha512-6m1I5RmHBGTnUGS113G04DMu3CpSdxCAU/UvtjNWL4Nuf3MW9tQhiJqRlHzChIkhy6kZSAQmc+I1bcGjE3yNKg==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@typescript-eslint/scope-manager": "8.46.3",
"@typescript-eslint/types": "8.46.3",
@@ -1197,7 +1196,6 @@
"integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
"dev": true,
"license": "MIT",
"peer": true,
"bin": {
"acorn": "bin/acorn"
},
@@ -1630,7 +1628,6 @@
"integrity": "sha512-BhHmn2yNOFA9H9JmmIVKJmd288g9hrVRDkdoIgRCRuSySRUHH7r/DI6aAXW9T1WwUuY3DFgrcaqB+deURBLR5g==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@eslint-community/eslint-utils": "^4.8.0",
"@eslint-community/regexpp": "^4.12.1",
@@ -2795,7 +2792,8 @@
"version": "0.27.0",
"resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.27.0.tgz",
"integrity": "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q==",
"license": "MIT"
"license": "MIT",
"peer": true
},
"node_modules/semver": {
"version": "7.7.3",
@@ -3134,7 +3132,6 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
"peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
+1 -4
View File
@@ -9,7 +9,7 @@
"version": "1.0.0",
"dependencies": {
"@types/better-sqlite3": "^7.6.13",
"better-sqlite3": "12.4.1",
"better-sqlite3": "^12.4.1",
"drizzle-orm": "^0.44.7"
},
"devDependencies": {
@@ -907,7 +907,6 @@
"resolved": "https://registry.npmjs.org/@types/better-sqlite3/-/better-sqlite3-7.6.13.tgz",
"integrity": "sha512-NMv9ASNARoKksWtsq/SHakpYAYnhBrQgGD8zkLYk/jaK8jUGn08CfEdTRgYhMypUQAfzSP8W6gNLe0q19/t4VA==",
"license": "MIT",
"peer": true,
"dependencies": {
"@types/node": "*"
}
@@ -947,7 +946,6 @@
"integrity": "sha512-3yVdyZhklTiNrtg+4WqHpJpFDd+WHTg2oM7UcR80GqL05AOV0xEJzc6qNvFYoEtE+hRp1n9MpN6/+4yhlGkDXQ==",
"hasInstallScript": true,
"license": "MIT",
"peer": true,
"dependencies": {
"bindings": "^1.5.0",
"prebuild-install": "^7.1.1"
@@ -1221,7 +1219,6 @@
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"peer": true,
"bin": {
"esbuild": "bin/esbuild"
},
+1 -1
View File
@@ -12,7 +12,7 @@
},
"dependencies": {
"drizzle-orm": "^0.44.7",
"better-sqlite3": "12.4.1",
"better-sqlite3": "^12.4.1",
"@types/better-sqlite3": "^7.6.13"
},
"devDependencies": {
+1 -1
View File
@@ -12,4 +12,4 @@ const sqlite = new Database(dbPath)
export const db = drizzle(sqlite, { schema })
export * from './schema'
export { eq, and, or, desc, asc, sql } from 'drizzle-orm'
export { eq, and, or, desc, asc } from 'drizzle-orm'
+11 -3
View File
@@ -74,9 +74,9 @@ export const payments = sqliteTable("payments", {
export const attendance = sqliteTable("attendance", {
id: text("id").primaryKey(),
userId: text("user_id")
clientId: text("client_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
.references(() => clients.id, { onDelete: "cascade" }),
checkInTime: integer("check_in_time", { mode: "timestamp" }).notNull(),
checkOutTime: integer("check_out_time", { mode: "timestamp" }),
type: text("type", { enum: ["gym", "class", "personal_training"] })
@@ -116,7 +116,15 @@ export const fitnessProfiles = sqliteTable("fitness_profiles", {
gender: text("gender", {
enum: ["male", "female", "other", "prefer_not_to_say"],
}),
fitnessGoals: text("fitness_goals", { mode: "json" }).$type<string[]>(),
fitnessGoal: text("fitness_goal", {
enum: [
"weight_loss",
"muscle_gain",
"endurance",
"flexibility",
"general_fitness",
],
}),
activityLevel: text("activity_level", {
enum: [
"sedentary",
-21
View File
@@ -8,33 +8,12 @@
"name": "@fitai/shared",
"version": "1.0.0",
"dependencies": {
"clsx": "^2.1.1",
"tailwind-merge": "^3.4.0",
"zod": "^4.1.12"
},
"devDependencies": {
"typescript": "^5.9.3"
}
},
"node_modules/clsx": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz",
"integrity": "sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==",
"license": "MIT",
"engines": {
"node": ">=6"
}
},
"node_modules/tailwind-merge": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-3.4.0.tgz",
"integrity": "sha512-uSaO4gnW+b3Y2aWoWfFpX62vn2sR3skfhbjsEnaBI81WD1wBLlHZe5sWf0AqjksNdYTbGBEd0UasQMT3SNV15g==",
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/dcastil"
}
},
"node_modules/typescript": {
"version": "5.9.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
+1 -3
View File
@@ -9,11 +9,9 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"clsx": "^2.1.1",
"tailwind-merge": "^3.4.0",
"zod": "^4.1.12"
},
"devDependencies": {
"typescript": "^5.9.3"
}
}
}
+14 -72
View File
@@ -3,10 +3,7 @@ export interface User {
email: string
firstName: string
lastName: string
password?: string
phone?: string
role: 'superAdmin' | 'admin' | 'trainer' | 'client'
imageUrl?: string
role: 'admin' | 'trainer' | 'client'
createdAt: Date
updatedAt: Date
}
@@ -14,83 +11,18 @@ export interface User {
export interface Client {
id: string
userId: string
user?: User
user: User
membershipType: 'basic' | 'premium' | 'vip'
membershipStatus: 'active' | 'inactive' | 'suspended' | 'expired'
membershipStatus: 'active' | 'inactive' | 'suspended'
joinDate: Date
lastVisit?: Date
emergencyContact?: {
emergencyContact: {
name: string
phone: string
relationship: string
}
}
export interface FitnessProfile {
id: string
userId: string
height: string
weight: string
age: string
gender: "male" | "female" | "other"
activityLevel: "sedentary" | "lightly_active" | "moderately_active" | "very_active" | "extremely_active"
fitnessGoals: string[]
exerciseHabits: string
dietHabits: string
medicalConditions: string
allergies?: string
injuries?: string
createdAt: Date
updatedAt: Date
}
export interface Attendance {
id: string
userId: string
clientId?: string
client?: Client
checkInTime: Date
checkOutTime?: Date
type: 'gym' | 'class' | 'personal_training'
notes?: string
createdAt?: Date
}
export interface Recommendation {
id: string
userId: string
fitnessProfileId?: string
type: "short_term" | "medium_term" | "long_term" | "ai_plan"
recommendationText: string
activityPlan?: string
dietPlan?: string
status: "pending" | "approved" | "rejected" | "completed"
createdAt: Date
approvedAt?: Date
approvedBy?: string
}
export interface FitnessGoal {
id: string
userId: string
fitnessProfileId?: string
goalType: "weight_target" | "strength_milestone" | "endurance_target" | "flexibility_goal" | "habit_building" | "custom"
title: string
description?: string
targetValue?: number
currentValue?: number
unit?: string
startDate: Date
targetDate?: Date
completedDate?: Date
status: "active" | "completed" | "abandoned" | "paused"
progress: number
priority: "low" | "medium" | "high"
notes?: string
createdAt: Date
updatedAt: Date
}
export interface Payment {
id: string
clientId: string
@@ -104,6 +36,16 @@ export interface Payment {
description: string
}
export interface Attendance {
id: string
clientId: string
client: Client
checkInTime: Date
checkOutTime?: Date
type: 'gym' | 'class' | 'personal_training'
notes?: string
}
export interface Notification {
id: string
userId: string
+3 -11
View File
@@ -1,17 +1,9 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export const formatDate = (date: Date | string): string => {
const d = new Date(date)
export const formatDate = (date: Date): string => {
return new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
month: 'short',
day: 'numeric',
}).format(d)
}).format(date)
}
export const formatCurrency = (
View File
Whitespace-only changes.
+16
View File
@@ -0,0 +1,16 @@
SSUUMMMMAARRYY OOFF LLEESSSS CCOOMMMMAANNDDSS
Commands marked with * may be preceded by a number, _N.
Notes in parentheses indicate the behavior if _N is given.
A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.
h H Display this help.
q :q Q :Q ZZ Exit.
---------------------------------------------------------------------------
MMOOVVIINNGG
e ^E j ^N CR * Forward one line (or _N lines).
y ^Y k ^K ^P * Backward one line (or _N lines).
f ^F ^V SPACE * Forward one window (or _N lines).