refactor: consolidate formatting utilities and add testing infrastructure

- Replace 8 duplicate formatCurrency functions with shared utility
- Add comprehensive formatter utilities (currency, date, number, etc.)
- Set up Vitest for unit testing with 38 passing tests
- Set up Playwright for E2E testing
- Consolidate database connection to single source (src/utils/db.ts)
- Add auth middleware helpers to reduce code duplication
- Fix display bug: remove spurious minus sign in recent activity amounts
- Add path aliases for cleaner imports ($utils, $models)
- Add project documentation (CODEMAP.md, REFACTORING_PLAN.md)

Test coverage: 38 unit tests passing
Build: successful with no breaking changes
This commit is contained in:
2025-11-18 15:24:22 +01:00
parent d09dc2dfed
commit 10ee2e81ae
58 changed files with 11127 additions and 131 deletions

100
src/models/Exercise.ts Normal file
View File

@@ -0,0 +1,100 @@
import mongoose from 'mongoose';
export interface IExercise {
_id?: string;
exerciseId: string; // Original ExerciseDB ID
name: string;
gifUrl: string; // URL to the exercise animation GIF
bodyPart: string; // e.g., "chest", "back", "legs"
equipment: string; // e.g., "barbell", "dumbbell", "bodyweight"
target: string; // Primary target muscle
secondaryMuscles: string[]; // Secondary muscles worked
instructions: string[]; // Step-by-step instructions
category?: string; // Custom categorization
difficulty?: 'beginner' | 'intermediate' | 'advanced';
isActive?: boolean; // Allow disabling exercises
createdAt?: Date;
updatedAt?: Date;
}
const ExerciseSchema = new mongoose.Schema(
{
exerciseId: {
type: String,
required: true,
unique: true
},
name: {
type: String,
required: true,
trim: true,
index: true // For fast searching
},
gifUrl: {
type: String,
required: true
},
bodyPart: {
type: String,
required: true,
lowercase: true,
index: true // For filtering by body part
},
equipment: {
type: String,
required: true,
lowercase: true,
index: true // For filtering by equipment
},
target: {
type: String,
required: true,
lowercase: true,
index: true // For filtering by target muscle
},
secondaryMuscles: {
type: [String],
default: []
},
instructions: {
type: [String],
required: true,
validate: {
validator: function(instructions: string[]) {
return instructions.length > 0;
},
message: 'Exercise must have at least one instruction'
}
},
category: {
type: String,
trim: true
},
difficulty: {
type: String,
enum: ['beginner', 'intermediate', 'advanced'],
default: 'intermediate'
},
isActive: {
type: Boolean,
default: true
}
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);
// Text search index for exercise names and instructions
ExerciseSchema.index({
name: 'text',
instructions: 'text'
});
// Compound indexes for common queries
ExerciseSchema.index({ bodyPart: 1, equipment: 1 });
ExerciseSchema.index({ target: 1, isActive: 1 });
export const Exercise = mongoose.model<IExercise>("Exercise", ExerciseSchema);

View File

@@ -0,0 +1,228 @@
import mongoose from 'mongoose';
export interface IContestant {
_id?: string;
name: string;
seed?: number; // For bracket seeding
dnf?: boolean; // Did Not Finish - marked as inactive mid-tournament
}
export interface IRound {
roundNumber: number;
scores: Map<string, number>; // contestantId -> score
completedAt?: Date;
}
export interface IGroupMatch {
_id?: string;
contestantIds: string[]; // All contestants in this match
rounds: IRound[];
completed: boolean;
}
export interface IGroup {
_id?: string;
name: string;
contestantIds: string[]; // References to contestants
matches: IGroupMatch[];
standings?: { contestantId: string; totalScore: number; position: number }[];
}
export interface IBracketMatch {
_id?: string;
contestantIds: string[]; // Array of contestant IDs competing in this match
rounds: IRound[];
winnerId?: string;
completed: boolean;
}
export interface IBracketRound {
roundNumber: number; // 1 = finals, 2 = semis, 3 = quarters, etc.
name: string; // "Finals", "Semi-Finals", etc.
matches: IBracketMatch[];
}
export interface IBracket {
rounds: IBracketRound[];
}
export interface IMarioKartTournament {
_id?: string;
name: string;
status: 'setup' | 'group_stage' | 'bracket' | 'completed';
contestants: IContestant[];
groups: IGroup[];
bracket?: IBracket;
runnersUpBracket?: IBracket;
roundsPerMatch: number; // How many rounds in each match
matchSize: number; // How many contestants compete simultaneously (default 2 for 1v1)
createdBy: string;
createdAt?: Date;
updatedAt?: Date;
}
const RoundSchema = new mongoose.Schema({
roundNumber: {
type: Number,
required: true,
min: 1
},
scores: {
type: Map,
of: Number,
required: true
},
completedAt: {
type: Date
}
});
const GroupMatchSchema = new mongoose.Schema({
contestantIds: {
type: [String],
required: true
},
rounds: {
type: [RoundSchema],
default: []
},
completed: {
type: Boolean,
default: false
}
});
const GroupSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
contestantIds: {
type: [String],
required: true
},
matches: {
type: [GroupMatchSchema],
default: []
},
standings: [{
contestantId: String,
totalScore: Number,
position: Number
}]
});
const BracketMatchSchema = new mongoose.Schema({
contestantIds: {
type: [String],
default: [],
required: false
},
rounds: {
type: [RoundSchema],
default: []
},
winnerId: {
type: String,
required: false
},
completed: {
type: Boolean,
default: false
}
}, { _id: true, minimize: false });
const BracketRoundSchema = new mongoose.Schema({
roundNumber: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
matches: {
type: [BracketMatchSchema],
required: true
}
}, { _id: true, minimize: false });
const BracketSchema = new mongoose.Schema({
rounds: {
type: [BracketRoundSchema],
default: []
}
}, { _id: true, minimize: false });
const ContestantSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
seed: {
type: Number
},
dnf: {
type: Boolean,
default: false
}
});
const MarioKartTournamentSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
maxlength: 200
},
status: {
type: String,
enum: ['setup', 'group_stage', 'bracket', 'completed'],
default: 'setup'
},
contestants: {
type: [ContestantSchema],
default: []
},
groups: {
type: [GroupSchema],
default: []
},
bracket: {
type: BracketSchema
},
runnersUpBracket: {
type: BracketSchema
},
roundsPerMatch: {
type: Number,
default: 3,
min: 1,
max: 10
},
matchSize: {
type: Number,
default: 2,
min: 2,
max: 12
},
createdBy: {
type: String,
required: true,
trim: true
}
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);
MarioKartTournamentSchema.index({ createdBy: 1, createdAt: -1 });
export const MarioKartTournament = mongoose.models.MarioKartTournament ||
mongoose.model<IMarioKartTournament>("MarioKartTournament", MarioKartTournamentSchema);

