chore: update
This commit is contained in:
+119
-94
@@ -5,7 +5,7 @@
|
||||
// ✅ loginUser et registerUser retournent AuthResponse
|
||||
// ✅ sessionStorage (pas localStorage)
|
||||
|
||||
const API_URL = "/api/v1";
|
||||
const API_URL = "https://uber-stup.club/api/v1";
|
||||
import type {
|
||||
ConfirmReceptionResponse,
|
||||
CheckoutCartResponse,
|
||||
@@ -36,6 +36,7 @@ export interface AuthResponse {
|
||||
telephone?: string;
|
||||
role?: string;
|
||||
session_id?: string;
|
||||
must_change_password?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -177,11 +178,16 @@ export const loginUser = async (
|
||||
let errorMessage = "Erreur de connexion";
|
||||
try {
|
||||
const errorData = await response.json();
|
||||
errorMessage = errorData.error || errorData.message || errorMessage;
|
||||
errorMessage =
|
||||
errorData.error || errorData.message || errorMessage;
|
||||
} catch {
|
||||
// body vide ou non-JSON
|
||||
}
|
||||
console.error("❌ [LOGIN] Erreur API:", response.status, errorMessage);
|
||||
console.error(
|
||||
"❌ [LOGIN] Erreur API:",
|
||||
response.status,
|
||||
errorMessage,
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
message: errorMessage,
|
||||
@@ -235,97 +241,6 @@ export const loginUser = async (
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ REGISTER - Retourne AuthResponse avec access_token
|
||||
* POST /api/v1/auth/register
|
||||
*/
|
||||
export const registerUser = async (
|
||||
username: string,
|
||||
password: string,
|
||||
nom: string,
|
||||
prenom: string,
|
||||
telephone: string,
|
||||
): Promise<AuthResponse> => {
|
||||
// ✅ Type de retour CORRECT
|
||||
try {
|
||||
console.log("📝 [REGISTER] Appel API...");
|
||||
|
||||
const response = await fetch(`${API_URL}/auth/register`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
password,
|
||||
nom,
|
||||
prenom,
|
||||
telephone,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
let errorMessage = "Erreur d'inscription";
|
||||
try {
|
||||
const errorData = await response.json();
|
||||
errorMessage = errorData.error || errorData.message || errorMessage;
|
||||
} catch {
|
||||
// body vide ou non-JSON
|
||||
}
|
||||
console.error("❌ [REGISTER] Erreur API:", response.status, errorMessage);
|
||||
return {
|
||||
success: false,
|
||||
message: errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log("📋 [REGISTER] Réponse:", data);
|
||||
|
||||
// ✅ Vérifier access_token
|
||||
if (!data.access_token) {
|
||||
console.error("❌ [REGISTER] Pas de access_token");
|
||||
return {
|
||||
success: false,
|
||||
message: "Token non reçu du serveur",
|
||||
};
|
||||
}
|
||||
|
||||
// ✅ Stocker en sessionStorage
|
||||
sessionStorage.setItem("token", data.access_token);
|
||||
console.log("✅ [REGISTER] Token stocké");
|
||||
|
||||
// ✅ Synchroniser username
|
||||
const jwtUsername = syncUsernameFromJWT();
|
||||
if (!jwtUsername) {
|
||||
console.warn("⚠️ [REGISTER] Impossible de synchroniser username");
|
||||
return {
|
||||
success: false,
|
||||
message: "Erreur synchronisation JWT",
|
||||
};
|
||||
}
|
||||
|
||||
console.log(`✅ [REGISTER] Créé et connecté: ${jwtUsername}`);
|
||||
|
||||
// ✅ Retourner avec access_token
|
||||
return {
|
||||
success: true,
|
||||
message: "Inscription réussie",
|
||||
access_token: data.access_token, // ✅ IMPORTANT!
|
||||
token_type: data.token_type,
|
||||
expires_in: data.expires_in,
|
||||
user: data.user,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [REGISTER] Erreur:", error);
|
||||
return {
|
||||
success: false,
|
||||
message:
|
||||
error instanceof Error ? error.message : "Erreur d'inscription",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ LOGOUT
|
||||
*/
|
||||
@@ -351,6 +266,43 @@ export const logoutUser = async (): Promise<void> => {
|
||||
console.log("✅ [LOGOUT] sessionStorage nettoyé");
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ CHANGE PASSWORD
|
||||
* PUT /api/v1/auth/change-password
|
||||
*/
|
||||
export const changePassword = async (
|
||||
currentPassword: string,
|
||||
newPassword: string,
|
||||
): Promise<{ success: boolean; message: string }> => {
|
||||
const token = sessionStorage.getItem("token");
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/auth/change-password`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
current_password: currentPassword,
|
||||
new_password: newPassword,
|
||||
}),
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
return {
|
||||
success: false,
|
||||
message: data.error || data.message || "Erreur lors du changement de mot de passe",
|
||||
};
|
||||
}
|
||||
return { success: true, message: data.message || "Mot de passe mis à jour" };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Erreur de connexion",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 🛒 PANIER
|
||||
// ============================================
|
||||
@@ -1765,3 +1717,76 @@ export const checkoutCart = async (
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 🔔 NOTIFICATIONS CLIENT
|
||||
// ============================================
|
||||
|
||||
export interface ClientNotification {
|
||||
command_id: number;
|
||||
type: string;
|
||||
message: string;
|
||||
created_at: string;
|
||||
read: boolean;
|
||||
}
|
||||
|
||||
export interface NotificationsResponse {
|
||||
success: boolean;
|
||||
notifications: ClientNotification[];
|
||||
unread_count: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
export const getClientNotifications =
|
||||
async (): Promise<NotificationsResponse> => {
|
||||
const token = getAuthToken();
|
||||
if (!token)
|
||||
return {
|
||||
success: false,
|
||||
notifications: [],
|
||||
unread_count: 0,
|
||||
total: 0,
|
||||
};
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/notifications`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
if (!response.ok)
|
||||
return {
|
||||
success: false,
|
||||
notifications: [],
|
||||
unread_count: 0,
|
||||
total: 0,
|
||||
};
|
||||
const data = await response.json();
|
||||
return {
|
||||
success: true,
|
||||
notifications: data.notifications || [],
|
||||
unread_count: data.unread_count || 0,
|
||||
total: data.total || 0,
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
success: false,
|
||||
notifications: [],
|
||||
unread_count: 0,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const markNotificationsRead = async (): Promise<{
|
||||
success: boolean;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
if (!token) return { success: false };
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/notifications/read`, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
return { success: response.ok };
|
||||
} catch {
|
||||
return { success: false };
|
||||
}
|
||||
};
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,363 +0,0 @@
|
||||
export interface AdminLogin {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface ApiResponse {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
error?: string;
|
||||
access_token?: string;
|
||||
token_type?: string;
|
||||
expires_in?: number;
|
||||
user?: AdminResponse;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface AdminResponse {
|
||||
id: number;
|
||||
username: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Deliveryman Location Response
|
||||
*/
|
||||
export interface DeliverymanLocation {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
last_update: number;
|
||||
last_update_ago: number;
|
||||
is_recent: boolean;
|
||||
}
|
||||
|
||||
export interface DeliverymanInfo {
|
||||
username: string;
|
||||
status: string;
|
||||
current_command: number;
|
||||
queue_size: number;
|
||||
location: DeliverymanLocation;
|
||||
}
|
||||
|
||||
export interface CommandETA {
|
||||
minutes: number;
|
||||
has_eta: boolean;
|
||||
set_at: number;
|
||||
}
|
||||
|
||||
export interface DeliverymanLocationResponse {
|
||||
success: boolean;
|
||||
data?: {
|
||||
command_id: number;
|
||||
client: string;
|
||||
command_status: string;
|
||||
deliveryman: DeliverymanInfo;
|
||||
eta: CommandETA;
|
||||
};
|
||||
requested_by?: {
|
||||
username: string;
|
||||
role: string;
|
||||
};
|
||||
error?: string;
|
||||
message?: string;
|
||||
command_info?: {
|
||||
command_id: number;
|
||||
client: string;
|
||||
deliveryman?: string;
|
||||
status: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ProductPrice {
|
||||
id?: number;
|
||||
product_id?: number;
|
||||
quantity: number;
|
||||
price: number;
|
||||
created_at?: string;
|
||||
}
|
||||
|
||||
export interface Media {
|
||||
id?: number;
|
||||
product_id?: number;
|
||||
type: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface Product {
|
||||
id?: number;
|
||||
name: string;
|
||||
category: string;
|
||||
description: string;
|
||||
stock: number;
|
||||
prices: ProductPrice[];
|
||||
media?: Media[];
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
export interface ProductResponse {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
product?: Product;
|
||||
data?: Product;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface ProductListResponse {
|
||||
success: boolean;
|
||||
data?: Product[];
|
||||
count?: number;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface CreateProductData {
|
||||
name: string;
|
||||
category: string;
|
||||
description: string;
|
||||
stock: number;
|
||||
prices: ProductPrice[];
|
||||
media?: File[];
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Informations détaillées d'un livreur
|
||||
*/
|
||||
export interface DeliveryPersonDetails {
|
||||
id: number;
|
||||
username: string;
|
||||
role: string;
|
||||
status: "available" | "busy" | "offline";
|
||||
current_command?: number | null;
|
||||
queue_size: number;
|
||||
total_deliveries?: number;
|
||||
completed_deliveries?: number;
|
||||
pending_deliveries?: number;
|
||||
location?: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
last_update: number;
|
||||
last_update_ago: number;
|
||||
is_recent: boolean;
|
||||
};
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Statistiques d'un livreur
|
||||
*/
|
||||
export interface DeliveryPersonStats {
|
||||
username: string;
|
||||
total_deliveries: number;
|
||||
completed_deliveries: number;
|
||||
cancelled_deliveries: number;
|
||||
pending_deliveries: number;
|
||||
in_progress_deliveries: number;
|
||||
average_delivery_time?: number; // en minutes
|
||||
success_rate?: number; // en pourcentage
|
||||
total_distance?: number; // en km
|
||||
current_queue_size: number;
|
||||
last_delivery_date?: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Historique des livraisons d'un livreur
|
||||
*/
|
||||
export interface DeliveryHistory {
|
||||
command_id: number;
|
||||
client: string;
|
||||
status: string;
|
||||
adresse: string;
|
||||
total_prix: number;
|
||||
assigned_at: string;
|
||||
completed_at?: string;
|
||||
delivery_time?: number; // en minutes
|
||||
distance?: number; // en km
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Position GPS d'un livreur
|
||||
*/
|
||||
export interface DeliveryPersonLocation {
|
||||
username: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
last_update: number;
|
||||
last_update_ago: number;
|
||||
is_recent: boolean;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export interface DeliverymanLocation {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
last_update: number;
|
||||
last_update_ago: number;
|
||||
is_recent: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Statistiques d'un livreur
|
||||
*/
|
||||
export interface DeliveryPersonStats {
|
||||
username: string;
|
||||
total_deliveries: number;
|
||||
completed_deliveries: number;
|
||||
cancelled_deliveries: number;
|
||||
pending_deliveries: number;
|
||||
in_progress_deliveries: number;
|
||||
average_delivery_time?: number; // en minutes
|
||||
success_rate?: number; // en pourcentage
|
||||
total_distance?: number; // en km
|
||||
current_queue_size: number;
|
||||
last_delivery_date?: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Historique des livraisons d'un livreur
|
||||
*/
|
||||
export interface DeliveryHistory {
|
||||
command_id: number;
|
||||
client: string;
|
||||
status: string;
|
||||
adresse: string;
|
||||
total_prix: number;
|
||||
assigned_at: string;
|
||||
completed_at?: string;
|
||||
delivery_time?: number; // en minutes
|
||||
distance?: number; // en km
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Position GPS d'un livreur
|
||||
*/
|
||||
export interface DeliveryPersonLocation {
|
||||
username: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
last_update: number;
|
||||
last_update_ago: number;
|
||||
is_recent: boolean;
|
||||
status: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Réponse position livreur pour une commande
|
||||
*/
|
||||
export interface DeliverymanLocationResponse {
|
||||
success: boolean;
|
||||
data?: {
|
||||
command_id: number;
|
||||
client: string;
|
||||
command_status: string;
|
||||
deliveryman: DeliverymanInfo;
|
||||
eta: CommandETA;
|
||||
};
|
||||
requested_by?: {
|
||||
username: string;
|
||||
role: string;
|
||||
};
|
||||
error?: string;
|
||||
message?: string;
|
||||
command_info?: {
|
||||
command_id: number;
|
||||
client: string;
|
||||
deliveryman?: string;
|
||||
status: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Liste des livreurs disponibles
|
||||
*/
|
||||
export interface DeliveryPersonsListResponse {
|
||||
success: boolean;
|
||||
livreurs: DeliveryPersonDetails[];
|
||||
count: number;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 📦 INTERFACES LIVREURS - CABINE & ADMIN
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* ✅ Livreur avec détails complets (pour liste)
|
||||
*/
|
||||
export interface DeliveryPerson {
|
||||
id: number;
|
||||
username: string;
|
||||
nom?: string;
|
||||
prenom?: string;
|
||||
telephone?: string;
|
||||
status: "available" | "busy" | "offline";
|
||||
location: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
last_update: string;
|
||||
is_recent: boolean;
|
||||
};
|
||||
stats: {
|
||||
total_deliveries: number;
|
||||
completed_today: number;
|
||||
queue_size: number;
|
||||
current_command: number | null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Stats globales des livreurs
|
||||
*/
|
||||
export interface DeliveryPersonsStats {
|
||||
total: number;
|
||||
available: number;
|
||||
busy: number;
|
||||
offline: number;
|
||||
active_deliveries: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Réponse API getAllDeliveryPersonsWithDetails
|
||||
*/
|
||||
export interface AllDeliveryPersonsResponse {
|
||||
success: boolean;
|
||||
deliveryPersons: DeliveryPerson[];
|
||||
count: number;
|
||||
stats: DeliveryPersonsStats;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Liens de navigation GPS
|
||||
*/
|
||||
export interface MapLinks {
|
||||
google_maps: string;
|
||||
waze: string;
|
||||
apple_maps: string;
|
||||
openstreetmap: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ✅ Réponse API getDeliveryPersonMapLinks
|
||||
*/
|
||||
export interface MapLinksResponse {
|
||||
success: boolean;
|
||||
deliveryman?: string;
|
||||
location?: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
last_update?: number;
|
||||
is_recent: boolean;
|
||||
};
|
||||
map_links?: MapLinks;
|
||||
error?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export interface DeleteResponse {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,727 +0,0 @@
|
||||
// ============================================
|
||||
// api/api_livreur.ts - LIVREUR API HELPERS
|
||||
// ============================================
|
||||
// ✅ Fonctions helper pour le dashboard livreur
|
||||
// ✅ Utilise /api/v1/livreur/* endpoints
|
||||
|
||||
const API_URL = "/api/v1/livreur";
|
||||
|
||||
// ============================================
|
||||
// 🔐 TYPES - LIVREUR
|
||||
// ============================================
|
||||
|
||||
export interface DeliveryStatus {
|
||||
status: "available" | "busy" | "offline";
|
||||
current_command?: number;
|
||||
last_update?: number;
|
||||
}
|
||||
|
||||
export interface QueueInfo {
|
||||
queue_size: number;
|
||||
commands: any[];
|
||||
}
|
||||
|
||||
export interface DeliveryItem {
|
||||
id: number;
|
||||
status: string;
|
||||
adresse: string;
|
||||
total_prix: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
eta?: string;
|
||||
}
|
||||
|
||||
export interface ClientInfo {
|
||||
username: string;
|
||||
nom?: string;
|
||||
prenom?: string;
|
||||
telephone?: string;
|
||||
}
|
||||
|
||||
export interface DeliveryDetails {
|
||||
delivery: DeliveryItem;
|
||||
client_info: ClientInfo;
|
||||
}
|
||||
|
||||
export interface Alert {
|
||||
id: number;
|
||||
username: string;
|
||||
status: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 🔐 GESTION JWT
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* ✅ Récupérer le token d'authentification
|
||||
*/
|
||||
const getAuthToken = (): string | null => {
|
||||
return sessionStorage.getItem("admin_token");
|
||||
};
|
||||
|
||||
export const isDeliveryAuthenticated = (): boolean => {
|
||||
const token = sessionStorage.getItem("admin_token");
|
||||
return !!token;
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 📊 STATUT DU LIVREUR
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* ✅ Récupérer le statut actuel du livreur
|
||||
* GET /api/v1/livreur/status
|
||||
*/
|
||||
export const getMyStatus = async (): Promise<{
|
||||
success: boolean;
|
||||
status?: DeliveryStatus;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🔍 [GET_MY_STATUS] Appel API");
|
||||
|
||||
const response = await fetch(`${API_URL}/status`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [GET_MY_STATUS] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [GET_MY_STATUS] Réponse:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
status: data.status,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [GET_MY_STATUS] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Mettre à jour le statut du livreur
|
||||
* POST /api/v1/livreur/status
|
||||
*/
|
||||
export const updateMyStatus = async (
|
||||
status: "available" | "busy" | "offline",
|
||||
): Promise<{ success: boolean; message?: string; error?: string }> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("📝 [UPDATE_MY_STATUS] Appel API:", status);
|
||||
|
||||
const response = await fetch(`${API_URL}/update/status`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ status }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [UPDATE_MY_STATUS] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [UPDATE_MY_STATUS] Réponse:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: data.message,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [UPDATE_MY_STATUS] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 📦 QUEUE DU LIVREUR
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* ✅ Récupérer la queue de livraisons du livreur
|
||||
* GET /api/v1/livreur/queue
|
||||
*/
|
||||
export const getMyQueue = async (): Promise<{
|
||||
success: boolean;
|
||||
queue_info?: QueueInfo;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🔍 [GET_MY_QUEUE] Appel API");
|
||||
|
||||
const response = await fetch(`${API_URL}/queue`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [GET_MY_QUEUE] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [GET_MY_QUEUE] Réponse:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
queue_info: data.queue_info,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [GET_MY_QUEUE] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 🚚 LIVRAISONS
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* ✅ Récupérer toutes les livraisons du livreur
|
||||
* GET /api/v1/livreur/deliveries
|
||||
*/
|
||||
export const getMyDeliveries = async (): Promise<{
|
||||
success: boolean;
|
||||
deliveries?: DeliveryItem[];
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🔍 [GET_MY_DELIVERIES] Appel API");
|
||||
|
||||
const response = await fetch(`${API_URL}/deliveries`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [GET_MY_DELIVERIES] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [GET_MY_DELIVERIES] Réponse:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
deliveries: data.deliveries || [],
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [GET_MY_DELIVERIES] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Récupérer les détails d'une livraison spécifique
|
||||
* GET /api/v1/livreur/deliveries/:id
|
||||
*/
|
||||
export const getDeliveryDetails = async (
|
||||
deliveryId: number,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
delivery?: DeliveryDetails;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🔍 [GET_DELIVERY_DETAILS] Appel API:", deliveryId);
|
||||
|
||||
const response = await fetch(`${API_URL}/deliveries/${deliveryId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [GET_DELIVERY_DETAILS] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [GET_DELIVERY_DETAILS] Réponse:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
delivery: {
|
||||
delivery: data.delivery,
|
||||
client_info: data.client_info,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [GET_DELIVERY_DETAILS] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Démarrer une livraison
|
||||
* POST /api/v1/livreur/deliveries/:id/start
|
||||
*/
|
||||
export const startDelivery = async (
|
||||
deliveryId: number,
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
): Promise<{ success: boolean; message?: string; error?: string }> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🚀 [START_DELIVERY] Appel API:", deliveryId);
|
||||
|
||||
const response = await fetch(
|
||||
`${API_URL}/deliveries/${deliveryId}/start`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ latitude, longitude }),
|
||||
},
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [START_DELIVERY] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [START_DELIVERY] Réponse:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: data.message,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [START_DELIVERY] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Mettre à jour le statut d'une livraison
|
||||
* PUT /api/v1/livreur/deliveries/:id/status
|
||||
*/
|
||||
export const updateDeliveryStatus = async (
|
||||
deliveryId: number,
|
||||
status: string,
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
): Promise<{ success: boolean; message?: string; error?: string }> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(
|
||||
"📝 [UPDATE_DELIVERY_STATUS] Appel API:",
|
||||
deliveryId,
|
||||
status,
|
||||
);
|
||||
|
||||
const response = await fetch(
|
||||
`${API_URL}/deliveries/${deliveryId}/status`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ status, latitude, longitude }),
|
||||
},
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [UPDATE_DELIVERY_STATUS] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [UPDATE_DELIVERY_STATUS] Réponse:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: data.message,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [UPDATE_DELIVERY_STATUS] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 📍 POSITION GPS
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* ✅ Mettre à jour la position GPS du livreur
|
||||
* POST /api/v1/livreur/location/update
|
||||
*/
|
||||
export const updateMyLocation = async (
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
message?: string;
|
||||
status?: string;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/location/update`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ latitude, longitude }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [UPDATE_MY_LOCATION] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: data.message,
|
||||
status: data.status,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [UPDATE_MY_LOCATION] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Récupérer la position actuelle du livreur
|
||||
* GET /api/v1/livreur/location
|
||||
*/
|
||||
export const getMyLocation = async (): Promise<{
|
||||
success: boolean;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/location`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [GET_MY_LOCATION] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
latitude: data.latitude,
|
||||
longitude: data.longitude,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [GET_MY_LOCATION] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 🚨 ALERTES POLICE
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* ✅ Déclencher une alerte police
|
||||
* POST /api/v1/livreur/alert
|
||||
*/
|
||||
export const triggerPoliceAlert = async (): Promise<{
|
||||
success: boolean;
|
||||
alert_id?: number;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🚨 [TRIGGER_POLICE_ALERT] Déclenchement alerte police");
|
||||
|
||||
const response = await fetch(`${API_URL}/alert`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [TRIGGER_POLICE_ALERT] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [TRIGGER_POLICE_ALERT] Alerte créée:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
alert_id: data.alert_id,
|
||||
message: data.message,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [TRIGGER_POLICE_ALERT] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Mettre fin à une alerte
|
||||
* DELETE /api/v1/livreur/alert/:id
|
||||
*/
|
||||
export const endAlert = async (
|
||||
alertId: number,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🔚 [END_ALERT] Terminer alerte:", alertId);
|
||||
|
||||
const response = await fetch(`${API_URL}/alert/${alertId}`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [END_ALERT] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [END_ALERT] Alerte terminée:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: data.message,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [END_ALERT] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Récupérer toutes mes alertes
|
||||
* GET /api/v1/livreur/alerts
|
||||
*/
|
||||
export const getMyAlerts = async (): Promise<{
|
||||
success: boolean;
|
||||
alerts?: Alert[];
|
||||
count?: number;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🔍 [GET_MY_ALERTS] Récupération alertes");
|
||||
|
||||
const response = await fetch(`${API_URL}/alerts`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [GET_MY_ALERTS] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [GET_MY_ALERTS] Alertes récupérées:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
alerts: data.alerts || [],
|
||||
count: data.count || 0,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [GET_MY_ALERTS] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ✅ Récupérer les détails d'une alerte
|
||||
* GET /api/v1/livreur/alert/:id
|
||||
*/
|
||||
export const getAlertDetails = async (
|
||||
alertId: number,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
alert?: Alert;
|
||||
error?: string;
|
||||
}> => {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (!token) {
|
||||
return { success: false, error: "Token non trouvé" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("🔍 [GET_ALERT_DETAILS] Récupération alerte:", alertId);
|
||||
|
||||
const response = await fetch(`${API_URL}/alert/${alertId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("❌ [GET_ALERT_DETAILS] Erreur API:", data);
|
||||
return { success: false, error: data.error };
|
||||
}
|
||||
|
||||
console.log("✅ [GET_ALERT_DETAILS] Alerte récupérée:", data);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
alert: data.alert,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ [GET_ALERT_DETAILS] Erreur fetch:", error);
|
||||
return { success: false, error: "Erreur réseau" };
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================
|
||||
// 🔄 EXPORT PAR DÉFAUT
|
||||
// ============================================
|
||||
|
||||
export default {
|
||||
// Statut
|
||||
getMyStatus,
|
||||
updateMyStatus,
|
||||
|
||||
// Queue
|
||||
getMyQueue,
|
||||
|
||||
// Livraisons
|
||||
getMyDeliveries,
|
||||
getDeliveryDetails,
|
||||
startDelivery,
|
||||
updateDeliveryStatus,
|
||||
|
||||
// Position GPS
|
||||
updateMyLocation,
|
||||
getMyLocation,
|
||||
|
||||
// Alertes Police
|
||||
triggerPoliceAlert,
|
||||
endAlert,
|
||||
getMyAlerts,
|
||||
getAlertDetails,
|
||||
};
|
||||
Reference in New Issue
Block a user