43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { dolibarrFetch } from "./dolibarrClient";
|
|
import { DolibarrUser, UserProfile, mapDolibarrUser } from "@/types/user";
|
|
|
|
/**
|
|
* Obtiene los datos crudos de un usuario de Dolibarr
|
|
*/
|
|
export async function getDolibarrUser(id?: string): Promise<DolibarrUser> {
|
|
const userId = id || process.env.NEXT_PUBLIC_DOLIBARR_USER_ID || "1";
|
|
return dolibarrFetch(`users/${userId}`);
|
|
}
|
|
|
|
/**
|
|
* Obtiene el perfil de usuario mapeado para la UI
|
|
*/
|
|
export async function getUserProfile(id?: string): Promise<UserProfile> {
|
|
const raw = await getDolibarrUser(id);
|
|
return mapDolibarrUser(raw);
|
|
}
|
|
|
|
/**
|
|
* Obtiene datos básicos de usuario (para sidebar, compatibilidad hacia atrás)
|
|
*/
|
|
export async function getUser(id?: string): Promise<{
|
|
firstname: string;
|
|
lastname: string;
|
|
email: string;
|
|
}> {
|
|
const raw = await getDolibarrUser(id);
|
|
return {
|
|
firstname: raw.firstname || raw.login || 'Usuario',
|
|
lastname: raw.lastname || '',
|
|
email: raw.email || '',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Obtiene el perfil del usuario autenticado actualmente
|
|
* Usa el ID configurado en las variables de entorno
|
|
*/
|
|
export async function getCurrentUserProfile(): Promise<UserProfile> {
|
|
return getUserProfile();
|
|
}
|