Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 1x 12x 12x 12x 2x 2x 2x 1x 1x 1x 8x 6x 6x 2x 4x 4x 1x 3x 3x 1x 2x 1x 1x 2x 2x 2x 2x 2x 1x 1x 2x 1x | import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, map, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { environment } from '../config/environment';
import { AuthService } from './auth.service';
import { Win } from '../models';
@Injectable({
providedIn: 'root'
})
export class GameService {
private readonly API_URL = `${environment.apiUrl}/game`;
constructor(
private http: HttpClient,
private authService: AuthService
) {}
/**
* Participer au jeu concours avec un code
*/
participate(codeValue: string): Observable<{ message: string }> {
const params = new HttpParams().set('codeValue', codeValue);
// L'intercepteur ajoute déjà le token automatiquement
// On n'a pas besoin de l'ajouter manuellement ici
// Mais on vérifie quand même qu'il existe
const token = this.authService.getToken();
if (!token) {
return throwError(() => new Error('Token manquant. Veuillez vous reconnecter.'));
}
return this.http.post(this.API_URL + '/participate', null, {
params,
// Ne pas ajouter de headers manuellement, l'intercepteur s'en charge
responseType: 'text'
}).pipe(
map((message: string) => ({ message }))
);
}
/**
* Vérifier le statut d'un code
*/
getCodeStatus(codeValue: string): Observable<{ status: string; message: string; prizeType?: string }> {
// Le backend peut retourner du texte brut ou du JSON
return this.http.get(
`${this.API_URL}/status/${codeValue}`,
{
headers: this.authService.getAuthHeaders(),
responseType: 'text' // Accepter le texte brut
}
).pipe(
map((response: string) => {
// Essayer de parser comme JSON d'abord
try {
const jsonResponse = JSON.parse(response);
return {
status: jsonResponse.status || 'valid',
message: jsonResponse.message || response,
prizeType: jsonResponse.prizeType
};
} catch {
// Si ce n'est pas du JSON, analyser le texte brut
const lowerResponse = response.toLowerCase();
if (lowerResponse.includes('valid') || lowerResponse.includes('valide')) {
return {
status: 'valid',
message: response,
prizeType: undefined
};
} else Iif (lowerResponse.includes('invalid') || lowerResponse.includes('invalide')) {
return {
status: 'invalid',
message: response,
prizeType: undefined
};
} else if (lowerResponse.includes('used') || lowerResponse.includes('utilisé')) {
return {
status: 'used',
message: response,
prizeType: undefined
};
} else if (lowerResponse.includes('expired') || lowerResponse.includes('expiré')) {
return {
status: 'expired',
message: response,
prizeType: undefined
};
} else {
// Par défaut, considérer comme invalide
return {
status: 'invalid',
message: response || 'Code invalide',
prizeType: undefined
};
}
}
}),
catchError((error) => {
// Gérer les erreurs HTTP
let errorMessage = 'Code invalide ou inexistant';
if (error.error) {
if (typeof error.error === 'string') {
try {
const parsed = JSON.parse(error.error);
errorMessage = parsed.message || parsed.error || error.error;
} catch {
errorMessage = error.error;
}
} else Eif (error.error.message) {
errorMessage = error.error.message;
} else Iif (error.error.error) {
errorMessage = error.error.error;
}
}
return throwError(() => ({
...error,
error: { message: errorMessage, error: 'Code invalide ou inexistant' }
}));
})
);
}
/**
* Obtenir l'historique des participations de l'utilisateur connecté
*/
getMyHistory(): Observable<any[]> {
// L'API retourne un tableau avec id, code, prizeName, dateParticipation
return this.http.get<any[]>(`${this.API_URL}/history`, {
headers: this.authService.getAuthHeaders()
});
}
}
|