NAS deployment fixed

This commit is contained in:
aodulov
2025-12-18 13:03:12 +02:00
parent 97b4e5de32
commit e9aec9a65d
14 changed files with 278 additions and 76 deletions

View File

@@ -68,20 +68,30 @@ export class AuthService {
});
// Seed default exercises
// Seed default exercises
await this.seedDefaultExercises(user.id);
const token = jwt.sign({ userId: user.id, role: user.role }, JWT_SECRET);
const { password: _, ...userSafe } = user;
return { user: userSafe, token };
}
static async seedDefaultExercises(userId: string) {
try {
// Ensure env is loaded (in case server didn't restart)
// Ensure env is loaded from root (in case server didn't restart)
if (!process.env.DEFAULT_EXERCISES_CSV_PATH) {
dotenv.config({ path: path.resolve(process.cwd(), '.env'), override: true });
if (!process.env.DEFAULT_EXERCISES_CSV_PATH) {
// Try root if CWD is server
dotenv.config({ path: path.resolve(process.cwd(), '../.env'), override: true });
const rootEnv = path.resolve(process.cwd(), '../.env');
if (fs.existsSync(rootEnv)) {
dotenv.config({ path: rootEnv, override: true });
} else {
dotenv.config({ path: path.resolve(process.cwd(), '.env'), override: true });
}
}
const csvPath = process.env.DEFAULT_EXERCISES_CSV_PATH;
if (csvPath) {
// ... logic continues
let resolvedPath = path.resolve(process.cwd(), csvPath);
// Try to handle if resolvedPath doesn't exist but relative to root does (if CWD is server)
@@ -109,7 +119,7 @@ export class AuthService {
if (row.name && row.type) {
exercisesToCreate.push({
userId: user.id,
userId,
name: row.name,
type: row.type,
bodyWeightPercentage: row.bodyWeightPercentage ? parseFloat(row.bodyWeightPercentage) : 0,
@@ -120,9 +130,16 @@ export class AuthService {
}
if (exercisesToCreate.length > 0) {
await prisma.exercise.createMany({
data: exercisesToCreate
});
// Check if exercises already exist for this user to avoid duplicates
const existingCount = await prisma.exercise.count({ where: { userId } });
if (existingCount === 0) {
await prisma.exercise.createMany({
data: exercisesToCreate
});
console.log(`[AuthService] Seeded ${exercisesToCreate.length} exercises for user: ${userId}`);
} else {
console.log(`[AuthService] User ${userId} already has ${existingCount} exercises. Skipping seed.`);
}
}
}
} else {
@@ -134,11 +151,6 @@ export class AuthService {
console.error('[AuthService] Failed to seed default exercises:', error);
// Non-blocking error
}
const token = jwt.sign({ userId: user.id, role: user.role }, JWT_SECRET);
const { password: _, ...userSafe } = user;
return { user: userSafe, token };
}
static async changePassword(userId: string, newPassword: string) {