refactor: worker_id 잔재 제거 - user_id 기반으로 완전 전환

- workerModel: remove()를 user_id 기반 cascading delete로 전환
- workerController: 계정 생성/해제를 workers.user_id 연결 방식으로 변경
- userController: JOIN 방향 전환 (u.worker_id→w.worker_id 에서 w.user_id→u.user_id)
- authController, systemController, authRoutes: 모든 CRUD에서 worker_id 참조 제거
- DB: UNIQUE KEY 5개 교체, FK 7개 삭제, daily_worker_summary user_id 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-03-06 07:47:01 +09:00
parent 7089548722
commit d385ce7ac1
6 changed files with 110 additions and 150 deletions

View File

@@ -243,21 +243,20 @@ exports.getAllUsers = asyncHandler(async (req, res) => {
const db = await getDb();
const [users] = await db.query(`
SELECT
SELECT
user_id,
username,
name,
email,
role,
access_level,
worker_id,
is_active,
last_login_at,
failed_login_attempts,
locked_until,
created_at,
updated_at
FROM users
FROM users
ORDER BY created_at DESC
`);
@@ -272,20 +271,20 @@ exports.getAllUsers = asyncHandler(async (req, res) => {
* 사용자 생성
*/
exports.createUser = asyncHandler(async (req, res) => {
const { username, password, name, email, role, access_level, worker_id } = req.body;
const { username, password, name, email, role, access_level } = req.body;
// 스키마 기반 유효성 검사
validateSchema(req.body, schemas.createUser);
try {
const db = await getDb();
// 사용자명 중복 확인
const [existing] = await db.query('SELECT user_id FROM users WHERE username = ?', [username]);
if (existing.length > 0) {
throw new ApiError('이미 존재하는 사용자명입니다.', 409);
}
// 이메일 중복 확인 (이메일이 제공된 경우)
if (email) {
const [existingEmail] = await db.query('SELECT user_id FROM users WHERE email = ?', [email]);
@@ -293,15 +292,15 @@ exports.createUser = asyncHandler(async (req, res) => {
throw new ApiError('이미 사용 중인 이메일입니다.', 409);
}
}
// 비밀번호 해시화
const hashedPassword = await bcrypt.hash(password, 10);
// 사용자 생성
const [result] = await db.query(`
INSERT INTO users (username, password, name, email, role, access_level, worker_id, is_active, created_at, password_changed_at)
VALUES (?, ?, ?, ?, ?, ?, ?, 1, NOW(), NOW())
`, [username, hashedPassword, name, email || null, role, access_level || role, worker_id || null]);
INSERT INTO users (username, password, name, email, role, access_level, is_active, created_at, password_changed_at)
VALUES (?, ?, ?, ?, ?, ?, 1, NOW(), NOW())
`, [username, hashedPassword, name, email || null, role, access_level || role]);
// 비밀번호 변경 로그 기록
await db.query(`
@@ -322,9 +321,9 @@ exports.createUser = asyncHandler(async (req, res) => {
exports.updateUser = async (req, res) => {
try {
const { id } = req.params;
const { name, email, role, access_level, is_active, worker_id } = req.body;
const { name, email, role, access_level, is_active } = req.body;
const db = await getDb();
// 사용자 존재 확인
const [user] = await db.query('SELECT user_id FROM users WHERE user_id = ?', [id]);
if (user.length === 0) {
@@ -333,11 +332,11 @@ exports.updateUser = async (req, res) => {
error: '해당 사용자를 찾을 수 없습니다.'
});
}
// 이메일 중복 확인 (다른 사용자가 사용 중인지)
if (email) {
const [existingEmail] = await db.query(
'SELECT user_id FROM users WHERE email = ? AND user_id != ?',
'SELECT user_id FROM users WHERE email = ? AND user_id != ?',
[email, id]
);
if (existingEmail.length > 0) {
@@ -347,13 +346,13 @@ exports.updateUser = async (req, res) => {
});
}
}
// 사용자 정보 업데이트
await db.query(`
UPDATE users
SET name = ?, email = ?, role = ?, access_level = ?, is_active = ?, worker_id = ?, updated_at = NOW()
UPDATE users
SET name = ?, email = ?, role = ?, access_level = ?, is_active = ?, updated_at = NOW()
WHERE user_id = ?
`, [name, email || null, role, access_level || role, is_active ? 1 : 0, worker_id || null, id]);
`, [name, email || null, role, access_level || role, is_active ? 1 : 0, id]);
res.json({
success: true,