riseup-squad21/services/usuariosApi.ts
2025-10-22 20:55:23 -03:00

76 lines
1.8 KiB
TypeScript

import api from "./api";
export interface UserRole {
id: any;
user_id: string;
role: string;
[key: string]: any;
}
export interface User {
id: any;
email: string;
[key: string]: any;
}
export interface FullUserData {
user: User;
profile: any;
roles: string[];
permissions: any;
}
export const usuariosApi = {
listRoles: async (): Promise<UserRole[]> => {
const response = await api.get<UserRole[]>("/rest/v1/user_roles");
return response.data;
},
createUser: async (data: {
email: string;
full_name: string;
phone?: string;
phone_mobile?: string;
role: string;
cpf?: string;
create_patient_record?: boolean;
}): Promise<any> => {
try {
const response = await api.post("/functions/v1/create-user", data, {
headers: {
"Content-Type": "application/json",
apikey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || "",
Authorization: `Bearer ${
typeof window !== "undefined"
? localStorage.getItem("supabase-access-token") ||
""
: ""
}`,
},
});
return response.data;
} catch (error: any) {
console.error("❌ Erro no createUser:", error.response?.data || error);
throw error;
}
},
getCurrentUser: async (): Promise<User> => {
const response = await api.post<User>(
"/functions/v1/user-info",
{},
{ headers: { "Content-Type": "application/json" } }
);
return response.data;
},
getFullData: async (userId: string): Promise<FullUserData> => {
const response = await api.post<FullUserData>(
"/functions/v1/user-info-by-id",
{ user_id: userId },
{ headers: { "Content-Type": "application/json" } }
);
return response.data;
},
};