Files
THE-TIP-TOP/frontend/src/services/admin.js

74 lines
2.3 KiB
JavaScript

const API_URL = import.meta.env.VITE_API_URL || ''
const BASE = `${API_URL}/api/admin`
function authHeaders() {
return {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('token')}`,
}
}
function apiError(message, status) {
const err = new Error(message)
err.status = status
return err
}
export async function getStatsApi() {
const res = await fetch(`${BASE}/stats`, { headers: authHeaders() })
const data = await res.json()
if (!res.ok) throw apiError(data.message || 'Erreur stats.', res.status)
return data
}
export async function getUsersApi() {
const res = await fetch(`${BASE}/users`, { headers: authHeaders() })
const data = await res.json()
if (!res.ok) throw apiError(data.message || 'Erreur utilisateurs.', res.status)
return data
}
export async function drawWinnerApi() {
const res = await fetch(`${BASE}/draw`, { method: 'POST', headers: authHeaders() })
const data = await res.json()
if (!res.ok) throw apiError(data.message || 'Erreur tirage.', res.status)
return data
}
export async function getEmailingContactsApi() {
const res = await fetch(`${BASE}/emailing`, { headers: authHeaders() })
const data = await res.json()
if (!res.ok) throw apiError(data.message || 'Erreur contacts emailing.', res.status)
return data
}
export async function setGameSettingsApi(startDate) {
const res = await fetch(`${BASE}/game-settings`, {
method: 'PUT',
headers: authHeaders(),
body: JSON.stringify({ startDate }),
})
const data = await res.json()
if (!res.ok) throw apiError(data.message || 'Erreur configuration du jeu.', res.status)
return data
}
export async function getWinnerApi() {
const res = await fetch(`${BASE}/draw`, { headers: authHeaders() })
if (res.status === 404) return null
const data = await res.json()
if (!res.ok) throw apiError(data.message || 'Erreur gagnant.', res.status)
return data
}
export async function createEmployeeApi({ firstname, lastname, email, password }) {
const res = await fetch(`${BASE}/employees`, {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({ firstname, lastname, email, password }),
})
const data = await res.json()
if (!res.ok) throw apiError(data.message || "Erreur lors de la création de l'employé.", res.status)
return data
}