diff --git a/src/image/image.controller.ts b/src/image/image.controller.ts index 76f685f..aeb5218 100644 --- a/src/image/image.controller.ts +++ b/src/image/image.controller.ts @@ -194,31 +194,27 @@ export class ImageController { * DELETE /merchants/:merchantId/logos/:fileName * Supprime un logo */ - @Delete(':fileName') + @Delete('merchants/:merchantId/logos/url') async deleteLogo( @Param('merchantId') merchantId: string, - @Param('fileName') fileName: string, - @Body() body: { merchantName?: string }, - @Request() req + @Query('fileName') fileName: string ) { - const userId = req.user?.sub || req.user?.userId; - - if (!userId) { - throw new BadRequestException('Utilisateur non identifié'); + if (!fileName) { + throw new BadRequestException('fileName requis'); } try { - const merchantName = body.merchantName; - + // Sécurité minimale : vérifier que le fichier appartient bien au merchant + if (!fileName.startsWith(`merchants/${merchantId}_`)) { + throw new BadRequestException('Accès non autorisé à ce fichier'); + } + await this.imageService.deleteMerchantLogo(fileName); return { success: true, message: 'Logo supprimé avec succès', - merchant: { - id: merchantId, - name: merchantName - }, + merchantId: merchantId, deletedFile: fileName }; diff --git a/src/merchant/services/user.service.client.ts b/src/merchant/services/user.service.client.ts index 0daa651..d4a8d01 100644 --- a/src/merchant/services/user.service.client.ts +++ b/src/merchant/services/user.service.client.ts @@ -78,25 +78,67 @@ export class HttpUserServiceClient implements UserServiceClient { 'Content-Type': 'application/json', }; } - + async verifyUserExists(userId: string): Promise { try { + console.log(`🔍 [verifyUserExists] Vérification de l'utilisateur: ${userId}`); + console.log(` Type de userId: ${typeof userId}`); + console.log(` Valeur de userId: "${userId}"`); + const headers = await this.getAuthHeaders(); + + const url = `${this.baseUrl}/merchant-users/${userId}`; + const response = await firstValueFrom( - this.httpService.get(`${this.baseUrl}/users/${userId}/exists`, { headers }), + this.httpService.get(url, { headers }), ); - return response.data.exists === true; - } catch (error) { - if (error.response?.status === 404) { + + console.log(`✅ [verifyUserExists] Réponse complète:`, JSON.stringify(response.data, null, 2)); + + // Vérifier si on a reçu une réponse valide + if (!response.data) { + console.log(` ❌ Aucune donnée dans la réponse`); return false; } + + // L'utilisateur existe si on a reçu une réponse 200 avec des données + const exists = response.data && response.data.id === userId; + console.log(` Résultat: ${exists ? '✅ Utilisateur existe' : '❌ Utilisateur non trouvé'}`); + + return exists; + + } catch (error) { + console.error(`❌ [verifyUserExists] Erreur détaillée:`, { + name: error.name, + message: error.message, + status: error.response?.status, + statusText: error.response?.statusText, + data: error.response?.data, + code: error.code + }); + + if (error.response?.status === 404) { + console.log(` 📭 Utilisateur ${userId} non trouvé (404)`); + return false; + } + if (error.response?.status === 401) { - // Token invalide, réessayer une fois après rafraîchissement + console.log(` 🔄 Token invalide (401), rafraîchissement...`); this.accessToken = null; + await new Promise(resolve => setTimeout(resolve, 1000)); return this.verifyUserExists(userId); } + + // Autres erreurs HTTP + if (error.response?.status) { + console.log(` ⚠️ Erreur HTTP ${error.response.status}: ${error.response.statusText}`); + return false; + } + + // Erreur réseau ou autre + console.log(` 🚨 Erreur non-HTTP: ${error.message}`); throw new HttpException( - 'Failed to verify user existence', + `Failed to verify user existence: ${error.message}`, HttpStatus.SERVICE_UNAVAILABLE, ); }