대시보드 삭제 버튼은 지금까지 projects.deleted_at만 찍는 소프트 삭제였다. 배포에서는
그게 맞다 — 사용자가 올린 라이다 원본은 다른 프로젝트에 재활용할 자산이다. 그러나 개발
중에는 프로젝트를 반복 생성·삭제하는데 정리 잡이 없어 수십 GB 원본이 계속 쌓인다.
config_system.py 맨 위에 PROJECT_DELETE_HARD_ENABLED를 두고 갈랐다. 기본값 False라
환경변수를 빠뜨린 배포 환경은 자동으로 안전한 쪽에 선다. True면 projects 행을 실제로
DELETE 하고(자식 테이블은 FK CASCADE로 함께 사라진다) storage/{회사}/{사용자}/{프로젝트ID}/
폴더를 통째로 지운다.
자식 테이블 목록은 코드에 나열하지 않았다. projects.id 참조가 전부 ON DELETE CASCADE라
행 하나면 충분하고, 목록을 복사해 두면 스키마가 바뀔 때 조용히 어긋난다.
순서는 DB 먼저 커밋, rmtree 나중이다. 파일을 먼저 지우면 DB 실패 시 실체 없는 프로젝트가
목록에 남아 화면이 깨진다. 반대면 rmtree가 실패해도 고아 폴더만 남고 정합성은 유지된다.
resolve_stored_project_path()는 끝에서 makedirs를 하므로 삭제에 쓸 수 없다 — 지우기 직전에
폴더를 되살린다. 검증만 하는 resolve_project_root_for_delete()를 따로 뒀고, 저장소 루트 안 ·
세그먼트 정확히 4개 · 마지막 세그먼트가 요청 project_id와 일치를 모두 요구한다. DB의
storage_path가 오염돼도 상위 폴더나 남의 폴더를 지우지 못한다.
하드 삭제 모드에서는 확인 모달 문구를 바꿔 원본까지 사라진다고 알린다.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
326 lines
9.2 KiB
TypeScript
326 lines
9.2 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;
|
|
/** 서버가 하드 삭제 모드인지 (`PROJECT_DELETE_HARD_ENABLED`). GET /me에서만 채워진다. */
|
|
project_delete_hard?: boolean;
|
|
}
|
|
|
|
export interface WorkflowStageState {
|
|
stage_no: number;
|
|
stage_key: string;
|
|
state: "NOT_STARTED" | "IN_PROGRESS" | "COMPLETE" | "FAILED" | "STALE";
|
|
progress_percent: number;
|
|
params?: any | null;
|
|
message?: string | null;
|
|
started_at?: string | null;
|
|
completed_at?: string | null;
|
|
}
|
|
|
|
export interface WorkflowState {
|
|
project_id: string;
|
|
current_stage: number;
|
|
stages: WorkflowStageState[];
|
|
}
|
|
|
|
export interface ProjectItem {
|
|
id: string;
|
|
company_id?: number | null;
|
|
name: string;
|
|
region?: string | null;
|
|
road_type?: string | null;
|
|
project_year?: number | null;
|
|
estimated_length_m?: number | null;
|
|
memo?: string | null;
|
|
status?: string | null;
|
|
owner_name?: string | null;
|
|
workflow_stage: number;
|
|
progress_percent: number;
|
|
workflow_state?: WorkflowState;
|
|
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 UpdateProjectRequest {
|
|
name: string;
|
|
region?: string | null;
|
|
road_type?: string | null;
|
|
project_year?: number | null;
|
|
estimated_length_m?: number | null;
|
|
memo?: string | null;
|
|
status?: string | null;
|
|
}
|
|
|
|
export interface AdminUpdateUserRequest extends UpdateUserRequest {
|
|
status?: 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 createSystemCompany(payload: CreateCompanyRequest): Promise<unknown> {
|
|
return request("/dashboard/admin/companies", { 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 fetchAllProjects(): Promise<ProjectItem[]> {
|
|
const data = await request<{ projects: ProjectItem[] }>("/dashboard/admin/projects-all");
|
|
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 updateProject(projectId: string, payload: UpdateProjectRequest): Promise<unknown> {
|
|
return request(`/dashboard/projects/${encodeURIComponent(projectId)}`, {
|
|
method: "PUT",
|
|
body: body(payload),
|
|
});
|
|
}
|
|
|
|
export function deleteProject(projectId: string): Promise<unknown> {
|
|
return request(`/dashboard/projects/${encodeURIComponent(projectId)}`, { method: "DELETE" });
|
|
}
|
|
|
|
export function updateDashboardUser(
|
|
userId: number,
|
|
payload: AdminUpdateUserRequest,
|
|
): Promise<unknown> {
|
|
return request(`/dashboard/admin/users/${userId}`, {
|
|
method: "PUT",
|
|
body: body(payload),
|
|
});
|
|
}
|
|
|
|
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)}`);
|
|
}
|
|
|
|
export function fetchProjectWorkflowState(projectId: string): Promise<WorkflowState> {
|
|
return request(`/projects/${projectId}/workflow-state`, {
|
|
method: "GET",
|
|
}) as Promise<WorkflowState>;
|
|
}
|