View File

@@ -0,0 +1,143 @@
import mongoose from 'mongoose';
export interface ICompletedSet {
reps: number;
weight?: number;
rpe?: number; // Rate of Perceived Exertion (1-10)
completed: boolean;
notes?: string;
}
export interface ICompletedExercise {
name: string;
sets: ICompletedSet[];
restTime?: number;
notes?: string;
}
export interface IWorkoutSession {
_id?: string;
templateId?: string; // Reference to WorkoutTemplate if based on template
templateName?: string; // Snapshot of template name for history
name: string;
exercises: ICompletedExercise[];
startTime: Date;
endTime?: Date;
duration?: number; // Duration in minutes
notes?: string;
createdBy: string; // username/nickname of the person who performed the workout
createdAt?: Date;
updatedAt?: Date;
}
const CompletedSetSchema = new mongoose.Schema({
reps: {
type: Number,
required: true,
min: 0,
max: 1000
},
weight: {
type: Number,
min: 0,
max: 1000 // kg
},
rpe: {
type: Number,
min: 1,
max: 10
},
completed: {
type: Boolean,
default: false
},
notes: {
type: String,
trim: true,
maxlength: 200
}
});
const CompletedExerciseSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
sets: {
type: [CompletedSetSchema],
required: true
},
restTime: {
type: Number,
default: 120, // 2 minutes in seconds
min: 10,
max: 600 // max 10 minutes rest
},
notes: {
type: String,
trim: true,
maxlength: 500
}
});
const WorkoutSessionSchema = new mongoose.Schema(
{
templateId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'WorkoutTemplate'
},
templateName: {
type: String,
trim: true
},
name: {
type: String,
required: true,
trim: true,
maxlength: 100
},
exercises: {
type: [CompletedExerciseSchema],
required: true,
validate: {
validator: function(exercises: ICompletedExercise[]) {
return exercises.length > 0;
},
message: 'A workout session must have at least one exercise'
}
},
startTime: {
type: Date,
required: true,
default: Date.now
},
endTime: {
type: Date
},
duration: {
type: Number, // in minutes
min: 0
},
notes: {
type: String,
trim: true,
maxlength: 1000
},
createdBy: {
type: String,
required: true,
trim: true
}
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);
WorkoutSessionSchema.index({ createdBy: 1, startTime: -1 });
WorkoutSessionSchema.index({ templateId: 1 });
export const WorkoutSession = mongoose.model<IWorkoutSession>("WorkoutSession", WorkoutSessionSchema);

View File

@@ -0,0 +1,112 @@
import mongoose from 'mongoose';
export interface ISet {
reps: number;
weight?: number;
rpe?: number; // Rate of Perceived Exertion (1-10)
}
export interface IExercise {
name: string;
sets: ISet[];
restTime?: number; // Rest time in seconds, defaults to 120 (2 minutes)
}
export interface IWorkoutTemplate {
_id?: string;
name: string;
description?: string;
exercises: IExercise[];
createdBy: string; // username/nickname of the person who created the template
isPublic?: boolean; // whether other users can see/use this template
createdAt?: Date;
updatedAt?: Date;
}
const SetSchema = new mongoose.Schema({
reps: {
type: Number,
required: true,
min: 1,
max: 1000
},
weight: {
type: Number,
min: 0,
max: 1000 // kg
},
rpe: {
type: Number,
min: 1,
max: 10
}
});
const ExerciseSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
sets: {
type: [SetSchema],
required: true,
validate: {
validator: function(sets: ISet[]) {
return sets.length > 0;
},
message: 'An exercise must have at least one set'
}
},
restTime: {
type: Number,
default: 120, // 2 minutes in seconds
min: 10,
max: 600 // max 10 minutes rest
}
});
const WorkoutTemplateSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
maxlength: 100
},
description: {
type: String,
trim: true,
maxlength: 500
},
exercises: {
type: [ExerciseSchema],
required: true,
validate: {
validator: function(exercises: IExercise[]) {
return exercises.length > 0;
},
message: 'A workout template must have at least one exercise'
}
},
createdBy: {
type: String,
required: true,
trim: true
},
isPublic: {
type: Boolean,
default: false
}
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);
WorkoutTemplateSchema.index({ createdBy: 1 });
WorkoutTemplateSchema.index({ name: 1, createdBy: 1 });
export const WorkoutTemplate = mongoose.model<IWorkoutTemplate>("WorkoutTemplate", WorkoutTemplateSchema);