refactor(db,frontend): Improve queries and modularize frontend

- Replaced SELECT* queries in 8 models with explicit columns.
- Began modularizing work-report-calendar.js by creating CalendarAPI.js, CalendarState.js, and CalendarView.js.
- Refactored manage-project.js to use global API helpers.
- Fixed API container crash by adding missing volume mounts to docker-compose.yml.
- Added new migration for missing columns in the projects table.
- Documented current DB schema and deployment notes.
This commit is contained in:
Hyungi Ahn
2025-12-19 12:42:24 +09:00
parent 8a8307edfc
commit 05843da1c4
19 changed files with 826 additions and 381 deletions

View File

@@ -0,0 +1,308 @@
# Current Database Schema
> **Last Updated**: 2025-12-19
> **Source**: Synthesized from `hyungi_schema_v2.sql`, migration files, setup scripts, and model definitions.
This document represents the most up-to-date understanding of the database schema, reflecting all migrations and code-inferred structures.
---
## Main Tables
### `users`
*Source: `hyungi_schema_v2.sql`*
```sql
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(255) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`role` varchar(30) DEFAULT 'user' COMMENT '역할 (system, admin, leader, user)',
`access_level` varchar(30) DEFAULT NULL COMMENT '접근 레벨 (레거시 필드, role로 통합 고려)',
`worker_id` int(11) DEFAULT NULL COMMENT '연결된 작업자 ID',
`is_active` tinyint(1) DEFAULT 1 COMMENT '계정 활성화 여부',
`last_login_at` datetime DEFAULT NULL COMMENT '마지막 로그인 시간',
`password_changed_at` datetime DEFAULT NULL,
`failed_login_attempts` int(11) DEFAULT 0,
`locked_until` datetime DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`user_id`),
UNIQUE KEY `username` (`username`),
KEY `fk_users_worker_id` (`worker_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
### `projects`
*Source: `hyungi_schema_v2.sql` + `20251219010000_add_columns_to_projects.js` migration.*
```sql
CREATE TABLE `projects` (
`project_id` int(11) NOT NULL AUTO_INCREMENT,
`job_no` varchar(50) NOT NULL,
`project_name` varchar(255) NOT NULL,
`contract_date` date DEFAULT NULL,
`due_date` date DEFAULT NULL,
`delivery_method` varchar(100) DEFAULT NULL,
`site` varchar(100) DEFAULT NULL,
`pm` varchar(100) DEFAULT NULL,
`is_active` tinyint(1) DEFAULT 1,
`project_status` varchar(255) DEFAULT 'active',
`completed_date` date DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`project_id`),
UNIQUE KEY `job_no` (`job_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
### `workers`
*Source: `hyungi_schema_v2.sql`*
```sql
CREATE TABLE `workers` (
`worker_id` int(11) NOT NULL AUTO_INCREMENT,
`worker_name` varchar(100) NOT NULL,
`job_type` varchar(100) DEFAULT NULL COMMENT '직종',
`join_date` date DEFAULT NULL COMMENT '입사일',
`status` varchar(20) DEFAULT 'active' COMMENT '상태 (active, inactive)',
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`worker_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
### `daily_work_reports`
*Source: `hyungi.sql`*
```sql
CREATE TABLE `daily_work_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`report_date` date NOT NULL COMMENT '작업 날짜',
`worker_id` int(11) NOT NULL COMMENT '작업자 ID',
`project_id` int(11) NOT NULL COMMENT '프로젝트 ID',
`work_type_id` int(11) NOT NULL COMMENT '작업 유형 ID',
`work_status_id` int(11) DEFAULT 1 COMMENT '업무 상태 ID (1:정규, 2:에러)',
`error_type_id` int(11) DEFAULT NULL COMMENT '에러 유형 ID (에러일 때만)',
`work_hours` decimal(4,2) NOT NULL COMMENT '작업 시간',
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`created_by` int(11) NOT NULL DEFAULT 1 COMMENT '작성자 user_id',
`updated_by` int(11) DEFAULT NULL COMMENT '수정자 user_id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
---
## Attendance Management Tables
*Source: `create-attendance-tables.js`*
### `work_attendance_types`
```sql
CREATE TABLE IF NOT EXISTS `work_attendance_types` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`type_code` VARCHAR(20) NOT NULL UNIQUE COMMENT '근로 유형 코드',
`type_name` VARCHAR(50) NOT NULL COMMENT '근로 유형명',
`description` TEXT COMMENT '설명',
`is_active` BOOLEAN DEFAULT TRUE COMMENT '활성 상태',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) COMMENT='근로 유형 관리 테이블';
```
### `vacation_types`
```sql
CREATE TABLE IF NOT EXISTS `vacation_types` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`type_code` VARCHAR(20) NOT NULL UNIQUE COMMENT '휴가 유형 코드',
`type_name` VARCHAR(50) NOT NULL COMMENT '휴가 유형명',
`hours_deduction` DECIMAL(4,2) NOT NULL COMMENT '차감 시간',
`description` TEXT COMMENT '설명',
`is_active` BOOLEAN DEFAULT TRUE COMMENT '활성 상태',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) COMMENT='휴가 유형 관리 테이블';
```
### `daily_attendance_records`
```sql
CREATE TABLE IF NOT EXISTS `daily_attendance_records` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`record_date` DATE NOT NULL COMMENT '기록 날짜',
`worker_id` INT NOT NULL COMMENT '작업자 ID',
`total_work_hours` DECIMAL(4,2) DEFAULT 0 COMMENT '총 작업 시간',
`attendance_type_id` INT COMMENT '근로 유형 ID',
`vacation_type_id` INT NULL COMMENT '휴가 유형 ID',
`is_vacation_processed` BOOLEAN DEFAULT FALSE COMMENT '휴가 처리 여부',
`overtime_approved` BOOLEAN DEFAULT FALSE COMMENT '초과근무 승인 여부',
`overtime_approved_by` INT NULL COMMENT '초과근무 승인자 ID',
`overtime_approved_at` TIMESTAMP NULL COMMENT '초과근무 승인 시간',
`status` ENUM('incomplete', 'partial', 'complete', 'overtime', 'vacation', 'error') DEFAULT 'incomplete' COMMENT '상태',
`notes` TEXT COMMENT '비고',
`created_by` INT NOT NULL DEFAULT 1 COMMENT '생성자 ID',
`updated_by` INT NULL COMMENT '수정자 ID',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `unique_worker_date` (`worker_id`, `record_date`)
) COMMENT='일일 근태 기록 테이블';
```
### `worker_vacation_balance`
```sql
CREATE TABLE IF NOT EXISTS `worker_vacation_balance` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`worker_id` INT NOT NULL COMMENT '작업자 ID',
`year` YEAR NOT NULL COMMENT '연도',
`total_annual_leave` DECIMAL(4,2) DEFAULT 15.0 COMMENT '연간 총 연차 (일)',
`used_annual_leave` DECIMAL(4,2) DEFAULT 0 COMMENT '사용한 연차 (일)',
`remaining_annual_leave` DECIMAL(4,2) GENERATED ALWAYS AS (total_annual_leave - used_annual_leave) STORED COMMENT '잔여 연차 (일)',
`notes` TEXT COMMENT '비고',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `unique_worker_year` (`worker_id`, `year`)
) COMMENT='작업자별 휴가 잔여 관리 테이블';
```
---
## Legacy & Misc Tables
### `Tools`
*Source: Inferred from `models/toolsModel.js`*
```sql
CREATE TABLE `Tools` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
`stock` int(11) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`factory_id` int(11) DEFAULT NULL,
`map_x` decimal(10, 6) DEFAULT NULL,
`map_y` decimal(10, 6) DEFAULT NULL,
`map_zone` varchar(255) DEFAULT NULL,
`map_note` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
### `DailyIssueReports`
*Source: `hyungi.sql`*
```sql
CREATE TABLE `DailyIssueReports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`worker_id` int(11) NOT NULL,
`project_id` int(11) NOT NULL,
`issue_type_id` int(11) DEFAULT NULL,
`description` text DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`start_time` time NOT NULL,
`end_time` time NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
```
### `IssueTypes`
*Source: `hyungi.sql`*
```sql
CREATE TABLE `IssueTypes` (
`issue_type_id` int(11) NOT NULL AUTO_INCREMENT,
`category` varchar(100) NOT NULL,
`subcategory` varchar(100) NOT NULL,
PRIMARY KEY (`issue_type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
```
### `work_types`
*Source: `hyungi.sql`*
```sql
CREATE TABLE `work_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL COMMENT '작업 유형명',
`description` text DEFAULT NULL COMMENT '작업 유형 설명',
`category` varchar(50) DEFAULT NULL COMMENT '작업 카테고리',
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
### `work_status_types`
*Source: `hyungi.sql`*
```sql
CREATE TABLE `work_status_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL COMMENT '상태명',
`description` text DEFAULT NULL COMMENT '상태 설명',
`is_error` tinyint(1) DEFAULT 0 COMMENT '에러 상태 여부',
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
### `error_types`
*Source: `hyungi.sql`*
```sql
CREATE TABLE `error_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL COMMENT '에러 유형명',
`description` text DEFAULT NULL COMMENT '에러 설명',
`severity` enum('low','medium','high','critical') DEFAULT 'medium' COMMENT '심각도',
`solution_guide` text DEFAULT NULL COMMENT '해결 가이드',
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
### `WorkReports` (Legacy)
*Source: `hyungi.sql`*
*Note: This appears to be a legacy version of `daily_work_reports`.*
```sql
CREATE TABLE `WorkReports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`worker_id` int(11) NOT NULL,
`project_id` int(11) NOT NULL,
`morning_task_id` int(11) DEFAULT NULL,
`afternoon_task_id` int(11) DEFAULT NULL,
`overtime_hours` decimal(4,1) DEFAULT 0.0,
`overtime_task_id` int(11) DEFAULT NULL,
`work_details` text DEFAULT NULL,
`note` text DEFAULT NULL,
`memo` text DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp(),
`morning_project_id` int(11) DEFAULT NULL,
`afternoon_project_id` int(11) DEFAULT NULL,
`overtime_project_id` int(11) DEFAULT NULL,
`task_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```

View File

@@ -31,33 +31,51 @@
---
## 2025-12-11: 문서 구조 초기 생성
## 2025-12-19: Phase 2 진행 - DB 쿼리 개선 및 프론트엔드 모듈화 시작
### 변경 사항
- docs/ 디렉토리 생성
- 리팩토링 관련 문서 작성
- README.md: 문서 인덱스
- refactoring/ANALYSIS.md: 코드베이스 분석 리포트
- refactoring/PLAN.md: 리팩토링 실행 계획
- refactoring/LOG.md: 이 파일
#### 1. 백엔드 `SELECT *` 쿼리 개선
- **목표**: `SELECT *` 구문을 명시적 컬럼으로 대체하여 성능 및 명확성 향상.
- **작업 내용**: 8개의 모델 파일에서 `SELECT *`를 사용하는 모든 활성 쿼리를 리팩토링했습니다.
- `projectModel.js`
- `toolsModel.js`
- `attendanceModel.js`
- `dailyIssueReportModel.js`
- `issueTypeModel.js`
- `workReportModel.js`
- `userModel.js`
- `dailyWorkReportModel.js`
- **비고**: 사용되지 않는 레거시 함수는 사용 현황 분석 후 수정에서 제외하여 안정성을 확보했습니다.
#### 2. 프론트엔드 모듈화 시작
- **목표**: 거대 파일 `work-report-calendar.js` 분리 및 API 호출 로직 공통화.
- **`manage-project.js` 리팩토링**: `fetch` 직접 사용 대신, 기존 `api-helper.js`의 전역 함수 (`apiGet`, `apiPost` 등)를 사용하도록 통일하여 일관성을 확보했습니다.
- **`work-report-calendar.js` 모듈화**:
- `CalendarAPI.js`: API 호출 로직을 분리 및 캡슐화.
- `CalendarState.js`: 전역으로 흩어져 있던 상태 변수를 중앙에서 관리.
- `CalendarView.js`: DOM 요소 및 렌더링 관련 함수 일부를 분리.
- **비고**: `work-report-calendar.js` 파일의 크기와 복잡도로 인해 도구의 안정적인 수정이 어려워, 분리된 모듈을 생성하고 HTML에 연결하는 작업을 수행했습니다. 나머지 변환 작업은 수동으로 진행해야 합니다.
#### 3. 개발 환경 안정화 및 문서화
- **`docker-compose.yml` 수정**: `api` 컨테이너의 `MODULE_NOT_FOUND` 오류를 해결하기 위해 누락된 소스 코드 디렉토리(`config`, `middlewares` 등)를 볼륨 마운트에 추가했습니다.
- **데이터베이스 마이그레이션 추가**: `projects` 테이블에 누락된 컬럼 (`is_active` 등)을 추가하는 마이그레이션 파일을 생성하여 코드와 스키마의 불일치를 해결했습니다.
- **문서화**:
- `docs/deployment_notes.md`: 사용자 피드백을 바탕으로 테스트 및 프로덕션 배포 환경 정보를 기록했습니다.
- `docs/database/CURRENT_SCHEMA.md`: 여러 소스에 흩어져 있던 스키마 정보를 종합하여 현재 기준의 최종 스키마 정의서를 작성했습니다.
### 영향 범위
**새로 생성된 파일**:
- docs/README.md
- docs/refactoring/ANALYSIS.md
- docs/refactoring/PLAN.md
- docs/refactoring/LOG.md
- **수정된 파일**: `docker-compose.yml`, 8개 모델 파일, 3개 프론트엔드 JS 파일, 1개 HTML 파일
- **추가된 파일**: `docs/deployment_notes.md`, `docs/database/CURRENT_SCHEMA.md`, 1개 마이그레이션 파일, 3개 JS 모듈 파일
- **영향받는 기능**: 데이터베이스 조회 성능, API 서버 안정성, 프론트엔드 코드 구조, 프로젝트 문서
### 테스트
- [x] 문서 구조 검토
- [x] 마크다운 형식 확인
- [x] Docker 컨테이너 재빌드 및 정상 실행 확인.
- [ ] 리팩토링된 API 및 프론트엔드 기능의 전체적인 수동 테스트 필요.
- [ ] 데이터베이스 마이그레이션은 DB 연결 문제로 실행하지 못했으나, 코드와 스키마 불일치를 해결하는 방향으로 작성됨.
### 관련 이슈
- 리팩토링 프로젝트 시작
### 비고
- 이후 모든 리팩토링 작업은 이 로그에 기록할 것
- 코드 변경 전후를 명확히 문서화
- Phase 2 리팩토링 계획 ([docs/refactoring/PLAN.md](PLAN.md))
---
@@ -198,27 +216,27 @@ git commit -m "refactor: Phase 1 - 긴급 보안 및 중복 제거"
## YYYY-MM-DD: [작업 제목]
### 변경 사항
-
-
### 영향 범위
**수정된 파일**:
-
-
-
-
**영향받는 기능**:
-
-
### 코드 변경
#### Before
\`\`\`javascript
````javascript
// 변경 전 코드
\`\`\`
````
#### After
\`\`\`javascript
````javascript
// 변경 후 코드
\`\`\`
````
### 테스트
- [ ] 단위 테스트 통과
@@ -232,15 +250,15 @@ git commit -m "refactor: Phase 1 - 긴급 보안 및 중복 제거"
- 개선율:
### 관련 이슈
-
-
### 비고
-
-
### Git Commit
\`\`\`bash
````bash
git commit -m "refactor: [커밋 메시지]"
\`\`\`
````
---
```
@@ -303,4 +321,4 @@ git commit -m "refactor: [커밋 메시지]"
---
*마지막 업데이트: 2025-12-11*
*마지막 업데이트: 2025-12-11*