Initial commit for Start9 packaging

This commit is contained in:
MacPro
2026-02-28 09:27:26 -06:00
commit 1b64c45c52
124 changed files with 15671 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
import bcryptjs from "bcryptjs";
import { prisma } from "./prisma";
import { ReadonlyRequestCookies } from "next/dist/server/web/spec-extension/adapters/request-cookies";
import { cookies } from "next/headers";
import { User } from "@prisma/client";
/**
* Hash a password using bcryptjs
*/
export async function hashPassword(password: string): Promise<string> {
const salt = await bcryptjs.genSalt(10);
return bcryptjs.hash(password, salt);
}
/**
* Verify a password against its hash
*/
export async function verifyPassword(
password: string,
hash: string
): Promise<boolean> {
return bcryptjs.compare(password, hash);
}
/**
* Create a session token for a user (30-day expiration)
*/
export async function createSession(
userId: string
): Promise<{ token: string; expiresAt: Date }> {
const token = Buffer.from(
`${userId}:${Date.now()}:${Math.random()}`
).toString("hex");
const expiresAt = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000); // 30 days
await prisma.session.create({
data: {
token,
userId,
expiresAt,
},
});
return { token, expiresAt };
}
/**
* Validate a session token and return the associated user
*/
export async function validateSession(token: string): Promise<User | null> {
const session = await prisma.session.findUnique({
where: { token },
include: { user: true },
});
if (!session) {
return null;
}
// Check if session has expired
if (session.expiresAt < new Date()) {
await prisma.session.delete({ where: { token } });
return null;
}
return session.user;
}
/**
* Delete a session token
*/
export async function deleteSession(token: string): Promise<void> {
await prisma.session.delete({
where: { token },
});
}
/**
* Get session from cookies object
*/
export async function getSessionFromCookies(
cookieStore: ReadonlyRequestCookies
): Promise<User | null> {
const sessionToken = cookieStore.get("sessionToken")?.value;
if (!sessionToken) {
return null;
}
return validateSession(sessionToken);
}
/**
* Get the current user from request cookies
*/
export async function getCurrentUser(): Promise<User | null> {
const cookieStore = await cookies();
return getSessionFromCookies(cookieStore);
}
+111
View File
@@ -0,0 +1,111 @@
import { prisma } from "../prisma";
import { Exercise, SetLog } from "@prisma/client";
/**
* Get all exercises for a user
*/
export async function getExercises(userId: string): Promise<Exercise[]> {
return prisma.exercise.findMany({
where: { userId },
orderBy: {
name: "asc",
},
});
}
/**
* Get a single exercise by ID
*/
export async function getExerciseById(id: string): Promise<Exercise | null> {
return prisma.exercise.findUnique({
where: { id },
});
}
/**
* Create a new exercise
*/
export async function createExercise(data: {
userId: string;
name: string;
type?: string;
description?: string;
muscleGroups?: string;
inputFields?: string;
defaultWeightUnit?: string | null;
isCustom?: boolean;
}): Promise<Exercise> {
return prisma.exercise.create({
data: {
userId: data.userId,
name: data.name,
type: data.type || "other",
description: data.description,
muscleGroups: data.muscleGroups || JSON.stringify([]),
isCustom: data.isCustom || false,
// These fields exist in schema but Prisma client may not be regenerated yet
...(data.inputFields ? { inputFields: data.inputFields } : {}),
...(data.defaultWeightUnit ? { defaultWeightUnit: data.defaultWeightUnit } : {}),
} as any,
});
}
/**
* Get all set logs for an exercise (history)
*/
export async function getExerciseHistory(
exerciseId: string,
userId: string
): Promise<SetLog[]> {
return prisma.setLog.findMany({
where: {
exerciseId,
workout: {
userId,
},
},
orderBy: {
createdAt: "desc",
},
});
}
/**
* Get the personal best (highest weight) for an exercise
*/
export async function getPersonalBest(
exerciseId: string,
userId: string
): Promise<{ weight: number; reps: number; date: Date } | null> {
const set = await prisma.setLog.findFirst({
where: {
exerciseId,
workout: {
userId,
},
},
orderBy: [
{
weight: "desc",
},
{
reps: "desc",
},
],
select: {
weight: true,
reps: true,
createdAt: true,
},
});
if (!set) {
return null;
}
return {
weight: set.weight || 0,
reps: set.reps || 0,
date: set.createdAt,
};
}
+219
View File
@@ -0,0 +1,219 @@
import { prisma } from "../prisma";
import { DashboardStats } from "@/types";
/**
* Get number of workouts this week (Mon-Sun)
*/
export async function getWeeklyWorkoutCount(userId: string): Promise<number> {
const now = new Date();
const dayOfWeek = now.getDay();
const daysBack = dayOfWeek === 0 ? 6 : dayOfWeek - 1; // Monday is 0
const mondayStart = new Date(now);
mondayStart.setDate(mondayStart.getDate() - daysBack);
mondayStart.setHours(0, 0, 0, 0);
const sundayEnd = new Date(mondayStart);
sundayEnd.setDate(sundayEnd.getDate() + 6);
sundayEnd.setHours(23, 59, 59, 999);
const count = await prisma.workout.count({
where: {
userId,
date: {
gte: mondayStart,
lte: sundayEnd,
},
},
});
return count;
}
/**
* Get total workouts ever for user
*/
export async function getTotalWorkoutCount(userId: string): Promise<number> {
return prisma.workout.count({
where: { userId },
});
}
/**
* Get number of workouts this month (1st to end of month)
*/
export async function getMonthlyWorkoutCount(userId: string): Promise<number> {
const now = new Date();
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
monthStart.setHours(0, 0, 0, 0);
const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0);
monthEnd.setHours(23, 59, 59, 999);
return prisma.workout.count({
where: {
userId,
date: {
gte: monthStart,
lte: monthEnd,
},
},
});
}
/**
* Get number of workouts this year (Jan 1 to Dec 31)
*/
export async function getYearlyWorkoutCount(userId: string): Promise<number> {
const now = new Date();
const yearStart = new Date(now.getFullYear(), 0, 1);
yearStart.setHours(0, 0, 0, 0);
const yearEnd = new Date(now.getFullYear(), 11, 31);
yearEnd.setHours(23, 59, 59, 999);
return prisma.workout.count({
where: {
userId,
date: {
gte: yearStart,
lte: yearEnd,
},
},
});
}
/**
* Get current streak (consecutive days with workouts, working backwards from today)
*/
export async function getCurrentStreak(userId: string): Promise<number> {
const workouts = await prisma.workout.findMany({
where: { userId },
select: { date: true },
orderBy: { date: "desc" },
});
if (workouts.length === 0) {
return 0;
}
let streak = 0;
let currentDate = new Date();
currentDate.setHours(0, 0, 0, 0);
for (const workout of workouts) {
const workoutDate = new Date(workout.date);
workoutDate.setHours(0, 0, 0, 0);
const daysDiff = Math.floor(
(currentDate.getTime() - workoutDate.getTime()) / (1000 * 60 * 60 * 24)
);
if (daysDiff === streak) {
streak++;
} else {
break;
}
}
return streak;
}
/**
* Get total volume (sum of reps * weight) for this week
*/
export async function getWeeklyVolume(userId: string): Promise<number> {
const now = new Date();
const dayOfWeek = now.getDay();
const daysBack = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
const mondayStart = new Date(now);
mondayStart.setDate(mondayStart.getDate() - daysBack);
mondayStart.setHours(0, 0, 0, 0);
const sundayEnd = new Date(mondayStart);
sundayEnd.setDate(sundayEnd.getDate() + 6);
sundayEnd.setHours(23, 59, 59, 999);
const setLogs = await prisma.setLog.findMany({
where: {
workout: {
userId,
date: {
gte: mondayStart,
lte: sundayEnd,
},
},
},
select: {
reps: true,
weight: true,
},
});
const totalVolume = setLogs.reduce((sum, set) => {
const reps = set.reps || 0;
const weight = set.weight || 0;
return sum + reps * weight;
}, 0);
return totalVolume;
}
/**
* Get all dashboard stats combined
*/
export async function getDashboardStats(userId: string): Promise<DashboardStats> {
const [
totalWorkouts,
_weeklyWorkoutCount,
_currentStreak,
weeklyVolume,
recentWorkouts,
] = await Promise.all([
getTotalWorkoutCount(userId),
getWeeklyWorkoutCount(userId),
getCurrentStreak(userId),
getWeeklyVolume(userId),
prisma.workout.findMany({
where: { userId },
include: {
setLogs: {
include: {
exercise: true,
},
orderBy: {
setNumber: "asc",
},
},
},
orderBy: {
date: "desc",
},
take: 5,
}),
]);
// Calculate total sets and reps from recent workouts
let totalSets = 0;
let totalReps = 0;
const personalBests: DashboardStats["personalBests"] = [];
for (const workout of recentWorkouts) {
for (const set of (workout as any).setLogs) {
totalSets++;
if (set.reps) {
totalReps += set.reps;
}
}
}
return {
totalWorkouts,
totalVolume: weeklyVolume,
totalSets,
totalReps,
personalBests,
recentWorkouts: recentWorkouts as any,
};
}
+160
View File
@@ -0,0 +1,160 @@
import { prisma } from "../prisma";
import { Workout } from "@prisma/client";
import { SearchFilters } from "@/types";
/**
* Get all workouts for a user with optional filters
*/
export async function getWorkouts(
userId: string,
filters?: SearchFilters
) {
const {
query,
exerciseId,
dateFrom,
dateTo,
limit = 50,
offset = 0,
} = filters || {};
const where: any = {
userId,
};
if (query) {
where.name = {
contains: query,
};
}
if (dateFrom || dateTo) {
where.date = {};
if (dateFrom) where.date.gte = dateFrom;
if (dateTo) where.date.lte = dateTo;
}
const workouts = await prisma.workout.findMany({
where,
include: {
setLogs: {
include: {
exercise: true,
},
orderBy: {
setNumber: "asc",
},
},
},
orderBy: {
date: "desc",
},
take: limit,
skip: offset,
});
// Filter by exerciseId if provided
if (exerciseId) {
return workouts.filter((workout) =>
workout.setLogs.some((set) => set.exerciseId === exerciseId)
);
}
return workouts;
}
/**
* Get a single workout by ID with all its sets
*/
export async function getWorkoutById(id: string) {
return prisma.workout.findUnique({
where: { id },
include: {
setLogs: {
include: {
exercise: true,
},
orderBy: {
setNumber: "asc",
},
},
},
});
}
/**
* Create a new workout
*/
export async function createWorkout(data: {
userId: string;
name: string;
date?: Date;
notes?: string;
duration?: number;
}): Promise<Workout> {
return prisma.workout.create({
data: {
userId: data.userId,
name: data.name,
date: data.date || new Date(),
notes: data.notes,
durationMinutes: data.duration,
},
});
}
/**
* Delete a workout and all its associated sets
*/
export async function deleteWorkout(id: string): Promise<void> {
await prisma.setLog.deleteMany({
where: { workoutId: id },
});
await prisma.workout.delete({
where: { id },
});
}
/**
* Get recent workouts for a user
*/
export async function getRecentWorkouts(
userId: string,
limit: number = 10
) {
return prisma.workout.findMany({
where: { userId },
include: {
setLogs: {
include: {
exercise: true,
},
orderBy: {
setNumber: "asc",
},
},
},
orderBy: {
date: "desc",
},
take: limit,
});
}
/**
* Search workouts by name, date range, or exercise
*/
export async function searchWorkouts(
userId: string,
query: string,
dateFrom?: Date,
dateTo?: Date
) {
return getWorkouts(userId, {
query,
dateFrom,
dateTo,
limit: 100,
});
}
+76
View File
@@ -0,0 +1,76 @@
/**
* Shared exercise search utilities — fuzzy matching + abbreviation expansion.
* Used by both ExercisePicker (log workout) and ExercisesClient (exercise library).
*/
// Common gym abbreviations
const ABBREVIATIONS: Record<string, string> = {
kb: "kettlebell",
db: "dumbbell",
bb: "barbell",
ghd: "glute ham developer",
rdl: "romanian deadlift",
ohp: "overhead press",
};
/**
* Expand a search query into multiple variants using known abbreviations.
* e.g. "kb swing" → ["kb swing", "kettlebell swing"]
*/
export function expandAbbreviations(query: string): string[] {
const lower = query.toLowerCase().trim();
const variants: string[] = [lower];
// Check if the whole query is an abbreviation
if (ABBREVIATIONS[lower]) {
variants.push(ABBREVIATIONS[lower]);
}
// Check if the query starts with an abbreviation followed by a space
for (const [abbr, full] of Object.entries(ABBREVIATIONS)) {
if (lower.startsWith(abbr + " ")) {
variants.push(full + lower.slice(abbr.length));
}
}
return variants;
}
/**
* Score how well a query matches a target string.
* Lower = better match. Returns -1 for no match.
*
* Priority: exact match (0) > starts with (1) > word starts with (2) > substring (3) > fuzzy chars (4+)
*/
export function fuzzyScore(query: string, target: string): number {
const q = query.toLowerCase();
const t = target.toLowerCase();
if (t === q) return 0;
if (t.startsWith(q)) return 1;
const words = t.split(/\s+/);
if (words.some((w) => w.startsWith(q))) return 2;
if (t.includes(q)) return 3;
// Fuzzy character match
let qi = 0;
for (let ti = 0; ti < t.length && qi < q.length; ti++) {
if (t[ti] === q[qi]) qi++;
}
return qi === q.length ? 4 + (t.length - q.length) : -1;
}
/**
* Search exercises using fuzzy matching + abbreviation expansion.
* Returns scored exercises sorted by best match, or -1 for no match.
*/
export function scoreExercise(query: string, exerciseName: string): number {
const variants = expandAbbreviations(query);
const scores = variants
.map((q) => fuzzyScore(q, exerciseName))
.filter((s) => s >= 0);
return scores.length > 0 ? Math.min(...scores) : -1;
}
+43
View File
@@ -0,0 +1,43 @@
/**
* Format an array of sets into a compact grouped summary.
*
* Consecutive sets with the same weight are collapsed:
* "135 x 5, 185 x 5, 205 x 5, 225 x 3, 245 x 3/3/3"
*
* When weightUnit is provided (per-set or as a default), it is appended:
* "16kg x 5/5/5" or "245 x 3/3/3" (lbs omitted since it's the common default)
*
* Sets without weight show just reps; sets without reps are skipped.
*/
export function formatSetsSummary(
sets: Array<{ weight?: number | null; reps?: number | null; weightUnit?: string | null }>,
defaultUnit?: string
): string {
const valid = sets.filter((s) => s.reps);
if (valid.length === 0) return "";
// Group consecutive sets by weight
const groups: Array<{ weight: number | null | undefined; weightUnit: string | null | undefined; reps: number[] }> =
[];
for (const s of valid) {
const unit = s.weightUnit || defaultUnit || null;
const last = groups[groups.length - 1];
if (last && last.weight === s.weight && last.weightUnit === unit) {
last.reps.push(s.reps!);
} else {
groups.push({ weight: s.weight, weightUnit: unit, reps: [s.reps!] });
}
}
return groups
.map((g) => {
const repsStr = g.reps.join("/");
if (g.weight) {
const unit = g.weightUnit === "kg" ? "kg" : "";
return `${g.weight}${unit} x ${repsStr}`;
}
return repsStr;
})
.join(", ");
}
+47
View File
@@ -0,0 +1,47 @@
import { PrismaClient } from "@prisma/client";
declare global {
var prisma: PrismaClient | undefined;
}
export const prisma =
global.prisma ||
new PrismaClient({
log: process.env.NODE_ENV === "development" ? ["error", "warn"] : ["error"],
});
if (process.env.NODE_ENV !== "production") global.prisma = prisma;
/**
* caloriesBurned is in the DB schema but NOT in the generated Prisma client.
* These helpers use raw SQL to read/write it until Prisma client can be regenerated.
*/
export async function getCaloriesBurned(workoutId: string): Promise<number | null> {
const rows = await prisma.$queryRawUnsafe<Array<{ caloriesBurned: number | null }>>(
`SELECT caloriesBurned FROM Workout WHERE id = ?`,
workoutId
);
return rows[0]?.caloriesBurned ?? null;
}
export async function setCaloriesBurned(workoutId: string, calories: number | null): Promise<void> {
await prisma.$executeRawUnsafe(
`UPDATE Workout SET caloriesBurned = ? WHERE id = ?`,
calories,
workoutId
);
}
export async function getCaloriesBurnedBulk(workoutIds: string[]): Promise<Record<string, number | null>> {
if (workoutIds.length === 0) return {};
const placeholders = workoutIds.map(() => "?").join(",");
const rows = await prisma.$queryRawUnsafe<Array<{ id: string; caloriesBurned: number | null }>>(
`SELECT id, caloriesBurned FROM Workout WHERE id IN (${placeholders})`,
...workoutIds
);
const map: Record<string, number | null> = {};
for (const r of rows) {
map[r.id] = r.caloriesBurned;
}
return map;
}
+74
View File
@@ -0,0 +1,74 @@
import { ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { SetLog } from "@prisma/client";
/**
* Merge classnames using clsx and tailwind-merge
*/
export function cn(...inputs: ClassValue[]): string {
return twMerge(clsx(inputs));
}
/**
* Format a date to a readable string (e.g., "Feb 16, 2026")
*/
export function formatDate(date: Date | string): string {
const dateObj = typeof date === "string" ? new Date(date) : date;
return dateObj.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
}
/**
* Format weight with unit (e.g., "150 lbs")
*/
export function formatWeight(weight: number, unit: string): string {
return `${weight} ${unit}`;
}
/**
* Calculate total volume (sets * reps * weight)
*/
export function calculateVolume(sets: SetLog[]): number {
return sets.reduce((total, set) => {
const reps = set.reps || 0;
const weight = set.weight || 0;
return total + reps * weight;
}, 0);
}
/**
* Calculate estimated 1-rep max using Epley formula
* 1RM = weight * (1 + reps / 30)
*/
export function calculateE1RM(weight: number, reps: number): number {
if (reps === 1) return weight;
return Math.round(weight * (1 + reps / 30) * 100) / 100;
}
/**
* Get the start and end of a week for a given date
*/
export function getWeekRange(date: Date): { start: Date; end: Date } {
const d = new Date(date);
const day = d.getDay();
const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Adjust to Monday
const start = new Date(d.setDate(diff));
start.setHours(0, 0, 0, 0);
const end = new Date(start);
end.setDate(end.getDate() + 6);
end.setHours(23, 59, 59, 999);
return { start, end };
}
/**
* Generate a random UUID
*/
export function generateId(): string {
return crypto.randomUUID();
}