From 0fe0514660763604cf743da99ae74cc4cdd6c54e Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 1 Oct 2025 13:24:16 +0200 Subject: [PATCH] Verbessert die Protokollierung in den Club-Controller- und Benutzer-Utils-Dateien, indem die Konsolenausgaben durch eine bedingte Entwicklungsprotokollierungsfunktion ersetzt werden. Aktualisiert die Fehlerbehandlung, um detailliertere Fehlermeldungen auszugeben. --- backend/controllers/clubsController.js | 52 +++++++++++++------------- backend/utils/userUtils.js | 14 ++++--- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/backend/controllers/clubsController.js b/backend/controllers/clubsController.js index e7bef76..8248271 100644 --- a/backend/controllers/clubsController.js +++ b/backend/controllers/clubsController.js @@ -1,77 +1,78 @@ import ClubService from '../services/clubService.js'; import { getUserByToken } from '../utils/userUtils.js'; +const isDev = process.env.STAGE === 'dev'; +const devLog = (...args) => isDev && console.log(...args); + export const getClubs = async (req, res) => { try { - console.log('[getClubs] - get clubs'); + devLog('[getClubs] - get clubs'); const clubs = await ClubService.getAllClubs(); - console.log('[getClubs] - prepare response'); + devLog('[getClubs] - prepare response'); res.status(200).json(clubs); - console.log('[getClubs] - done'); + devLog('[getClubs] - done'); } catch (error) { - console.log('[getClubs] - error'); - console.log(error); + console.error('[getClubs] - error:', error); res.status(500).json({ error: "internalerror" }); } }; export const addClub = async (req, res) => { - console.log('[addClub] - Read out parameters'); + devLog('[addClub] - Read out parameters'); const { authcode: token } = req.headers; const { name: clubName } = req.body; try { - console.log('[addClub] - find club by name'); + devLog('[addClub] - find club by name'); const club = await ClubService.findClubByName(clubName); - console.log('[addClub] - get user'); + devLog('[addClub] - get user'); const user = await getUserByToken(token); - console.log('[addClub] - check if club already exists'); + devLog('[addClub] - check if club already exists'); if (club) { res.status(409).json({ error: "alreadyexists" }); return; } - console.log('[addClub] - create club'); + devLog('[addClub] - create club'); const newClub = await ClubService.createClub(clubName); - console.log('[addClub] - add user to new club'); + devLog('[addClub] - add user to new club'); await ClubService.addUserToClub(user.id, newClub.id); - console.log('[addClub] - prepare response'); + devLog('[addClub] - prepare response'); res.status(200).json(newClub); - console.log('[addClub] - done'); + devLog('[addClub] - done'); } catch (error) { - console.log('[addClub] - error'); - console.log(error); + console.error('[addClub] - error:', error); res.status(500).json({ error: "internalerror" }); } }; export const getClub = async (req, res) => { - console.log('[getClub] - start'); + devLog('[getClub] - start'); try { const { authcode: token } = req.headers; const { clubid: clubId } = req.params; - console.log('[getClub] - get user'); + devLog('[getClub] - get user'); const user = await getUserByToken(token); - console.log('[getClub] - get users club'); + devLog('[getClub] - get users club'); const access = await ClubService.getUserClubAccess(user.id, clubId); - console.log('[getClub] - check access'); + devLog('[getClub] - check access'); if (access.length === 0 || !access[0].approved) { res.status(403).json({ error: "noaccess", status: access.length === 0 ? "notrequested" : "requested" }); return; } - console.log('[getClub] - get club'); + devLog('[getClub] - get club'); const club = await ClubService.findClubById(clubId); - console.log('[getClub] - check club exists'); + devLog('[getClub] - check club exists'); if (!club) { return res.status(404).json({ message: 'Club not found' }); } - console.log('[getClub] - set response'); + devLog('[getClub] - set response'); res.status(200).json(club); - console.log('[getClub] - done'); + devLog('[getClub] - done'); } catch (error) { - console.log(error); + console.error('[getClub] - error:', error); res.status(500).json({ message: 'Server error' }); } }; @@ -82,7 +83,7 @@ export const requestClubAccess = async (req, res) => { try { const user = await getUserByToken(token); - console.log(user); + devLog('[requestClubAccess] - user:', user); await ClubService.requestAccessToClub(user.id, clubId); res.status(200).json({}); @@ -92,6 +93,7 @@ export const requestClubAccess = async (req, res) => { } else if (error.message === 'clubnotfound') { res.status(404).json({ err: "clubnotfound" }); } else { + console.error('[requestClubAccess] - error:', error); res.status(500).json({ err: "internalerror" }); } } diff --git a/backend/utils/userUtils.js b/backend/utils/userUtils.js index 3980280..ea1f3aa 100644 --- a/backend/utils/userUtils.js +++ b/backend/utils/userUtils.js @@ -7,6 +7,9 @@ import HttpError from '../exceptions/HttpError.js'; import { config } from 'dotenv'; config(); // sorgt dafür, dass process.env.JWT_SECRET geladen wird +const isDev = process.env.STAGE === 'dev'; +const devLog = (...args) => isDev && console.log(...args); + export const getUserByToken = async (token) => { try { // 1. JWT validieren @@ -41,7 +44,7 @@ export const getUserByToken = async (token) => { export const hasUserClubAccess = async (userId, clubId) => { try { - console.log('[hasUserClubAccess]'); + devLog('[hasUserClubAccess]'); const userClub = await UserClub.findOne({ where: { user_id: userId, @@ -51,10 +54,9 @@ export const hasUserClubAccess = async (userId, clubId) => { }); return userClub !== null; } catch (error) { - console.log(error); + console.error('[hasUserClubAccess] - error:', error); throw new HttpError('notfound', 500); } - console.log('---- no user found'); } export const checkAccess = async (userToken, clubId) => { @@ -62,11 +64,11 @@ export const checkAccess = async (userToken, clubId) => { const user = await getUserByToken(userToken); const hasAccess = await hasUserClubAccess(user.id, clubId); if (!hasAccess) { - console.log('no club access'); + devLog('[checkAccess] - no club access'); throw new HttpError('noaccess', 403); } } catch (error) { - console.log(error); + devLog('[checkAccess] - error:', error); throw error; } }; @@ -76,7 +78,7 @@ export const checkGlobalAccess = async (userToken) => { const user = await getUserByToken(userToken); return user; // Einfach den User zurückgeben, da globale Zugriffe nur Authentifizierung benötigen } catch (error) { - console.log(error); + devLog('[checkGlobalAccess] - error:', error); throw error; } };