251 lines
7.0 KiB
TypeScript
251 lines
7.0 KiB
TypeScript
import { API_BASE_URL } from "@config/config_frontend";
|
|
|
|
export interface DashboardUser {
|
|
id: number;
|
|
email: string;
|
|
name: string;
|
|
position?: string | null;
|
|
department?: string | null;
|
|
phone?: string | null;
|
|
company_id?: number | null;
|
|
company_name?: string | null;
|
|
role: "SYSTEM_ADMIN" | "ADMIN" | "USER";
|
|
is_master: boolean;
|
|
status: string;
|
|
}
|
|
|
|
export interface ProjectItem {
|
|
id: string;
|
|
name: string;
|
|
region?: string | null;
|
|
status?: string | null;
|
|
owner_name?: string | null;
|
|
workflow_stage: number;
|
|
progress_percent: number;
|
|
updated_at?: string | null;
|
|
}
|
|
|
|
export interface CompanyInfo {
|
|
id: number;
|
|
name: string;
|
|
business_registration_number?: string | null;
|
|
business_address?: string | null;
|
|
business_owner?: string | null;
|
|
business_status?: string | null;
|
|
user_count?: number;
|
|
project_count?: number;
|
|
}
|
|
|
|
export interface Member {
|
|
id: number;
|
|
email: string;
|
|
name: string;
|
|
position?: string | null;
|
|
department?: string | null;
|
|
role: string;
|
|
status: string;
|
|
}
|
|
|
|
export interface JoinRequest {
|
|
id: number;
|
|
user_id: number;
|
|
company_id: number;
|
|
user_email: string;
|
|
user_name?: string | null;
|
|
company_name?: string | null;
|
|
requested_at: string;
|
|
status: string;
|
|
}
|
|
|
|
export interface AuditLog {
|
|
id: number;
|
|
user_id: number;
|
|
email?: string | null;
|
|
action: string;
|
|
resource_type?: string | null;
|
|
resource_id?: number | null;
|
|
timestamp: string;
|
|
}
|
|
|
|
export interface ResourceSnapshot {
|
|
cpu_usage_percent: number | null;
|
|
memory_usage_percent: number | null;
|
|
disk_usage_percent: number | null;
|
|
active_user_count: number;
|
|
active_project_count: number;
|
|
total_storage_mb: number;
|
|
}
|
|
|
|
export interface ResourceHistoryPoint {
|
|
timestamp: string;
|
|
cpu_usage_percent: number | null;
|
|
memory_usage_percent: number | null;
|
|
disk_usage_percent: number | null;
|
|
active_user_count: number;
|
|
active_project_count: number;
|
|
total_storage_mb: number;
|
|
}
|
|
|
|
export interface ResourceData {
|
|
current: ResourceSnapshot;
|
|
history: ResourceHistoryPoint[];
|
|
stats: ResourceSnapshot;
|
|
}
|
|
|
|
export interface UpdateUserRequest {
|
|
name: string;
|
|
position?: string | null;
|
|
department?: string | null;
|
|
phone?: string | null;
|
|
}
|
|
|
|
export interface CreateCompanyRequest {
|
|
name: string;
|
|
business_registration_number: string;
|
|
business_address?: string | null;
|
|
business_owner?: string | null;
|
|
}
|
|
|
|
export interface ChangePasswordRequest {
|
|
current_password: string;
|
|
new_password: string;
|
|
new_password_confirm: string;
|
|
logout_all: boolean;
|
|
}
|
|
|
|
async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
|
|
const response = await fetch(`${API_BASE_URL}${path}`, {
|
|
credentials: "include",
|
|
headers: { "Content-Type": "application/json", ...(init.headers ?? {}) },
|
|
...init,
|
|
});
|
|
const data = (await response.json()) as { detail?: string } & T;
|
|
if (!response.ok) throw new Error(data.detail ?? "Request failed");
|
|
return data;
|
|
}
|
|
|
|
const body = (value: object): string => JSON.stringify(value);
|
|
|
|
export async function fetchDashboardMe(): Promise<DashboardUser> {
|
|
const data = await request<{ user: DashboardUser }>("/dashboard/me");
|
|
return data.user;
|
|
}
|
|
|
|
export async function updateUserProfile(payload: UpdateUserRequest): Promise<DashboardUser> {
|
|
const data = await request<{ user: DashboardUser }>("/dashboard/me", {
|
|
method: "PATCH",
|
|
body: body(payload),
|
|
});
|
|
return data.user;
|
|
}
|
|
|
|
export function changePassword(payload: ChangePasswordRequest): Promise<unknown> {
|
|
return request("/auth/password", {
|
|
method: "POST",
|
|
body: body(payload),
|
|
});
|
|
}
|
|
|
|
export async function fetchUserProjects(): Promise<ProjectItem[]> {
|
|
const data = await request<{ projects: ProjectItem[] }>("/dashboard/user/projects");
|
|
return data.projects;
|
|
}
|
|
|
|
export async function fetchUserCompany(): Promise<CompanyInfo | null> {
|
|
const data = await request<{ company: CompanyInfo | null }>("/dashboard/user/company");
|
|
return data.company;
|
|
}
|
|
|
|
export async function searchCompanies(query: string): Promise<CompanyInfo[]> {
|
|
const data = await request<{ companies: CompanyInfo[] }>(
|
|
`/dashboard/user/companies?q=${encodeURIComponent(query)}`,
|
|
);
|
|
return data.companies;
|
|
}
|
|
|
|
export function createCompany(payload: CreateCompanyRequest): Promise<unknown> {
|
|
return request("/dashboard/user/company/create", { method: "POST", body: body(payload) });
|
|
}
|
|
|
|
export function joinCompany(companyId: number): Promise<unknown> {
|
|
return request("/dashboard/user/company/join", {
|
|
method: "POST",
|
|
body: body({ company_id: companyId }),
|
|
});
|
|
}
|
|
|
|
export async function fetchCompanyMembers(): Promise<Member[]> {
|
|
const data = await request<{ members: Member[] }>("/dashboard/admin/members");
|
|
return data.members;
|
|
}
|
|
|
|
export function addCompanyMember(email: string): Promise<unknown> {
|
|
return request("/dashboard/admin/members", { method: "POST", body: body({ email }) });
|
|
}
|
|
|
|
export function removeCompanyMember(userId: number): Promise<unknown> {
|
|
return request(`/dashboard/admin/members/${userId}`, { method: "DELETE" });
|
|
}
|
|
|
|
export async function fetchCompanyJoinRequests(): Promise<JoinRequest[]> {
|
|
const data = await request<{ requests: JoinRequest[] }>("/dashboard/admin/join-requests");
|
|
return data.requests;
|
|
}
|
|
|
|
export function processJoinRequest(
|
|
requestId: number,
|
|
action: "APPROVE" | "REJECT",
|
|
): Promise<unknown> {
|
|
return request(`/dashboard/admin/join-requests/${requestId}`, {
|
|
method: "PATCH",
|
|
body: body({ action }),
|
|
});
|
|
}
|
|
|
|
export async function fetchCompanyProjects(): Promise<ProjectItem[]> {
|
|
const data = await request<{ projects: ProjectItem[] }>("/dashboard/admin/projects");
|
|
return data.projects;
|
|
}
|
|
|
|
export async function fetchAllCompanies(): Promise<CompanyInfo[]> {
|
|
const data = await request<{ companies: CompanyInfo[] }>("/dashboard/admin/companies");
|
|
return data.companies;
|
|
}
|
|
|
|
export async function fetchAllUsers(): Promise<DashboardUser[]> {
|
|
const data = await request<{ users: DashboardUser[] }>("/dashboard/admin/users");
|
|
return data.users;
|
|
}
|
|
|
|
export function changeUserRole(userId: number, role: string): Promise<unknown> {
|
|
return request(`/dashboard/admin/users/${userId}/role`, {
|
|
method: "PATCH",
|
|
body: body({ role }),
|
|
});
|
|
}
|
|
|
|
export function assignUserToCompany(userId: number, companyId: number | null): Promise<unknown> {
|
|
return request(`/dashboard/admin/users/${userId}/company`, {
|
|
method: "PATCH",
|
|
body: body({ company_id: companyId }),
|
|
});
|
|
}
|
|
|
|
export async function fetchAllJoinRequests(): Promise<JoinRequest[]> {
|
|
const data = await request<{ requests: JoinRequest[] }>("/dashboard/admin/join-requests-all");
|
|
return data.requests;
|
|
}
|
|
|
|
export function systemApproveJoinRequest(requestId: number): Promise<unknown> {
|
|
return request(`/dashboard/admin/join-requests/${requestId}/approve`, { method: "PATCH" });
|
|
}
|
|
|
|
export async function fetchAuditLogs(): Promise<AuditLog[]> {
|
|
const data = await request<{ items: AuditLog[] }>("/dashboard/admin/audit-logs");
|
|
return data.items;
|
|
}
|
|
|
|
export async function fetchSystemResources(days = 30): Promise<ResourceData> {
|
|
return request<ResourceData>(`/dashboard/admin/resources?days=${encodeURIComponent(days)}`);
|
|
}
|