Initial commit for Start9 packaging
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { cookies } from 'next/headers';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function POST(_request: NextRequest) {
|
||||
try {
|
||||
const cookieStore = await cookies();
|
||||
const sessionCookie = cookieStore.get('sessionToken');
|
||||
|
||||
if (sessionCookie) {
|
||||
// Delete the session from the database
|
||||
await prisma.session.delete({
|
||||
where: {
|
||||
token: sessionCookie.value,
|
||||
},
|
||||
}).catch(() => {
|
||||
// Session might not exist, that's ok
|
||||
});
|
||||
|
||||
// Clear the cookie
|
||||
cookieStore.delete('sessionToken');
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'An error occurred during logout' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { verifyPassword, createSession } from '@/lib/auth';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string().email(),
|
||||
password: z.string().min(1),
|
||||
});
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { email, password } = loginSchema.parse(body);
|
||||
|
||||
// Look up user by email
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid email or password' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify the password
|
||||
const isValid = await verifyPassword(password, user.passwordHash);
|
||||
|
||||
if (!isValid) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid email or password' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create a session
|
||||
const session = await createSession(user.id);
|
||||
|
||||
// Set the session cookie
|
||||
const response = NextResponse.json({
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
},
|
||||
});
|
||||
|
||||
response.cookies.set('sessionToken', session.token, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
maxAge: 60 * 60 * 24 * 30, // 30 days
|
||||
path: '/',
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid request data', details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.error('Login error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'An error occurred during login' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
/**
|
||||
* GET /api/exercises/[id]
|
||||
* Get exercise with history
|
||||
*/
|
||||
export async function GET(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const exercise = await prisma.exercise.findFirst({
|
||||
where: {
|
||||
id: params.id,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!exercise) {
|
||||
return NextResponse.json({ error: "Exercise not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Get exercise history grouped by workout
|
||||
const setLogs = await prisma.setLog.findMany({
|
||||
where: {
|
||||
exerciseId: params.id,
|
||||
workout: {
|
||||
userId: user.id,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
workout: {
|
||||
select: {
|
||||
id: true,
|
||||
date: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [
|
||||
{ workout: { date: "desc" } },
|
||||
{ setNumber: "asc" },
|
||||
],
|
||||
take: 100,
|
||||
});
|
||||
|
||||
// Group by workout
|
||||
const workoutMap = new Map<string, { workout: any; sets: any[] }>();
|
||||
for (const log of setLogs) {
|
||||
const key = log.workoutId;
|
||||
if (!workoutMap.has(key)) {
|
||||
workoutMap.set(key, { workout: log.workout, sets: [] });
|
||||
}
|
||||
workoutMap.get(key)!.sets.push(log);
|
||||
}
|
||||
|
||||
const history = Array.from(workoutMap.values()).slice(0, 20);
|
||||
|
||||
return NextResponse.json({
|
||||
exercise,
|
||||
history,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("GET /api/exercises/[id] error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/exercises/[id]
|
||||
* Edit exercise details
|
||||
*/
|
||||
const updateExerciseSchema = z.object({
|
||||
name: z.string().min(1).optional(),
|
||||
type: z.string().min(1).optional(),
|
||||
muscleGroups: z.array(z.string()).optional(),
|
||||
description: z.string().optional(),
|
||||
inputFields: z.array(z.string().min(1)).optional(),
|
||||
defaultWeightUnit: z.string().nullable().optional(),
|
||||
});
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const exercise = await prisma.exercise.findFirst({
|
||||
where: {
|
||||
id: params.id,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!exercise) {
|
||||
return NextResponse.json({ error: "Exercise not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = updateExerciseSchema.parse(body);
|
||||
|
||||
const data: any = {};
|
||||
if (validated.name !== undefined) data.name = validated.name;
|
||||
if (validated.type !== undefined) data.type = validated.type;
|
||||
if (validated.description !== undefined) data.description = validated.description;
|
||||
if (validated.muscleGroups !== undefined)
|
||||
data.muscleGroups = JSON.stringify(validated.muscleGroups);
|
||||
if (validated.inputFields !== undefined)
|
||||
data.inputFields = JSON.stringify(validated.inputFields);
|
||||
if (validated.defaultWeightUnit !== undefined)
|
||||
data.defaultWeightUnit = validated.defaultWeightUnit;
|
||||
|
||||
const updated = await prisma.exercise.update({
|
||||
where: { id: params.id },
|
||||
data,
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid data", details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
console.error("PATCH /api/exercises/[id] error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/exercises/[id]
|
||||
*/
|
||||
export async function DELETE(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const exercise = await prisma.exercise.findFirst({
|
||||
where: {
|
||||
id: params.id,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!exercise) {
|
||||
return NextResponse.json({ error: "Exercise not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.setLog.deleteMany({
|
||||
where: { exerciseId: params.id },
|
||||
});
|
||||
|
||||
await prisma.exercise.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: "Exercise deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error("DELETE /api/exercises/[id] error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { getExercises, createExercise } from "@/lib/db/exercises";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
const CreateExerciseSchema = z.object({
|
||||
name: z.string().min(1, "Exercise name is required"),
|
||||
type: z.string().min(1),
|
||||
muscleGroups: z.array(z.string()).default([]),
|
||||
description: z.string().optional(),
|
||||
inputFields: z.array(z.string().min(1)).optional(),
|
||||
defaultWeightUnit: z.string().nullable().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/exercises
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const query = searchParams.get("q");
|
||||
|
||||
let exercises;
|
||||
|
||||
if (query) {
|
||||
exercises = await prisma.exercise.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
name: { contains: query },
|
||||
},
|
||||
orderBy: { name: "asc" },
|
||||
});
|
||||
} else {
|
||||
exercises = await getExercises(user.id);
|
||||
}
|
||||
|
||||
return NextResponse.json(exercises);
|
||||
} catch (error) {
|
||||
console.error("GET /api/exercises error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/exercises
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = CreateExerciseSchema.parse(body);
|
||||
|
||||
const existing = await prisma.exercise.findUnique({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name: validated.name,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json(
|
||||
{ error: "Exercise already exists" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Determine default inputFields based on type
|
||||
let inputFields = validated.inputFields;
|
||||
if (!inputFields) {
|
||||
if (validated.type === "cardio") {
|
||||
inputFields = ["sets", "duration", "calories"];
|
||||
} else {
|
||||
inputFields = ["sets", "reps", "weight"];
|
||||
}
|
||||
}
|
||||
|
||||
// Kettlebell defaults to kg
|
||||
let defaultWeightUnit = validated.defaultWeightUnit;
|
||||
if (defaultWeightUnit === undefined && validated.type === "kettlebell") {
|
||||
defaultWeightUnit = "kg";
|
||||
}
|
||||
|
||||
const exercise = await createExercise({
|
||||
userId: user.id,
|
||||
name: validated.name,
|
||||
type: validated.type,
|
||||
description: validated.description,
|
||||
muscleGroups: JSON.stringify(validated.muscleGroups),
|
||||
inputFields: JSON.stringify(inputFields),
|
||||
defaultWeightUnit: defaultWeightUnit || null,
|
||||
isCustom: true,
|
||||
});
|
||||
|
||||
return NextResponse.json(exercise, { status: 201 });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: "Validation error", details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.error("POST /api/exercises error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
export const revalidate = 0;
|
||||
|
||||
/**
|
||||
* GET /api/health
|
||||
* Health check endpoint — verifies both the server and database are operational.
|
||||
* Used by StartOS health checks and Docker health checks.
|
||||
* Excluded from auth middleware in middleware.ts.
|
||||
*/
|
||||
export async function GET() {
|
||||
try {
|
||||
// Verify database connectivity with a lightweight query
|
||||
const userCount = await prisma.user.count();
|
||||
|
||||
return NextResponse.json({
|
||||
status: "ok",
|
||||
timestamp: Date.now(),
|
||||
database: "connected",
|
||||
users: userCount,
|
||||
});
|
||||
} catch (error) {
|
||||
// Server is up but database is unreachable or corrupted
|
||||
return NextResponse.json(
|
||||
{
|
||||
status: "error",
|
||||
timestamp: Date.now(),
|
||||
database: "disconnected",
|
||||
error: error instanceof Error ? error.message : "Unknown database error",
|
||||
},
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
// Exercise name mapping - CSV shorthand to DB names
|
||||
const NAME_MAP: Record<string, string> = {
|
||||
"Ab Wheel": "Ab Wheel Rollout",
|
||||
"BB Upright Row": "Upright Row",
|
||||
"Ball Situp": "Exercise Ball Situp",
|
||||
"Bench": "Bench Press",
|
||||
"DB Lateral Raise": "Lateral Raise",
|
||||
"Dip": "Dips (Chest)",
|
||||
"Face Pull": "Face Pulls",
|
||||
"SA Lat Pulldown": "Lat Pulldown",
|
||||
"SL Calf Raise": "Calf Raise",
|
||||
"BB Row": "Barbell Row",
|
||||
"DB Row": "Dumbbell Row",
|
||||
"GHD": "Glute ham developer",
|
||||
"Hamstring DL": "Hamstring deadlift",
|
||||
"BB Curl": "Barbell Curl",
|
||||
"BB Hip Bridge": "Hip Thrust",
|
||||
"Cable Trap": "Rear delt",
|
||||
"Chinup (Narrow)": "Chinup",
|
||||
"Chinup Negatives": "Chinup",
|
||||
"Squat (Foot Elevated)": "Squat",
|
||||
"Ball Bicep Curl": "Dumbbell Curl",
|
||||
"KB Hip Flexor": "Hip Flexor",
|
||||
"Hamstring Deadlift": "Hamstring deadlift",
|
||||
"Shoulder Press": "Overhead Press",
|
||||
"CoC": "Captains of Crush",
|
||||
"Hex DL": "Hex Bar Deadlift",
|
||||
"KB Extension": "Kettlebell Leg Extension",
|
||||
"Ski": "SkiErg",
|
||||
};
|
||||
|
||||
interface ParsedSet {
|
||||
setNumber: number;
|
||||
weight?: number;
|
||||
weightUnit: string;
|
||||
reps?: number;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
interface ParsedExercise {
|
||||
exerciseId: string;
|
||||
exerciseName: string;
|
||||
sets: ParsedSet[];
|
||||
}
|
||||
|
||||
interface ParsedWorkout {
|
||||
date: string;
|
||||
exercises: ParsedExercise[];
|
||||
}
|
||||
|
||||
interface ParseResponse {
|
||||
workouts: ParsedWorkout[];
|
||||
unmapped: string[];
|
||||
}
|
||||
|
||||
function parseCSV(content: string): Array<Record<string, string>> {
|
||||
const lines = content.trim().split("\n");
|
||||
if (lines.length === 0) return [];
|
||||
|
||||
// Parse header
|
||||
const header = lines[0].split(",").map((h) => h.trim().toLowerCase());
|
||||
const rows = [];
|
||||
|
||||
// Parse data rows
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
const line = lines[i].trim();
|
||||
if (!line) continue;
|
||||
|
||||
const values = line.split(",").map((v) => v.trim());
|
||||
const row: Record<string, string> = {};
|
||||
|
||||
header.forEach((col, idx) => {
|
||||
if (values[idx]) {
|
||||
row[col] = values[idx];
|
||||
}
|
||||
});
|
||||
|
||||
rows.push(row);
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
function getVariationNote(originalName: string): string | null {
|
||||
if (originalName.includes("Narrow")) return "narrow";
|
||||
if (originalName.includes("Negatives")) return "negatives";
|
||||
if (originalName.includes("Foot Elevated")) return "foot elevated";
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveExerciseName(csvName: string): string {
|
||||
// Check if it's in the name map
|
||||
if (NAME_MAP[csvName]) {
|
||||
return NAME_MAP[csvName];
|
||||
}
|
||||
// Return as-is for direct lookup
|
||||
return csvName;
|
||||
}
|
||||
|
||||
// Parse dates like "1/27/2026" or "2026-01-27" into ISO date string
|
||||
function parseDate(dateStr: string): string {
|
||||
// Try M/D/YYYY format
|
||||
const mdyMatch = dateStr.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
|
||||
if (mdyMatch) {
|
||||
const month = mdyMatch[1].padStart(2, "0");
|
||||
const day = mdyMatch[2].padStart(2, "0");
|
||||
const year = mdyMatch[3];
|
||||
return `${year}-${month}-${day}T12:00:00.000Z`;
|
||||
}
|
||||
// Try ISO format
|
||||
if (dateStr.includes("-")) {
|
||||
return new Date(dateStr + "T12:00:00.000Z").toISOString();
|
||||
}
|
||||
// Fallback
|
||||
return new Date(dateStr).toISOString();
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const file = formData.get("file") as File;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: "No file provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!file.name.endsWith(".csv")) {
|
||||
return NextResponse.json(
|
||||
{ error: "File must be a CSV file" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const content = await file.text();
|
||||
const rows = parseCSV(content);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "CSV is empty or invalid format" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get all user exercises for matching
|
||||
const exercises = await prisma.exercise.findMany({
|
||||
where: { userId: user.id },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
|
||||
// Build case-insensitive lookup map
|
||||
const exerciseMap = new Map<string, string>();
|
||||
for (const ex of exercises) {
|
||||
exerciseMap.set(ex.name.toLowerCase(), ex.id);
|
||||
}
|
||||
|
||||
// Group rows by date
|
||||
const workoutsByDate = new Map<string, Array<Record<string, string>>>();
|
||||
|
||||
const unmappedExercises = new Set<string>();
|
||||
|
||||
for (const row of rows) {
|
||||
const date = row.date || row.date_str || row.workout_date || "";
|
||||
const exerciseName = row.exercise || row.exercise_name || "";
|
||||
|
||||
if (!date || !exerciseName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!workoutsByDate.has(date)) {
|
||||
workoutsByDate.set(date, []);
|
||||
}
|
||||
|
||||
workoutsByDate.get(date)!.push(row);
|
||||
|
||||
// Check if exercise can be resolved
|
||||
const resolvedName = resolveExerciseName(exerciseName);
|
||||
const isKnown = exerciseMap.has(resolvedName.toLowerCase());
|
||||
if (!isKnown) {
|
||||
unmappedExercises.add(exerciseName);
|
||||
}
|
||||
}
|
||||
|
||||
// Build parsed workouts
|
||||
const parsedWorkouts: ParsedWorkout[] = [];
|
||||
|
||||
for (const [date, rowsForDate] of workoutsByDate) {
|
||||
const exercisesMap = new Map<
|
||||
string,
|
||||
{
|
||||
exerciseId: string;
|
||||
exerciseName: string;
|
||||
sets: ParsedSet[];
|
||||
}
|
||||
>();
|
||||
|
||||
for (const row of rowsForDate) {
|
||||
const csvExerciseName = row.exercise || row.exercise_name || "";
|
||||
const resolvedName = resolveExerciseName(csvExerciseName);
|
||||
const exerciseId =
|
||||
exerciseMap.get(resolvedName.toLowerCase()) || "";
|
||||
|
||||
if (!exerciseId) {
|
||||
unmappedExercises.add(csvExerciseName);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!exercisesMap.has(exerciseId)) {
|
||||
exercisesMap.set(exerciseId, {
|
||||
exerciseId,
|
||||
exerciseName: resolvedName,
|
||||
sets: [],
|
||||
});
|
||||
}
|
||||
|
||||
const exerciseData = exercisesMap.get(exerciseId)!;
|
||||
const weight = row.weight ? parseFloat(row.weight) : undefined;
|
||||
const reps = row.reps ? parseInt(row.reps, 10) : undefined;
|
||||
let notes = row.notes || "";
|
||||
|
||||
// Detect weight unit from notes
|
||||
let weightUnit = "lbs";
|
||||
if (notes.toLowerCase().includes("kg")) {
|
||||
weightUnit = "kg";
|
||||
}
|
||||
|
||||
// Add variation note if applicable
|
||||
const variationNote = getVariationNote(csvExerciseName);
|
||||
if (variationNote) {
|
||||
notes = notes
|
||||
? `${notes} (${variationNote})`
|
||||
: `(${variationNote})`;
|
||||
}
|
||||
|
||||
const setNumber = exerciseData.sets.length + 1;
|
||||
|
||||
exerciseData.sets.push({
|
||||
setNumber,
|
||||
weight,
|
||||
weightUnit,
|
||||
reps,
|
||||
notes: notes || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
const workoutExercises = Array.from(exercisesMap.values());
|
||||
if (workoutExercises.length > 0) {
|
||||
parsedWorkouts.push({
|
||||
date: parseDate(date),
|
||||
exercises: workoutExercises,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by date ascending (oldest first)
|
||||
parsedWorkouts.sort(
|
||||
(a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()
|
||||
);
|
||||
|
||||
const response: ParseResponse = {
|
||||
workouts: parsedWorkouts,
|
||||
unmapped: Array.from(unmappedExercises),
|
||||
};
|
||||
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
console.error("CSV parsing error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to parse CSV file" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
const PreferencesSchema = z.object({
|
||||
theme: z.enum(["light", "dark", "system"]).optional(),
|
||||
defaultWeightUnit: z.enum(["lbs", "kg"]).optional(),
|
||||
enableClaudeAI: z.boolean().optional(),
|
||||
claudeApiKey: z.string().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/preferences
|
||||
* Get user preferences
|
||||
*/
|
||||
export async function GET(_request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
let preferences = await prisma.userPreferences.findUnique({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
|
||||
if (!preferences) {
|
||||
// Create default preferences
|
||||
preferences = await prisma.userPreferences.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
theme: "system",
|
||||
defaultWeightUnit: "lbs",
|
||||
defaultRestSeconds: 90,
|
||||
enableClaudeAI: false,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Don't return API key in response
|
||||
const { claudeApiKey, ...safePreferences } = preferences;
|
||||
return NextResponse.json({
|
||||
...safePreferences,
|
||||
claudeApiKey: claudeApiKey ? "***" : undefined,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("GET /api/preferences error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/preferences
|
||||
* Update user preferences
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = PreferencesSchema.parse(body);
|
||||
|
||||
// Get or create preferences
|
||||
let preferences = await prisma.userPreferences.findUnique({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
|
||||
if (!preferences) {
|
||||
preferences = await prisma.userPreferences.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
...validated,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
preferences = await prisma.userPreferences.update({
|
||||
where: { userId: user.id },
|
||||
data: validated,
|
||||
});
|
||||
}
|
||||
|
||||
// Don't return API key in response
|
||||
const { claudeApiKey, ...safePreferences } = preferences;
|
||||
return NextResponse.json({
|
||||
...safePreferences,
|
||||
claudeApiKey: claudeApiKey ? "***" : undefined,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Validation error",
|
||||
details: error.errors,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.error("POST /api/preferences error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { writeFile, copyFile, unlink } from "fs/promises";
|
||||
import { existsSync } from "fs";
|
||||
import path from "path";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
/**
|
||||
* POST /api/settings/import-db
|
||||
* Upload a SQLite database file to replace the current one.
|
||||
* Creates a backup of the existing DB before replacing.
|
||||
* Validates the uploaded file is a valid SQLite database with the expected tables.
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const file = formData.get("database") as File | null;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
{ error: "No database file provided" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Basic size check (SQLite DBs for this app should be under 100MB)
|
||||
if (file.size > 100 * 1024 * 1024) {
|
||||
return NextResponse.json(
|
||||
{ error: "File too large (max 100MB)" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Read the uploaded file into a buffer
|
||||
const bytes = await file.arrayBuffer();
|
||||
const buffer = Buffer.from(bytes);
|
||||
|
||||
// Check SQLite magic bytes (first 16 bytes should start with "SQLite format 3\0")
|
||||
const magic = buffer.slice(0, 16).toString("ascii");
|
||||
if (!magic.startsWith("SQLite format 3")) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid file — not a SQLite database" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Determine the database file path from DATABASE_URL
|
||||
const dbUrl = process.env.DATABASE_URL || "file:./data/app.db";
|
||||
let dbPath: string;
|
||||
if (dbUrl.startsWith("file:")) {
|
||||
dbPath = dbUrl.replace("file:", "");
|
||||
// Handle relative paths
|
||||
if (!path.isAbsolute(dbPath)) {
|
||||
dbPath = path.resolve(process.cwd(), "prisma", dbPath.replace("./", ""));
|
||||
}
|
||||
} else {
|
||||
dbPath = path.resolve(process.cwd(), "prisma", "data", "app.db");
|
||||
}
|
||||
|
||||
// Write uploaded file to a temp location for validation
|
||||
const tempPath = dbPath + ".upload-temp";
|
||||
await writeFile(tempPath, buffer);
|
||||
|
||||
// Validate the uploaded DB has the expected tables
|
||||
try {
|
||||
const tables = execSync(
|
||||
`sqlite3 "${tempPath}" "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"`,
|
||||
{ encoding: "utf-8", timeout: 10000 }
|
||||
).trim();
|
||||
|
||||
const tableList = tables.split("\n").map((t) => t.trim());
|
||||
const requiredTables = ["User", "Exercise", "Workout", "SetLog"];
|
||||
const missingTables = requiredTables.filter(
|
||||
(t) => !tableList.includes(t)
|
||||
);
|
||||
|
||||
if (missingTables.length > 0) {
|
||||
await unlink(tempPath);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `Invalid database — missing tables: ${missingTables.join(", ")}. This doesn't look like a Workout Planner database.`,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Run integrity check
|
||||
const integrity = execSync(
|
||||
`sqlite3 "${tempPath}" "PRAGMA integrity_check;"`,
|
||||
{ encoding: "utf-8", timeout: 10000 }
|
||||
).trim();
|
||||
|
||||
if (integrity !== "ok") {
|
||||
await unlink(tempPath);
|
||||
return NextResponse.json(
|
||||
{ error: "Database integrity check failed — file may be corrupted" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
// Clean up temp file
|
||||
if (existsSync(tempPath)) await unlink(tempPath);
|
||||
return NextResponse.json(
|
||||
{ error: "Could not validate the uploaded database file" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get some stats from the uploaded DB for the response
|
||||
let stats = { users: 0, exercises: 0, workouts: 0 };
|
||||
try {
|
||||
const userCount = execSync(
|
||||
`sqlite3 "${tempPath}" "SELECT COUNT(*) FROM User;"`,
|
||||
{ encoding: "utf-8" }
|
||||
).trim();
|
||||
const exerciseCount = execSync(
|
||||
`sqlite3 "${tempPath}" "SELECT COUNT(*) FROM Exercise;"`,
|
||||
{ encoding: "utf-8" }
|
||||
).trim();
|
||||
const workoutCount = execSync(
|
||||
`sqlite3 "${tempPath}" "SELECT COUNT(*) FROM Workout;"`,
|
||||
{ encoding: "utf-8" }
|
||||
).trim();
|
||||
stats = {
|
||||
users: parseInt(userCount) || 0,
|
||||
exercises: parseInt(exerciseCount) || 0,
|
||||
workouts: parseInt(workoutCount) || 0,
|
||||
};
|
||||
} catch {
|
||||
// Stats are optional, continue anyway
|
||||
}
|
||||
|
||||
// Back up the current database
|
||||
const backupPath = dbPath + ".backup-" + Date.now();
|
||||
if (existsSync(dbPath)) {
|
||||
await copyFile(dbPath, backupPath);
|
||||
}
|
||||
|
||||
// Replace the current database with the uploaded one
|
||||
await copyFile(tempPath, dbPath);
|
||||
|
||||
// Also remove WAL/SHM files if they exist (SQLite journal files)
|
||||
for (const ext of ["-wal", "-shm", "-journal"]) {
|
||||
const journalPath = dbPath + ext;
|
||||
if (existsSync(journalPath)) {
|
||||
await unlink(journalPath);
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up temp file
|
||||
await unlink(tempPath);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Database imported successfully. Please refresh the page.",
|
||||
stats,
|
||||
backup: path.basename(backupPath),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Database import error:", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "An error occurred during import",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma, getCaloriesBurned, setCaloriesBurned } from "@/lib/prisma";
|
||||
import { z } from "zod";
|
||||
|
||||
// GET: Get workout by ID
|
||||
export async function GET(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const workout = await prisma.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
setLogs: {
|
||||
include: {
|
||||
exercise: true,
|
||||
},
|
||||
orderBy: {
|
||||
setNumber: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!workout) {
|
||||
return NextResponse.json({ error: "Workout not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (workout.userId !== user.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
||||
}
|
||||
|
||||
// Prisma client doesn't know about caloriesBurned — fetch via raw SQL
|
||||
const caloriesBurned = await getCaloriesBurned(workout.id);
|
||||
|
||||
return NextResponse.json({ ...workout, caloriesBurned });
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch workout:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch workout" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH: Update workout — supports metadata-only or full update with sets
|
||||
const setSchema = z.object({
|
||||
exerciseId: z.string().min(1),
|
||||
setNumber: z.number().int().positive(),
|
||||
reps: z.number().int().positive().optional().nullable(),
|
||||
weight: z.number().optional().nullable(),
|
||||
weightUnit: z.string().default("lbs"),
|
||||
rpe: z.number().int().min(1).max(10).optional().nullable(),
|
||||
notes: z.string().optional().nullable(),
|
||||
});
|
||||
|
||||
const updateWorkoutSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
notes: z.string().optional().nullable(),
|
||||
date: z.string().optional(), // ISO date string
|
||||
durationMinutes: z.number().int().positive().optional().nullable(),
|
||||
difficulty: z.number().int().min(1).max(10).optional().nullable(),
|
||||
caloriesBurned: z.number().int().positive().optional().nullable(),
|
||||
sets: z.array(setSchema).optional(), // if provided, replaces all sets
|
||||
});
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const workout = await prisma.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
select: { userId: true },
|
||||
});
|
||||
|
||||
if (!workout) {
|
||||
return NextResponse.json({ error: "Workout not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (workout.userId !== user.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = updateWorkoutSchema.parse(body);
|
||||
|
||||
// Extract caloriesBurned separately — handled via raw SQL
|
||||
const caloriesValue = validated.caloriesBurned;
|
||||
const hasCaloriesUpdate = validated.caloriesBurned !== undefined;
|
||||
|
||||
// Build the Prisma-compatible workout update data (no caloriesBurned)
|
||||
const workoutData: Record<string, unknown> = {};
|
||||
if (validated.name !== undefined) workoutData.name = validated.name;
|
||||
if (validated.notes !== undefined) workoutData.notes = validated.notes || null;
|
||||
if (validated.date !== undefined) workoutData.date = new Date(validated.date);
|
||||
if (validated.durationMinutes !== undefined)
|
||||
workoutData.durationMinutes = validated.durationMinutes;
|
||||
if (validated.difficulty !== undefined)
|
||||
workoutData.difficulty = validated.difficulty;
|
||||
|
||||
// If sets are provided, do a full replace inside a transaction
|
||||
if (validated.sets) {
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
// Update workout metadata (without caloriesBurned)
|
||||
if (Object.keys(workoutData).length > 0) {
|
||||
await tx.workout.update({ where: { id: params.id }, data: workoutData });
|
||||
}
|
||||
|
||||
// Delete all existing sets
|
||||
await tx.setLog.deleteMany({
|
||||
where: { workoutId: params.id },
|
||||
});
|
||||
|
||||
// Create new sets
|
||||
if (validated.sets!.length > 0) {
|
||||
await tx.setLog.createMany({
|
||||
data: validated.sets!.map((set) => ({
|
||||
workoutId: params.id,
|
||||
exerciseId: set.exerciseId,
|
||||
setNumber: set.setNumber,
|
||||
reps: set.reps ?? undefined,
|
||||
weight: set.weight ?? undefined,
|
||||
weightUnit: set.weightUnit,
|
||||
rpe: set.rpe ?? undefined,
|
||||
notes: set.notes ?? undefined,
|
||||
} as any)),
|
||||
});
|
||||
}
|
||||
|
||||
// Return full updated workout
|
||||
return tx.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
setLogs: {
|
||||
include: { exercise: true },
|
||||
orderBy: { setNumber: "asc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// Update caloriesBurned via raw SQL (outside transaction since Prisma doesn't know this column)
|
||||
if (hasCaloriesUpdate) {
|
||||
await setCaloriesBurned(params.id, caloriesValue ?? null);
|
||||
}
|
||||
const calories = await getCaloriesBurned(params.id);
|
||||
|
||||
return NextResponse.json({ ...result, caloriesBurned: calories });
|
||||
}
|
||||
|
||||
// Metadata-only update
|
||||
if (Object.keys(workoutData).length > 0) {
|
||||
await prisma.workout.update({
|
||||
where: { id: params.id },
|
||||
data: workoutData,
|
||||
});
|
||||
}
|
||||
|
||||
// Update caloriesBurned via raw SQL
|
||||
if (hasCaloriesUpdate) {
|
||||
await setCaloriesBurned(params.id, caloriesValue ?? null);
|
||||
}
|
||||
|
||||
const updated = await prisma.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
setLogs: {
|
||||
include: { exercise: true },
|
||||
orderBy: { setNumber: "asc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
const calories = await getCaloriesBurned(params.id);
|
||||
|
||||
return NextResponse.json({ ...updated, caloriesBurned: calories });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid data", details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
console.error("Failed to update workout:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update workout" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: Delete workout
|
||||
export async function DELETE(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const workout = await prisma.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
select: { userId: true },
|
||||
});
|
||||
|
||||
if (!workout) {
|
||||
return NextResponse.json({ error: "Workout not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (workout.userId !== user.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
||||
}
|
||||
|
||||
await prisma.workout.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("Failed to delete workout:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to delete workout" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { z } from "zod";
|
||||
|
||||
const addSetsSchema = z.object({
|
||||
exerciseId: z.string().min(1),
|
||||
sets: z.array(
|
||||
z.object({
|
||||
setNumber: z.number().int().positive(),
|
||||
reps: z.number().int().positive().optional(),
|
||||
weight: z.number().optional(),
|
||||
weightUnit: z.string().default("lbs"),
|
||||
rpe: z.number().int().min(1).max(10).optional(),
|
||||
durationSeconds: z.number().int().positive().optional(),
|
||||
distance: z.number().positive().optional(),
|
||||
distanceUnit: z.string().optional(),
|
||||
calories: z.number().int().positive().optional(),
|
||||
notes: z.string().optional(),
|
||||
})
|
||||
),
|
||||
});
|
||||
|
||||
// POST: Add an exercise's sets to an existing workout
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const workout = await prisma.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
select: { userId: true },
|
||||
});
|
||||
|
||||
if (!workout) {
|
||||
return NextResponse.json({ error: "Workout not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (workout.userId !== user.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = addSetsSchema.parse(body);
|
||||
|
||||
// Delete existing sets for this exercise in this workout (replace mode)
|
||||
await prisma.setLog.deleteMany({
|
||||
where: {
|
||||
workoutId: params.id,
|
||||
exerciseId: validated.exerciseId,
|
||||
},
|
||||
});
|
||||
|
||||
// Create new sets
|
||||
await prisma.setLog.createMany({
|
||||
data: validated.sets.map((set) => ({
|
||||
workoutId: params.id,
|
||||
exerciseId: validated.exerciseId,
|
||||
setNumber: set.setNumber,
|
||||
reps: set.reps,
|
||||
weight: set.weight,
|
||||
weightUnit: set.weightUnit,
|
||||
rpe: set.rpe,
|
||||
durationSeconds: set.durationSeconds,
|
||||
distance: set.distance,
|
||||
distanceUnit: set.distanceUnit,
|
||||
calories: set.calories,
|
||||
notes: set.notes,
|
||||
} as any)),
|
||||
});
|
||||
|
||||
// Return updated workout
|
||||
const updated = await prisma.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
setLogs: {
|
||||
include: { exercise: true },
|
||||
orderBy: { setNumber: "asc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated, { status: 201 });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid data", details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
console.error("Failed to add sets:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to add sets" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: Remove all sets for a specific exercise from a workout
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const workout = await prisma.workout.findUnique({
|
||||
where: { id: params.id },
|
||||
select: { userId: true },
|
||||
});
|
||||
|
||||
if (!workout) {
|
||||
return NextResponse.json({ error: "Workout not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (workout.userId !== user.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const exerciseId = searchParams.get("exerciseId");
|
||||
|
||||
if (!exerciseId) {
|
||||
return NextResponse.json(
|
||||
{ error: "exerciseId query param required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.setLog.deleteMany({
|
||||
where: {
|
||||
workoutId: params.id,
|
||||
exerciseId,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("Failed to delete sets:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to delete sets" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const importSchema = z.object({
|
||||
images: z.array(z.string()).min(1, "At least one image is required"),
|
||||
});
|
||||
|
||||
const CLAUDE_API_URL = "https://api.anthropic.com/v1/messages";
|
||||
|
||||
const SYSTEM_PROMPT = `You are analyzing photos of handwritten workout logs, Apple Notes, or other workout records. Extract all workout data you can find.
|
||||
|
||||
IMPORTANT RULES:
|
||||
- If you can identify a date for the workout, include it as an ISO date string (YYYY-MM-DD)
|
||||
- If no date is visible, set date to null
|
||||
- Extract exercise names as closely as written
|
||||
- For each exercise, extract all sets with whatever data is visible (reps, weight, duration, etc.)
|
||||
- If you're unsure about an exercise name or value, set "uncertain": true and explain in "uncertainReason"
|
||||
- Weight units: assume lbs unless kg or kilograms is explicitly written
|
||||
- For cardio exercises (running, biking, rowing, assault bike, jump rope, etc.), look for duration, distance, and calories
|
||||
- Be conservative — only include data you can actually read
|
||||
|
||||
Return ONLY valid JSON with this exact structure (no markdown, no code fences):
|
||||
{
|
||||
"workouts": [
|
||||
{
|
||||
"date": "2025-01-15" or null,
|
||||
"name": "Upper Body" or null,
|
||||
"notes": "any overall notes" or null,
|
||||
"exercises": [
|
||||
{
|
||||
"name": "Bench Press",
|
||||
"type": "barbell" | "dumbbell" | "machine" | "cable" | "bodyweight" | "cardio" | "kettlebell" | "other",
|
||||
"sets": [
|
||||
{
|
||||
"reps": 8,
|
||||
"weight": 225,
|
||||
"weightUnit": "lbs",
|
||||
"durationSeconds": null,
|
||||
"distance": null,
|
||||
"distanceUnit": null,
|
||||
"calories": null,
|
||||
"rpe": null,
|
||||
"notes": null
|
||||
}
|
||||
],
|
||||
"notes": null,
|
||||
"uncertain": false,
|
||||
"uncertainReason": null
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"confidence": "high" | "medium" | "low",
|
||||
"warnings": ["list any legibility issues or assumptions made"]
|
||||
}`;
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Get user's Claude API key from preferences
|
||||
const preferences = await prisma.userPreferences.findUnique({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
|
||||
if (!preferences?.enableClaudeAI || !preferences?.claudeApiKey) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Claude AI is not configured. Please add your API key in Settings.",
|
||||
code: "NO_API_KEY",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = importSchema.parse(body);
|
||||
|
||||
// Build Claude API request with vision
|
||||
const content: any[] = [
|
||||
{
|
||||
type: "text",
|
||||
text: "Please analyze the following workout log image(s) and extract all workout data. Return ONLY valid JSON.",
|
||||
},
|
||||
];
|
||||
|
||||
// Add each image
|
||||
for (const imageData of validated.images) {
|
||||
// imageData could be a data URL or raw base64
|
||||
let base64 = imageData;
|
||||
let mediaType = "image/jpeg";
|
||||
|
||||
if (imageData.startsWith("data:")) {
|
||||
const match = imageData.match(/^data:(image\/\w+);base64,(.+)$/);
|
||||
if (match) {
|
||||
mediaType = match[1];
|
||||
base64 = match[2];
|
||||
}
|
||||
}
|
||||
|
||||
content.push({
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: mediaType,
|
||||
data: base64,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Call Claude API
|
||||
const claudeResponse = await fetch(CLAUDE_API_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-api-key": preferences.claudeApiKey,
|
||||
"anthropic-version": "2023-06-01",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "claude-sonnet-4-20250514",
|
||||
max_tokens: 4096,
|
||||
system: SYSTEM_PROMPT,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content,
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
if (!claudeResponse.ok) {
|
||||
const errorBody = await claudeResponse.text();
|
||||
console.error("Claude API error:", claudeResponse.status, errorBody);
|
||||
|
||||
if (claudeResponse.status === 401) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid Claude API key. Please check your key in Settings.", code: "INVALID_KEY" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (claudeResponse.status === 429) {
|
||||
return NextResponse.json(
|
||||
{ error: "Claude API rate limit reached. Please try again in a moment.", code: "RATE_LIMITED" },
|
||||
{ status: 429 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to analyze images. Please try again.", code: "API_ERROR" },
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
|
||||
const claudeData = await claudeResponse.json();
|
||||
|
||||
// Extract text content from Claude's response
|
||||
const textContent = claudeData.content?.find((c: any) => c.type === "text");
|
||||
if (!textContent?.text) {
|
||||
return NextResponse.json(
|
||||
{ error: "No response from Claude. Please try again.", code: "EMPTY_RESPONSE" },
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
|
||||
// Parse the JSON response
|
||||
let parsed;
|
||||
try {
|
||||
// Try to extract JSON from the response (Claude might wrap it in code fences)
|
||||
let jsonText = textContent.text.trim();
|
||||
const jsonMatch = jsonText.match(/```(?:json)?\s*([\s\S]*?)```/);
|
||||
if (jsonMatch) {
|
||||
jsonText = jsonMatch[1].trim();
|
||||
}
|
||||
parsed = JSON.parse(jsonText);
|
||||
} catch {
|
||||
console.error("Failed to parse Claude response:", textContent.text);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Could not parse the workout data. The image may be too unclear.",
|
||||
code: "PARSE_ERROR",
|
||||
raw: textContent.text.substring(0, 500),
|
||||
},
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
|
||||
// Validate basic structure
|
||||
if (!parsed.workouts || !Array.isArray(parsed.workouts)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid response structure from Claude.", code: "INVALID_STRUCTURE" },
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(parsed);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid request data", details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.error("POST /api/workouts/import error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const setSchema = z.object({
|
||||
reps: z.number().int().positive().optional(),
|
||||
weight: z.number().positive().optional(),
|
||||
weightUnit: z.string().optional(),
|
||||
durationSeconds: z.number().int().positive().optional(),
|
||||
distance: z.number().positive().optional(),
|
||||
distanceUnit: z.string().optional(),
|
||||
calories: z.number().int().positive().optional(),
|
||||
rpe: z.number().int().min(1).max(10).optional(),
|
||||
notes: z.string().optional(),
|
||||
});
|
||||
|
||||
const exerciseSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
type: z.string().optional(),
|
||||
existingExerciseId: z.string().optional(),
|
||||
sets: z.array(setSchema),
|
||||
});
|
||||
|
||||
const workoutSchema = z.object({
|
||||
date: z.string(),
|
||||
name: z.string().optional(),
|
||||
notes: z.string().optional(),
|
||||
exercises: z.array(exerciseSchema),
|
||||
});
|
||||
|
||||
const saveImportSchema = z.object({
|
||||
workouts: z.array(workoutSchema).min(1),
|
||||
});
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = saveImportSchema.parse(body);
|
||||
|
||||
// Load all user exercises for matching
|
||||
const existingExercises = await prisma.exercise.findMany({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
|
||||
// Build a case-insensitive lookup map
|
||||
const exerciseMap = new Map<string, typeof existingExercises[0]>();
|
||||
for (const ex of existingExercises) {
|
||||
exerciseMap.set(ex.name.toLowerCase(), ex);
|
||||
}
|
||||
|
||||
const createdWorkoutIds: string[] = [];
|
||||
|
||||
for (const workoutData of validated.workouts) {
|
||||
// Resolve exercise IDs (match existing or create new)
|
||||
const resolvedExercises: Array<{
|
||||
exerciseId: string;
|
||||
sets: z.infer<typeof setSchema>[];
|
||||
}> = [];
|
||||
|
||||
for (const ex of workoutData.exercises) {
|
||||
let exerciseId: string;
|
||||
|
||||
if (ex.existingExerciseId) {
|
||||
// User explicitly matched this to an existing exercise
|
||||
exerciseId = ex.existingExerciseId;
|
||||
} else {
|
||||
// Try case-insensitive match
|
||||
const matched = exerciseMap.get(ex.name.toLowerCase());
|
||||
if (matched) {
|
||||
exerciseId = matched.id;
|
||||
} else {
|
||||
// Create new exercise
|
||||
const newExercise = await prisma.exercise.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
name: ex.name,
|
||||
type: ex.type || "other",
|
||||
muscleGroups: JSON.stringify([]),
|
||||
isCustom: true,
|
||||
} as any,
|
||||
});
|
||||
exerciseId = newExercise.id;
|
||||
// Add to map so subsequent references can find it
|
||||
exerciseMap.set(ex.name.toLowerCase(), newExercise);
|
||||
}
|
||||
}
|
||||
|
||||
resolvedExercises.push({ exerciseId, sets: ex.sets });
|
||||
}
|
||||
|
||||
// Create the workout with all sets
|
||||
const setLogsData: any[] = [];
|
||||
for (const resolved of resolvedExercises) {
|
||||
resolved.sets.forEach((set, index) => {
|
||||
setLogsData.push({
|
||||
exerciseId: resolved.exerciseId,
|
||||
setNumber: index + 1,
|
||||
reps: set.reps || null,
|
||||
weight: set.weight || null,
|
||||
weightUnit: set.weightUnit || "lbs",
|
||||
rpe: set.rpe || null,
|
||||
durationSeconds: set.durationSeconds || null,
|
||||
distance: set.distance || null,
|
||||
distanceUnit: set.distanceUnit || null,
|
||||
calories: set.calories || null,
|
||||
notes: set.notes || null,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const workout = await prisma.workout.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
date: new Date(workoutData.date),
|
||||
name: workoutData.name || null,
|
||||
notes: workoutData.notes || null,
|
||||
setLogs: {
|
||||
create: setLogsData,
|
||||
},
|
||||
} as any,
|
||||
});
|
||||
|
||||
createdWorkoutIds.push(workout.id);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
created: createdWorkoutIds,
|
||||
count: createdWorkoutIds.length,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid request data", details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.error("POST /api/workouts/import/save error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { prisma, setCaloriesBurned, getCaloriesBurnedBulk } from "@/lib/prisma";
|
||||
|
||||
// Schema now supports creating empty workouts (just date) or with sets
|
||||
const createWorkoutSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
notes: z.string().optional(),
|
||||
durationMinutes: z.number().int().positive().optional(),
|
||||
difficulty: z.number().int().min(1).max(10).optional(),
|
||||
caloriesBurned: z.number().int().positive().optional(),
|
||||
date: z.string().optional(), // ISO date string or date-only string
|
||||
sets: z
|
||||
.array(
|
||||
z.object({
|
||||
exerciseId: z.string(),
|
||||
setNumber: z.number().int().positive(),
|
||||
reps: z.number().int().positive().optional(),
|
||||
weight: z.number().positive().optional(),
|
||||
weightUnit: z.string().default("lbs"),
|
||||
rpe: z.number().int().min(1).max(10).optional(),
|
||||
durationSeconds: z.number().int().positive().optional(),
|
||||
distance: z.number().positive().optional(),
|
||||
distanceUnit: z.string().optional(),
|
||||
calories: z.number().int().positive().optional(),
|
||||
notes: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.optional()
|
||||
.default([]),
|
||||
});
|
||||
|
||||
// GET: List workouts with search/date filters
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const query = searchParams.get("q");
|
||||
const dateFrom = searchParams.get("dateFrom");
|
||||
const dateTo = searchParams.get("dateTo");
|
||||
const limit = Math.min(parseInt(searchParams.get("limit") || "50"), 100);
|
||||
const offset = parseInt(searchParams.get("offset") || "0");
|
||||
|
||||
const where: any = {
|
||||
userId: user.id,
|
||||
};
|
||||
|
||||
if (query) {
|
||||
where.name = {
|
||||
contains: query,
|
||||
};
|
||||
}
|
||||
|
||||
if (dateFrom || dateTo) {
|
||||
where.date = {};
|
||||
if (dateFrom) {
|
||||
where.date.gte = new Date(dateFrom);
|
||||
}
|
||||
if (dateTo) {
|
||||
const toDate = new Date(dateTo);
|
||||
toDate.setHours(23, 59, 59, 999);
|
||||
where.date.lte = toDate;
|
||||
}
|
||||
}
|
||||
|
||||
const [workouts, total] = await Promise.all([
|
||||
prisma.workout.findMany({
|
||||
where,
|
||||
include: {
|
||||
setLogs: {
|
||||
include: {
|
||||
exercise: true,
|
||||
},
|
||||
orderBy: {
|
||||
setNumber: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
date: "desc",
|
||||
},
|
||||
take: limit,
|
||||
skip: offset,
|
||||
}),
|
||||
prisma.workout.count({ where }),
|
||||
]);
|
||||
|
||||
// Supplement with caloriesBurned from raw SQL
|
||||
const ids = workouts.map((w) => w.id);
|
||||
const caloriesMap = await getCaloriesBurnedBulk(ids);
|
||||
const enriched = workouts.map((w) => ({
|
||||
...w,
|
||||
caloriesBurned: caloriesMap[w.id] ?? null,
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
data: enriched,
|
||||
meta: {
|
||||
total,
|
||||
limit,
|
||||
offset,
|
||||
hasMore: offset + limit < total,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch workouts:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch workouts" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST: Create workout (can be empty or with sets)
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const validated = createWorkoutSchema.parse(body);
|
||||
|
||||
const workoutDate = validated.date ? new Date(validated.date) : new Date();
|
||||
|
||||
// Extract caloriesBurned — handled via raw SQL after creation
|
||||
const caloriesValue = validated.caloriesBurned;
|
||||
|
||||
const createData: any = {
|
||||
userId: user.id,
|
||||
name: validated.name || null,
|
||||
notes: validated.notes,
|
||||
durationMinutes: validated.durationMinutes,
|
||||
difficulty: validated.difficulty,
|
||||
// caloriesBurned handled separately via raw SQL
|
||||
date: workoutDate,
|
||||
setLogs:
|
||||
validated.sets.length > 0
|
||||
? {
|
||||
create: validated.sets.map((set) => ({
|
||||
exerciseId: set.exerciseId,
|
||||
setNumber: set.setNumber,
|
||||
reps: set.reps,
|
||||
weight: set.weight,
|
||||
weightUnit: set.weightUnit,
|
||||
rpe: set.rpe,
|
||||
durationSeconds: set.durationSeconds,
|
||||
distance: set.distance,
|
||||
distanceUnit: set.distanceUnit,
|
||||
calories: set.calories,
|
||||
notes: set.notes,
|
||||
} as any)),
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
|
||||
const includeOpts = {
|
||||
setLogs: {
|
||||
include: { exercise: true },
|
||||
orderBy: { setNumber: "asc" as const },
|
||||
},
|
||||
};
|
||||
|
||||
const workout = await prisma.workout.create({ data: createData, include: includeOpts });
|
||||
|
||||
// Set caloriesBurned via raw SQL
|
||||
if (caloriesValue !== undefined) {
|
||||
await setCaloriesBurned(workout.id, caloriesValue);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ...workout, caloriesBurned: caloriesValue ?? null }, { status: 201 });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid request data", details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.error("Failed to create workout:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to create workout" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user