M-Gabrielly cde4c42309 feat(auth): Creates user in patient registration
- Adds automatic user creation in the new patient registration flow.              - To avoid merge conflicts, the user and profile APIs have been separated from the main lib/api.ts file.
2025-10-01 23:20:35 -03:00

79 lines
2.1 KiB
TypeScript

import {
API_BASE,
headers,
logAPI,
parse,
} from "../api";
import type { ApiOk } from "../api";
import type { UserProfile } from "./perfis";
//
// User Management APIs
//
export type UserRole = {
id: string;
user_id: string;
role: 'admin' | 'medico' | 'paciente';
created_at: string;
};
export type CreateUserInput = {
email: string;
password?: string;
full_name: string;
phone?: string;
role: 'admin' | 'medico' | 'paciente';
};
export type CreatedUser = {
id: string;
email: string;
full_name: string;
phone?: string;
role: string;
};
export type CompleteUserInfo = {
user: {
id: string;
email: string;
email_confirmed_at: string;
created_at: string;
last_sign_in_at: string;
};
profile: UserProfile;
roles: string[];
permissions: {
isAdmin: boolean;
isManager: boolean;
isDoctor: boolean;
isSecretary: boolean;
isAdminOrManager: boolean;
};
};
export async function criarUsuario(input: CreateUserInput): Promise<{ success: boolean; user: CreatedUser }> {
const url = `${API_BASE}/functions/v1/create-user`;
const res = await fetch(url, { method: "POST", headers: headers("json"), body: JSON.stringify(input) });
const data = await parse<any>(res);
logAPI("criarUsuario", { url, payload: input, result: data });
return data;
}
export async function listarUserRoles(): Promise<UserRole[]> {
const url = `${API_BASE}/rest/v1/user_roles`;
const res = await fetch(url, { method: "GET", headers: headers("json") });
const data = await parse<UserRole[]>(res);
logAPI("listarUserRoles", { url, result: data });
return data ?? [];
}
export async function getCompleteUserInfo(userId: string): Promise<CompleteUserInfo> {
const url = `${API_BASE}/functions/v1/user-info`;
// Assuming the function takes the user ID in the body of a POST request
const res = await fetch(url, { method: "POST", headers: headers("json"), body: JSON.stringify({ id: userId }) });
const data = await parse<CompleteUserInfo>(res);
logAPI("getCompleteUserInfo", { url, payload: { id: userId }, result: data });
return data;
}