implementação de listagem de usuários admin, padronização de roles e melhorias nos cadastros Backend: - Adicionados endpoints administrativos [ListUsers](cci:1://file:///c:/Projetos/photum/backend/internal/auth/service.go:268:0-270:1) e [GetUser](cci:1://file:///c:/Projetos/photum/backend/internal/auth/handler.go:475:0-514:1). - Padronizadas as constantes de [UserRole](cci:1://file:///c:/Projetos/photum/backend/internal/auth/service.go:202:0-216:1) (`SUPERADMIN`, `BUSINESS_OWNER`, etc.) para alinhar com o frontend. - Atualizada a função [EnsureDemoUsers](cci:1://file:///c:/Projetos/photum/backend/internal/auth/service.go:230:0-266:1) para migrar usuários existentes para as novas roles. - Documentação Swagger regenerada. Frontend: - Adicionado busca automática de CEP no formulário de Cadastro Profissional (AwesomeAPI). - Adicionado texto de ajuda e ordenação (priorizando "Não Cadastrado") no select de Empresas.
264 lines
5.6 KiB
TypeScript
264 lines
5.6 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");
|
|
}
|
|
|
|
// ... existing functions ...
|
|
|
|
/**
|
|
* Busca usuários pendentes de aprovação
|
|
*/
|
|
export async function getPendingUsers(token: string): Promise<ApiResponse<any[]>> {
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/admin/users/pending`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Authorization": `Bearer ${token}`
|
|
},
|
|
});
|
|
|
|
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 pending users:", error);
|
|
return {
|
|
data: null,
|
|
error: error instanceof Error ? error.message : "Erro desconhecido",
|
|
isBackendDown: true,
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Aprova um usuário
|
|
*/
|
|
export async function approveUser(userId: string, token: string): Promise<ApiResponse<any>> {
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/admin/users/${userId}/approve`, {
|
|
method: "PATCH",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Authorization": `Bearer ${token}`
|
|
},
|
|
});
|
|
|
|
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 approving user:", error);
|
|
return {
|
|
data: null,
|
|
error: error instanceof Error ? error.message : "Erro desconhecido",
|
|
isBackendDown: true,
|
|
};
|
|
}
|
|
}
|