Bug fixes and refactoring

This commit is contained in:
AG
2025-12-06 16:14:28 +02:00
parent 4106f3b783
commit 890f4f0958
13 changed files with 188 additions and 214 deletions

View File

@@ -1,26 +0,0 @@
import { PrismaClient } from '@prisma/client';
import fs from 'fs';
import path from 'path';
const prisma = new PrismaClient();
async function backup() {
try {
console.log('Starting backup...');
// Backup Quick Log sessions and their sets (formerly SporadicSets)
const quickLogSessions = await prisma.workoutSession.findMany({
where: { type: 'QUICK_LOG' },
include: { sets: true }
});
const backupPath = path.join(__dirname, '../../sporadic_backup.json');
fs.writeFileSync(backupPath, JSON.stringify(quickLogSessions, null, 2));
console.log(`Backed up ${quickLogSessions.length} quick log sessions to ${backupPath}`);
} catch (error) {
console.error('Backup failed:', error);
} finally {
await prisma.$disconnect();
}
}
backup();

View File

@@ -1,42 +0,0 @@
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function migrate() {
console.log('Starting migration...');
const plans = await prisma.workoutPlan.findMany();
console.log(`Found ${plans.length} plans.`);
for (const plan of plans) {
if (plan.exercises) {
try {
const steps = JSON.parse(plan.exercises);
console.log(`Migrating plan ${plan.name} (${plan.id}) with ${steps.length} steps.`);
let order = 0;
for (const step of steps) {
await prisma.planExercise.create({
data: {
planId: plan.id,
exerciseId: step.exerciseId,
order: order++,
isWeighted: step.isWeighted || false
}
});
}
} catch (e) {
console.error(`Error parsing JSON for plan ${plan.id}:`, e);
}
}
}
console.log('Migration complete.');
}
migrate()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});

View File

@@ -1,77 +0,0 @@
import { PrismaClient } from '@prisma/client';
import fs from 'fs';
import path from 'path';
const prisma = new PrismaClient();
async function restore() {
try {
const backupPath = path.join(__dirname, '../../sporadic_backup.json');
if (!fs.existsSync(backupPath)) {
console.error('Backup file not found!');
return;
}
const sporadicSets = JSON.parse(fs.readFileSync(backupPath, 'utf-8'));
console.log(`Found ${sporadicSets.length} sporadic sets to restore.`);
for (const set of sporadicSets) {
const date = new Date(set.timestamp);
const startOfDay = new Date(date);
startOfDay.setHours(0, 0, 0, 0);
const endOfDay = new Date(date);
endOfDay.setHours(23, 59, 59, 999);
// Find or create a QUICK_LOG session for this day
let session = await prisma.workoutSession.findFirst({
where: {
userId: set.userId,
type: 'QUICK_LOG',
startTime: {
gte: startOfDay,
lte: endOfDay
}
}
});
if (!session) {
session = await prisma.workoutSession.create({
data: {
userId: set.userId,
startTime: startOfDay, // Use start of day as session start
type: 'QUICK_LOG',
note: 'Daily Quick Log'
}
});
console.log(`Created new QUICK_LOG session for ${startOfDay.toISOString()}`);
}
// Create the WorkoutSet
await prisma.workoutSet.create({
data: {
sessionId: session.id,
exerciseId: set.exerciseId,
order: 0, // Order doesn't matter much for sporadic sets, or we could increment
weight: set.weight,
reps: set.reps,
distanceMeters: set.distanceMeters,
durationSeconds: set.durationSeconds,
height: set.height,
bodyWeightPercentage: set.bodyWeightPercentage,
side: set.side,
timestamp: new Date(set.timestamp),
completed: true
}
});
}
console.log('Restoration complete!');
} catch (error) {
console.error('Restoration failed:', error);
} finally {
await prisma.$disconnect();
}
}
restore();