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
+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,
});
}