- backend: atualizado /auth/register para retornar userId e access_token - backend: desabilitada criação automática de perfil parcial no registro - backend: adicionado suporte a cookie access_token no middleware e handlers - frontend: atualizado AuthContext para enviar role e retornar token - frontend: implementado registro de profissional em 2 etapas (Usuário -> Perfil) - frontend: adicionado serviço createProfessional com suporte a header de auth - frontend: definido role correto (EVENT_OWNER) para cadastro de clientes
196 lines
4 KiB
TypeScript
196 lines
4 KiB
TypeScript
// Serviço para comunicação com o backend
|
|
const API_BASE_URL =
|
|
import.meta.env.VITE_API_URL || "http://localhost:3000/api";
|
|
|
|
interface ApiResponse<T> {
|
|
data: T | null;
|
|
error: string | null;
|
|
isBackendDown: boolean;
|
|
}
|
|
|
|
// Função auxiliar para fazer requisições
|
|
async function fetchFromBackend<T>(endpoint: string): Promise<ApiResponse<T>> {
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
return {
|
|
data,
|
|
error: null,
|
|
isBackendDown: false,
|
|
};
|
|
} catch (error) {
|
|
console.error(`Error fetching ${endpoint}:`, error);
|
|
return {
|
|
data: null,
|
|
error: error instanceof Error ? error.message : "Erro desconhecido",
|
|
isBackendDown: true,
|
|
};
|
|
}
|
|
}
|
|
|
|
// Funções específicas para cada endpoint
|
|
|
|
/**
|
|
* Busca as funções profissionais disponíveis
|
|
*/
|
|
export async function getProfessionalRoles(): Promise<ApiResponse<string[]>> {
|
|
return fetchFromBackend<string[]>("/professional-roles");
|
|
}
|
|
|
|
/**
|
|
* Busca as empresas disponíveis
|
|
*/
|
|
export async function getCompanies(): Promise<
|
|
ApiResponse<
|
|
Array<{
|
|
id: string;
|
|
nome: string;
|
|
}>
|
|
>
|
|
> {
|
|
return fetchFromBackend("/api/empresas");
|
|
}
|
|
|
|
/**
|
|
* Busca as funções profissionais disponíveis do backend
|
|
*/
|
|
export async function getFunctions(): Promise<
|
|
ApiResponse<
|
|
Array<{
|
|
id: string;
|
|
nome: string;
|
|
}>
|
|
>
|
|
> {
|
|
return fetchFromBackend("/api/funcoes");
|
|
}
|
|
|
|
/**
|
|
* Cria um novo perfil profissional
|
|
*/
|
|
export async function createProfessional(data: any, token?: string): Promise<ApiResponse<any>> {
|
|
try {
|
|
const headers: any = {
|
|
"Content-Type": "application/json",
|
|
};
|
|
|
|
if (token) {
|
|
headers["Authorization"] = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/profissionais`, {
|
|
method: "POST",
|
|
headers: headers,
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
|
|
}
|
|
|
|
const responseData = await response.json();
|
|
return {
|
|
data: responseData,
|
|
error: null,
|
|
isBackendDown: false,
|
|
};
|
|
} catch (error) {
|
|
console.error("Error creating professional:", error);
|
|
return {
|
|
data: null,
|
|
error: error instanceof Error ? error.message : "Erro desconhecido",
|
|
isBackendDown: true,
|
|
};
|
|
}
|
|
}
|
|
|
|
export interface EventTypeResponse {
|
|
id: string;
|
|
nome: string;
|
|
precos: any[];
|
|
}
|
|
|
|
/**
|
|
* Busca os tipos de eventos disponíveis
|
|
*/
|
|
export async function getEventTypes(): Promise<
|
|
ApiResponse<EventTypeResponse[]>
|
|
> {
|
|
return fetchFromBackend<EventTypeResponse[]>("/api/tipos-eventos");
|
|
}
|
|
|
|
/**
|
|
* Busca os cursos/turmas disponíveis
|
|
*/
|
|
export async function getCourses(): Promise<
|
|
ApiResponse<
|
|
Array<{
|
|
id: string;
|
|
name: string;
|
|
institution: string;
|
|
year: number;
|
|
}>
|
|
>
|
|
> {
|
|
return fetchFromBackend("/courses");
|
|
}
|
|
|
|
/**
|
|
* Busca as instituições/empresas disponíveis
|
|
*/
|
|
export async function getInstitutions(): Promise<
|
|
ApiResponse<
|
|
Array<{
|
|
id: string;
|
|
name: string;
|
|
}>
|
|
>
|
|
> {
|
|
return fetchFromBackend("/institutions");
|
|
}
|
|
|
|
/**
|
|
* Busca os anos de formatura disponíveis
|
|
*/
|
|
export async function getGraduationYears(): Promise<ApiResponse<number[]>> {
|
|
return fetchFromBackend<number[]>("/graduation-years");
|
|
}
|
|
|
|
/**
|
|
* Busca os níveis educacionais disponíveis (EF I / EF II)
|
|
*/
|
|
export async function getEducationLevels(): Promise<
|
|
ApiResponse<
|
|
Array<{
|
|
id: string;
|
|
nome: string;
|
|
}>
|
|
>
|
|
> {
|
|
return fetchFromBackend("/api/niveis-educacionais");
|
|
}
|
|
|
|
/**
|
|
* Busca as universidades cadastradas
|
|
*/
|
|
export async function getUniversities(): Promise<
|
|
ApiResponse<
|
|
Array<{
|
|
id: string;
|
|
nome: string;
|
|
}>
|
|
>
|
|
> {
|
|
return fetchFromBackend("/api/universidades");
|
|
}
|