188 lines
7.5 KiB
TypeScript
188 lines
7.5 KiB
TypeScript
import 'dotenv/config';
|
|
import bcrypt from 'bcryptjs';
|
|
import { Prisma, PrismaClient } from '@prisma/client';
|
|
import { EXERCISES, SEED_VERSION } from './data/exercises.js';
|
|
import type { EquipmentKind, EquipmentSpec } from './lib/equipmentSpec.js';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
/**
|
|
* Default focus areas — one per muscle group present in the exercise catalog.
|
|
* The admin can rename/describe them; the muscle key is stable and links to Exercise.primaryMuscle.
|
|
* setsMin/setsMax: recommended weekly volume range in sets (decision 13 — volume warning).
|
|
*/
|
|
const FOCUS_AREAS: Array<{ name: string; muscle: string; description: string; setsMin: number; setsMax: number }> = [
|
|
{ name: 'Klatka piersiowa', muscle: 'chest', description: 'Wyciskanie, rozpiętki, pompki.', setsMin: 10, setsMax: 20 },
|
|
{ name: 'Plecy', muscle: 'back', description: 'Podciąganie, wiosłowanie, uginanie.', setsMin: 10, setsMax: 20 },
|
|
{ name: 'Nogi', muscle: 'legs', description: 'Przysiady, wykroki, prostowanie nóg.', setsMin: 10, setsMax: 18 },
|
|
{ name: 'Pośladki', muscle: 'glutes', description: 'Hip thrust, mostki, wykroki.', setsMin: 8, setsMax: 16 },
|
|
{ name: 'Barki', muscle: 'shoulders', description: 'Wyciskanie nad głowę, unoszenia.', setsMin: 8, setsMax: 16 },
|
|
{ name: 'Biceps', muscle: 'biceps', description: 'Uginanie, podciąganie.', setsMin: 6, setsMax: 12 },
|
|
{ name: 'Triceps', muscle: 'triceps', description: 'Osiadanie, wyciskanie francuskie.', setsMin: 6, setsMax: 12 },
|
|
{ name: 'Brzuch / core', muscle: 'core', description: 'Plank, unoszenia nóg, stabilizacja.', setsMin: 8, setsMax: 16 },
|
|
];
|
|
|
|
/**
|
|
* Equipment (machine/device) catalog — slug → Polish display name.
|
|
* The slug is the stable key used by the exercise seed data; the name is what
|
|
* admins and gym owners see and pick from.
|
|
* `kind` determines the shape of the type-specific `spec` payload:
|
|
* DUMBBELL/KETTLEBELL/CABLE/MACHINE → { minKg, maxKg } (available load range)
|
|
* BARBELL/BENCH → { maxLoadKg } (max total load)
|
|
* CARDIO → { maxSpeedKmh?, maxInclinePct? }
|
|
* BODYWEIGHT/OTHER → no spec
|
|
*/
|
|
const EQUIPMENT: Array<{
|
|
slug: string;
|
|
name: string;
|
|
kind: EquipmentKind;
|
|
spec: EquipmentSpec | null;
|
|
description: string;
|
|
}> = [
|
|
{ slug: 'barbell', name: 'Sztanga', kind: 'BARBELL', spec: { maxLoadKg: 100 }, description: 'Sztanga z talerzami.' },
|
|
{ slug: 'dumbbells', name: 'Hantle', kind: 'DUMBBELL', spec: { minKg: 2.5, maxKg: 25 }, description: 'Hantle wolne.' },
|
|
{ slug: 'bodyweight', name: 'Masa ciała', kind: 'BODYWEIGHT', spec: null, description: 'Ćwiczenia z masą ciała.' },
|
|
{ slug: 'cable', name: 'Wyciąg', kind: 'CABLE', spec: { minKg: 5, maxKg: 50 }, description: 'Wyciąg linowy.' },
|
|
{ slug: 'dip-bars', name: 'Drążek do dipów', kind: 'BODYWEIGHT', spec: null, description: 'Stojak do dipów.' },
|
|
{ slug: 'pull-up-bar', name: 'Drążek do podciągania', kind: 'BODYWEIGHT', spec: null, description: 'Drążek do podciągania.' },
|
|
{ slug: 'leg-press', name: 'Leg press', kind: 'MACHINE', spec: { minKg: 20, maxKg: 120 }, description: 'Maszyna do prostowania nóg.' },
|
|
{ slug: 'leg-curl', name: 'Leg curl', kind: 'MACHINE', spec: { minKg: 10, maxKg: 60 }, description: 'Maszyna do uginania nóg.' },
|
|
];
|
|
|
|
/**
|
|
* Idempotent seed of the equipment catalog — upsert by name.
|
|
*/
|
|
async function seedEquipment() {
|
|
for (const eq of EQUIPMENT) {
|
|
const spec = eq.spec === null ? Prisma.JsonNull : (eq.spec as Prisma.InputJsonValue);
|
|
await prisma.equipment.upsert({
|
|
where: { name: eq.name },
|
|
update: { description: eq.description, kind: eq.kind, spec },
|
|
create: { name: eq.name, description: eq.description, kind: eq.kind, spec },
|
|
});
|
|
}
|
|
console.log(`Seeded ${EQUIPMENT.length} equipment items`);
|
|
}
|
|
|
|
/** Resolve a list of equipment slugs to equipment ids (creating any missing). */
|
|
async function resolveEquipmentIds(slugs: string[]): Promise<string[]> {
|
|
const ids: string[] = [];
|
|
for (const slug of slugs) {
|
|
const known = EQUIPMENT.find((e) => e.slug === slug);
|
|
const name = known?.name ?? slug;
|
|
const eq = await prisma.equipment.upsert({
|
|
where: { name },
|
|
update: {},
|
|
create: { name },
|
|
});
|
|
ids.push(eq.id);
|
|
}
|
|
return ids;
|
|
}
|
|
|
|
/**
|
|
* Idempotent seed (upsert by slug + seed_version).
|
|
* Re-running updates the built-in pool.
|
|
*/
|
|
async function seedExercises() {
|
|
for (const ex of EXERCISES) {
|
|
const equipmentIds = await resolveEquipmentIds(ex.equipment);
|
|
await prisma.exercise.upsert({
|
|
where: { slug: ex.slug },
|
|
update: {
|
|
name: ex.name,
|
|
primaryMuscle: ex.primaryMuscle,
|
|
secondaryMuscles: ex.secondaryMuscles,
|
|
equipment: { set: equipmentIds.map((id) => ({ id })) },
|
|
hasReps: ex.hasReps,
|
|
hasLoad: ex.hasLoad,
|
|
hasRest: ex.hasRest,
|
|
hasSets: ex.hasSets,
|
|
hasRir: ex.hasRir,
|
|
hasDifficulty: ex.hasDifficulty,
|
|
hasCardio: ex.hasCardio,
|
|
hasTime: ex.hasTime,
|
|
hasSpeed: ex.hasSpeed,
|
|
description: ex.description,
|
|
howTo: ex.howTo,
|
|
commonMistakes: ex.commonMistakes,
|
|
difficulty: ex.difficulty,
|
|
source: 'BUILTIN',
|
|
seedVersion: SEED_VERSION,
|
|
},
|
|
create: {
|
|
slug: ex.slug,
|
|
name: ex.name,
|
|
primaryMuscle: ex.primaryMuscle,
|
|
secondaryMuscles: ex.secondaryMuscles,
|
|
equipment: { connect: equipmentIds.map((id) => ({ id })) },
|
|
hasReps: ex.hasReps,
|
|
hasLoad: ex.hasLoad,
|
|
hasRest: ex.hasRest,
|
|
hasSets: ex.hasSets,
|
|
hasRir: ex.hasRir,
|
|
hasDifficulty: ex.hasDifficulty,
|
|
hasCardio: ex.hasCardio,
|
|
hasTime: ex.hasTime,
|
|
hasSpeed: ex.hasSpeed,
|
|
description: ex.description,
|
|
howTo: ex.howTo,
|
|
commonMistakes: ex.commonMistakes,
|
|
difficulty: ex.difficulty,
|
|
source: 'BUILTIN',
|
|
seedVersion: SEED_VERSION,
|
|
},
|
|
});
|
|
}
|
|
console.log(`Seeded ${EXERCISES.length} exercises (seed_version=${SEED_VERSION})`);
|
|
}
|
|
|
|
/**
|
|
* Idempotent seed of the default focus areas — upsert by muscle key.
|
|
*/
|
|
async function seedFocusAreas() {
|
|
for (const fa of FOCUS_AREAS) {
|
|
await prisma.focusArea.upsert({
|
|
where: { muscle: fa.muscle },
|
|
update: { name: fa.name, description: fa.description, setsMin: fa.setsMin, setsMax: fa.setsMax },
|
|
create: { name: fa.name, muscle: fa.muscle, description: fa.description, setsMin: fa.setsMin, setsMax: fa.setsMax },
|
|
});
|
|
}
|
|
console.log(`Seeded ${FOCUS_AREAS.length} focus areas`);
|
|
}
|
|
|
|
/**
|
|
* First admin from env vars (FITOS_FIRST_ADMIN_EMAIL / FITOS_FIRST_ADMIN_PASSWORD).
|
|
*/
|
|
async function seedFirstAdmin() {
|
|
const email = process.env.FITOS_FIRST_ADMIN_EMAIL;
|
|
if (!email) {
|
|
console.log('FITOS_FIRST_ADMIN_EMAIL not set — skipping first admin seed');
|
|
return;
|
|
}
|
|
const password = process.env.FITOS_FIRST_ADMIN_PASSWORD || 'ChangeMe123!';
|
|
const existing = await prisma.user.findUnique({ where: { email } });
|
|
if (existing) return;
|
|
const passwordHash = await bcrypt.hash(password, 10);
|
|
await prisma.user.create({
|
|
data: { email, passwordHash, isAdmin: true, firstName: 'Admin' },
|
|
});
|
|
console.log(`Created first admin: ${email} (password from FITOS_FIRST_ADMIN_PASSWORD)`);
|
|
}
|
|
|
|
async function main() {
|
|
await seedEquipment();
|
|
await seedExercises();
|
|
await seedFocusAreas();
|
|
await seedFirstAdmin();
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error('Seed failed:', e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|