@@ -2,6 +2,7 @@ const express = require("express");
22const router = express . Router ( ) ;
33const fs = require ( "fs" ) ;
44const path = require ( "path" ) ;
5+ const logger = require ( "../logger" ) ;
56
67const dbPath = path . join ( __dirname , ".." , "database.json" ) ;
78
@@ -10,6 +11,7 @@ function readDb() {
1011 // Si le fichier n'existe pas, on le crée avec une structure vide
1112 if ( ! fs . existsSync ( dbPath ) ) {
1213 fs . writeFileSync ( dbPath , JSON . stringify ( { users : [ ] } , null , 2 ) ) ;
14+ logger . info ( "database.json created as it did not exist." ) ;
1315 }
1416
1517 try {
@@ -25,8 +27,8 @@ function readDb() {
2527 }
2628 return db ;
2729 } catch ( error ) {
28- console . error (
29- "Erreur critique lors de la lecture ou du parsing de database.json:" ,
30+ logger . error (
31+ "Critical error while reading or parsing database.json:" ,
3032 error
3133 ) ;
3234 // En cas d'erreur de parsing, on retourne une structure vide pour éviter un crash
@@ -42,8 +44,16 @@ function writeDb(data) {
4244// Cette route répondra à POST /link/
4345router . post ( "/" , ( req , res ) => {
4446 const { discord_id, coachfoot_id } = req . body ;
47+ logger . info (
48+ `Received validation request for discord_id: ${ discord_id } and coachfoot_id: ${ coachfoot_id } `
49+ ) ;
4550
4651 if ( ! discord_id || ! coachfoot_id ) {
52+ logger . warn (
53+ `Validation request rejected: missing discord_id or coachfoot_id. Body: ${ JSON . stringify (
54+ req . body
55+ ) } `
56+ ) ;
4757 return res
4858 . status ( 400 )
4959 . json ( { error : "discord_id et coachfoot_id sont requis." } ) ;
@@ -58,6 +68,9 @@ router.post("/", (req, res) => {
5868 ) ;
5969
6070 if ( existingCoachFootUser ) {
71+ logger . warn (
72+ `Validation rejected: coachfoot_id ${ coachfoot_id } already linked to another Discord account (${ existingCoachFootUser . discord_id } ).`
73+ ) ;
6174 return res . status ( 409 ) . json ( {
6275 error : "Ce compte CoachFoot est déjà lié à un autre compte Discord." ,
6376 } ) ;
@@ -66,10 +79,16 @@ router.post("/", (req, res) => {
6679 const userIndex = db . users . findIndex ( ( u ) => u . discord_id === discord_id ) ;
6780
6881 if ( userIndex === - 1 ) {
82+ logger . warn (
83+ `Validation rejected: User with discord_id ${ discord_id } not found in database.`
84+ ) ;
6985 return res . status ( 404 ) . json ( { error : "Utilisateur non trouvé." } ) ;
7086 }
7187
7288 if ( db . users [ userIndex ] . status !== "waiting" ) {
89+ logger . warn (
90+ `Validation rejected: User ${ discord_id } status is '${ db . users [ userIndex ] . status } ', not 'waiting'.`
91+ ) ;
7392 return res . status ( 409 ) . json ( {
7493 error : "Le statut de l'utilisateur n'est pas en attente de validation." ,
7594 } ) ;
@@ -81,9 +100,15 @@ router.post("/", (req, res) => {
81100
82101 writeDb ( db ) ;
83102
103+ logger . info (
104+ `Successfully validated and linked discord_id ${ discord_id } to coachfoot_id ${ coachfoot_id } .`
105+ ) ;
84106 res . status ( 201 ) . json ( { status : "validated" } ) ;
85107 } catch ( error ) {
86- console . error ( "Erreur lors de la validation:" , error ) ;
108+ logger . error (
109+ `Error during validation for discord_id ${ discord_id } :` ,
110+ error
111+ ) ;
87112 res . status ( 500 ) . json ( { error : "Erreur interne du serveur." } ) ;
88113 }
89114} ) ;
0 commit comments