refactor: worker_id → user_id 전체 마이그레이션 (Phase 1-4)

sso_users.user_id를 단일 식별자로 통합. JWT에서 worker_id 제거,
department_id/is_production 추가. 백엔드 15개 모델, 11개 컨트롤러,
4개 서비스, 7개 라우트, 프론트엔드 32+ JS/11+ HTML 변환.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-03-05 13:13:10 +09:00
parent 2197cdb3d5
commit abd7564e6b
90 changed files with 1790 additions and 925 deletions

View File

@@ -39,22 +39,22 @@ const getAllErrorTypes = async () => {
* 누적 추가 전용 함수 (createDailyReport 대체) - 절대 삭제 안함!
*/
const createDailyReport = async (reportData) => {
const { report_date, worker_id, work_entries, created_by, created_by_name, total_hours } = reportData;
const { report_date, user_id, work_entries, created_by, created_by_name, total_hours } = reportData;
const db = await getDb();
const conn = await db.getConnection();
try {
await conn.beginTransaction();
console.log(`${created_by_name}${report_date} ${worker_id}번 작업자에게 데이터 추가 중...`);
console.log(`${created_by_name}${report_date} ${user_id}번 작업자에게 데이터 추가 중...`);
const [existingReports] = await conn.query(
`SELECT dwr.created_by, u.name as created_by_name, COUNT(*) as count, SUM(dwr.work_hours) as total_hours
FROM daily_work_reports dwr
LEFT JOIN users u ON dwr.created_by = u.user_id
WHERE dwr.report_date = ? AND dwr.worker_id = ?
LEFT JOIN sso_users u ON dwr.created_by = u.user_id
WHERE dwr.report_date = ? AND dwr.user_id = ?
GROUP BY dwr.created_by`,
[report_date, worker_id]
[report_date, user_id]
);
console.log('기존 데이터 (삭제하지 않음):', existingReports);
@@ -66,9 +66,9 @@ const createDailyReport = async (reportData) => {
const [insertResult] = await conn.query(
`INSERT INTO daily_work_reports
(report_date, worker_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_by, created_at)
(report_date, user_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_by, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())`,
[report_date, worker_id, project_id, work_type_id, work_status_id || 1, error_type_id || null, work_hours, created_by]
[report_date, user_id, project_id, work_type_id, work_status_id || 1, error_type_id || null, work_hours, created_by]
);
insertedIds.push(insertResult.insertId);
@@ -77,10 +77,10 @@ const createDailyReport = async (reportData) => {
const [finalReports] = await conn.query(
`SELECT dwr.created_by, u.name as created_by_name, COUNT(*) as count, SUM(dwr.work_hours) as total_hours
FROM daily_work_reports dwr
LEFT JOIN users u ON dwr.created_by = u.user_id
WHERE dwr.report_date = ? AND dwr.worker_id = ?
LEFT JOIN sso_users u ON dwr.created_by = u.user_id
WHERE dwr.report_date = ? AND dwr.user_id = ?
GROUP BY dwr.created_by`,
[report_date, worker_id]
[report_date, user_id]
);
const grandTotal = finalReports.reduce((sum, report) => sum + parseFloat(report.total_hours || 0), 0);
@@ -103,7 +103,7 @@ const createDailyReport = async (reportData) => {
insertedIds[0] || null,
JSON.stringify({
report_date,
worker_id,
user_id,
work_entries_count: work_entries.length,
added_hours: total_hours,
my_total: myTotal,
@@ -123,7 +123,7 @@ const createDailyReport = async (reportData) => {
// 근태 기록 동기화
try {
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(worker_id, report_date);
await AttendanceModel.syncWithWorkReports(user_id, report_date);
} catch (syncErr) {
console.error('근태 기록 동기화 실패:', syncErr);
}
@@ -153,7 +153,7 @@ const createDailyReport = async (reportData) => {
/**
* 특정 날짜 + 작업자 + 작성자의 누적 현황 조회
*/
const getMyAccumulatedHours = async (date, worker_id, created_by) => {
const getMyAccumulatedHours = async (date, user_id, created_by) => {
const db = await getDb();
const sql = `
@@ -166,32 +166,32 @@ const getMyAccumulatedHours = async (date, worker_id, created_by) => {
) as my_entries
FROM daily_work_reports dwr
LEFT JOIN projects p ON dwr.project_id = p.project_id
WHERE dwr.report_date = ? AND dwr.worker_id = ? AND dwr.created_by = ?
WHERE dwr.report_date = ? AND dwr.user_id = ? AND dwr.created_by = ?
`;
const [rows] = await db.query(sql, [date, worker_id, created_by]);
const [rows] = await db.query(sql, [date, user_id, created_by]);
return rows[0] || { my_total_hours: 0, my_entry_count: 0, my_entries: null };
};
/**
* 누적 현황 조회 - 날짜+작업자별 (모든 기여자)
*/
const getAccumulatedReportsByDate = async (date, worker_id) => {
const getAccumulatedReportsByDate = async (date, user_id) => {
const db = await getDb();
const sql = getSelectQuery() + `
WHERE dwr.report_date = ? AND dwr.worker_id = ?
WHERE dwr.report_date = ? AND dwr.user_id = ?
ORDER BY dwr.created_by, dwr.created_at ASC
`;
const [rows] = await db.query(sql, [date, worker_id]);
const [rows] = await db.query(sql, [date, user_id]);
return rows;
};
/**
* 기여자별 요약 조회
*/
const getContributorsByDate = async (date, worker_id) => {
const getContributorsByDate = async (date, user_id) => {
const db = await getDb();
const sql = `
@@ -207,14 +207,14 @@ const getContributorsByDate = async (date, worker_id) => {
ORDER BY dwr.created_at SEPARATOR ', '
) as entry_details
FROM daily_work_reports dwr
LEFT JOIN users u ON dwr.created_by = u.user_id
LEFT JOIN sso_users u ON dwr.created_by = u.user_id
LEFT JOIN projects p ON dwr.project_id = p.project_id
WHERE dwr.report_date = ? AND dwr.worker_id = ?
WHERE dwr.report_date = ? AND dwr.user_id = ?
GROUP BY dwr.created_by
ORDER BY total_hours DESC, first_entry ASC
`;
const [rows] = await db.query(sql, [date, worker_id]);
const [rows] = await db.query(sql, [date, user_id]);
return rows;
};
@@ -232,9 +232,9 @@ const removeSpecificEntry = async (entry_id, deleted_by) => {
const [entryInfo] = await conn.query(
`SELECT dwr.*, w.worker_name, p.project_name, u.name as created_by_name
FROM daily_work_reports dwr
LEFT JOIN workers w ON dwr.worker_id = w.worker_id
LEFT JOIN workers w ON dwr.user_id = w.user_id
LEFT JOIN projects p ON dwr.project_id = p.project_id
LEFT JOIN users u ON dwr.created_by = u.user_id
LEFT JOIN sso_users u ON dwr.created_by = u.user_id
WHERE dwr.id = ?`,
[entry_id]
);
@@ -282,7 +282,7 @@ const getSelectQuery = () => `
SELECT
dwr.id,
dwr.report_date,
dwr.worker_id,
dwr.user_id,
dwr.project_id,
dwr.work_type_id,
dwr.work_status_id,
@@ -299,13 +299,13 @@ const getSelectQuery = () => `
dwr.created_at,
dwr.updated_at
FROM daily_work_reports dwr
LEFT JOIN workers w ON dwr.worker_id = w.worker_id
LEFT JOIN workers w ON dwr.user_id = w.user_id
LEFT JOIN projects p ON dwr.project_id = p.project_id
LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
LEFT JOIN work_status_types wst ON dwr.work_status_id = wst.id
LEFT JOIN issue_report_items iri ON dwr.error_type_id = iri.item_id
LEFT JOIN issue_report_categories irc ON iri.category_id = irc.category_id
LEFT JOIN users u ON dwr.created_by = u.user_id
LEFT JOIN sso_users u ON dwr.created_by = u.user_id
`;
/**
@@ -347,26 +347,26 @@ const getByDateAndCreator = async (date, created_by) => {
/**
* 10. 일일 작업보고서 조회 (작업자별)
*/
const getByWorker = async (worker_id) => {
const getByWorker = async (user_id) => {
const db = await getDb();
const sql = getSelectQuery() + `
WHERE dwr.worker_id = ?
WHERE dwr.user_id = ?
ORDER BY dwr.report_date DESC, dwr.id ASC
`;
const [rows] = await db.query(sql, [worker_id]);
const [rows] = await db.query(sql, [user_id]);
return rows;
};
/**
* 11. 일일 작업보고서 조회 (날짜 + 작업자)
*/
const getByDateAndWorker = async (date, worker_id) => {
const getByDateAndWorker = async (date, user_id) => {
const db = await getDb();
const sql = getSelectQuery() + `
WHERE dwr.report_date = ? AND dwr.worker_id = ?
WHERE dwr.report_date = ? AND dwr.user_id = ?
ORDER BY dwr.id ASC
`;
const [rows] = await db.query(sql, [date, worker_id]);
const [rows] = await db.query(sql, [date, user_id]);
return rows;
};
@@ -387,7 +387,7 @@ const getByRange = async (start_date, end_date) => {
* 13. 상세 검색 (페이지네이션 포함)
*/
const searchWithDetails = async (params) => {
const { start_date, end_date, worker_id, project_id, work_status_id, created_by, page, limit } = params;
const { start_date, end_date, user_id, project_id, work_status_id, created_by, page, limit } = params;
const db = await getDb();
@@ -395,9 +395,9 @@ const searchWithDetails = async (params) => {
let whereConditions = ['dwr.report_date BETWEEN ? AND ?'];
let queryParams = [start_date, end_date];
if (worker_id) {
whereConditions.push('dwr.worker_id = ?');
queryParams.push(worker_id);
if (user_id) {
whereConditions.push('dwr.user_id = ?');
queryParams.push(user_id);
}
if (project_id) {
@@ -448,16 +448,16 @@ const getSummaryByDate = async (date) => {
const sql = `
SELECT
dwr.worker_id,
dwr.user_id,
w.worker_name,
dwr.report_date,
SUM(dwr.work_hours) as total_hours,
COUNT(*) as work_entries_count,
SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count
FROM daily_work_reports dwr
LEFT JOIN workers w ON dwr.worker_id = w.worker_id
LEFT JOIN workers w ON dwr.user_id = w.user_id
WHERE dwr.report_date = ?
GROUP BY dwr.worker_id, dwr.report_date
GROUP BY dwr.user_id, dwr.report_date
ORDER BY w.worker_name ASC
`;
const [rows] = await db.query(sql, [date]);
@@ -467,24 +467,24 @@ const getSummaryByDate = async (date) => {
/**
* 15. 일일 근무 요약 조회 (작업자별)
*/
const getSummaryByWorker = async (worker_id) => {
const getSummaryByWorker = async (user_id) => {
const db = await getDb();
const sql = `
SELECT
dwr.report_date,
dwr.worker_id,
dwr.user_id,
w.worker_name,
SUM(dwr.work_hours) as total_hours,
COUNT(*) as work_entries_count,
SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count
FROM daily_work_reports dwr
LEFT JOIN workers w ON dwr.worker_id = w.worker_id
WHERE dwr.worker_id = ?
GROUP BY dwr.report_date, dwr.worker_id
LEFT JOIN workers w ON dwr.user_id = w.user_id
WHERE dwr.user_id = ?
GROUP BY dwr.report_date, dwr.user_id
ORDER BY dwr.report_date DESC
`;
const [rows] = await db.query(sql, [worker_id]);
const [rows] = await db.query(sql, [user_id]);
return rows;
};
@@ -499,7 +499,7 @@ const getMonthlySummary = async (year, month) => {
const sql = `
SELECT
dwr.report_date,
dwr.worker_id,
dwr.user_id,
w.worker_name,
SUM(dwr.work_hours) as total_work_hours,
COUNT(DISTINCT dwr.project_id) as project_count,
@@ -508,11 +508,11 @@ const getMonthlySummary = async (year, month) => {
GROUP_CONCAT(DISTINCT p.project_name ORDER BY p.project_name) as projects,
GROUP_CONCAT(DISTINCT wt.name ORDER BY wt.name) as work_types
FROM daily_work_reports dwr
LEFT JOIN workers w ON dwr.worker_id = w.worker_id
LEFT JOIN workers w ON dwr.user_id = w.user_id
LEFT JOIN projects p ON dwr.project_id = p.project_id
LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
WHERE dwr.report_date BETWEEN ? AND ?
GROUP BY dwr.report_date, dwr.worker_id
GROUP BY dwr.report_date, dwr.user_id
ORDER BY dwr.report_date DESC, w.worker_name ASC
`;
const [rows] = await db.query(sql, [start, end]);
@@ -567,11 +567,11 @@ const updateById = async (id, updateData) => {
// [Sync] 근태 기록 동기화
try {
const [targetReport] = await db.query('SELECT worker_id, report_date FROM daily_work_reports WHERE id = ?', [id]);
const [targetReport] = await db.query('SELECT user_id, report_date FROM daily_work_reports WHERE id = ?', [id]);
if (targetReport.length > 0) {
const { worker_id, report_date } = targetReport[0];
const { user_id, report_date } = targetReport[0];
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(worker_id, report_date);
await AttendanceModel.syncWithWorkReports(user_id, report_date);
}
} catch (syncErr) {
console.error('근태 기록 동기화 실패 (Update):', syncErr);
@@ -615,9 +615,9 @@ const removeById = async (id, deletedBy) => {
// [Sync] 근태 기록 동기화
if (reportInfo.length > 0) {
try {
const { worker_id, report_date } = reportInfo[0];
const { user_id, report_date } = reportInfo[0];
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(worker_id, report_date);
await AttendanceModel.syncWithWorkReports(user_id, report_date);
} catch (syncErr) {
console.error('근태 기록 동기화 실패 (Delete):', syncErr);
}
@@ -635,7 +635,7 @@ const removeById = async (id, deletedBy) => {
/**
* 19. 작업자의 특정 날짜 전체 삭제
*/
const removeByDateAndWorker = async (date, worker_id, deletedBy) => {
const removeByDateAndWorker = async (date, user_id, deletedBy) => {
const db = await getDb();
const conn = await db.getConnection();
@@ -644,14 +644,14 @@ const removeByDateAndWorker = async (date, worker_id, deletedBy) => {
// 삭제 전 정보 저장 (감사 로그용)
const [reportInfos] = await conn.query(
'SELECT id, report_date, worker_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_at, updated_at, created_by, updated_by FROM daily_work_reports WHERE report_date = ? AND worker_id = ?',
[date, worker_id]
'SELECT id, report_date, user_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_at, updated_at, created_by, updated_by FROM daily_work_reports WHERE report_date = ? AND user_id = ?',
[date, user_id]
);
// 작업보고서 삭제
const [result] = await conn.query(
'DELETE FROM daily_work_reports WHERE report_date = ? AND worker_id = ?',
[date, worker_id]
'DELETE FROM daily_work_reports WHERE report_date = ? AND user_id = ?',
[date, user_id]
);
// 감사 로그 추가
@@ -673,7 +673,7 @@ const removeByDateAndWorker = async (date, worker_id, deletedBy) => {
// [Sync] 근태 기록 동기화
try {
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(worker_id, date);
await AttendanceModel.syncWithWorkReports(user_id, date);
} catch (syncErr) {
console.error('근태 기록 동기화 실패 (Batch Delete):', syncErr);
}
@@ -697,7 +697,7 @@ const getStatistics = async (start_date, end_date) => {
SELECT
COUNT(*) as total_reports,
SUM(work_hours) as total_hours,
COUNT(DISTINCT worker_id) as unique_workers,
COUNT(DISTINCT user_id) as unique_workers,
COUNT(DISTINCT project_id) as unique_projects
FROM daily_work_reports
WHERE report_date BETWEEN ? AND ?
@@ -708,7 +708,7 @@ const getStatistics = async (start_date, end_date) => {
SELECT
report_date,
SUM(work_hours) as daily_hours,
COUNT(DISTINCT worker_id) as daily_workers
COUNT(DISTINCT user_id) as daily_workers
FROM daily_work_reports
WHERE report_date BETWEEN ? AND ?
GROUP BY report_date
@@ -727,7 +727,7 @@ const getStatistics = async (start_date, end_date) => {
* @param {object} modelData - 서비스 레이어에서 전달된 데이터
* @returns {Promise<object>} 삽입된 항목의 ID 배열과 개수
*/
const createReportEntries = async ({ report_date, worker_id, entries }) => {
const createReportEntries = async ({ report_date, user_id, entries }) => {
const db = await getDb();
const conn = await db.getConnection();
try {
@@ -736,7 +736,7 @@ const createReportEntries = async ({ report_date, worker_id, entries }) => {
const insertedIds = [];
const sql = `
INSERT INTO daily_work_reports
(report_date, worker_id, project_id, work_type_id, work_hours, work_status_id, error_type_id, created_by)
(report_date, user_id, project_id, work_type_id, work_hours, work_status_id, error_type_id, created_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`;
@@ -744,7 +744,7 @@ const createReportEntries = async ({ report_date, worker_id, entries }) => {
const { project_id, work_type_id, work_hours, work_status_id, error_type_id, created_by } = entry;
const [result] = await conn.query(sql, [
report_date,
worker_id,
user_id,
project_id,
work_type_id,
work_hours,
@@ -760,7 +760,7 @@ const createReportEntries = async ({ report_date, worker_id, entries }) => {
// [Sync] 근태 기록 동기화
try {
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(worker_id, report_date);
await AttendanceModel.syncWithWorkReports(user_id, report_date);
} catch (syncErr) {
console.error('근태 기록 동기화 실패 (V2 Create):', syncErr);
}
@@ -788,7 +788,7 @@ const getSelectQueryV2 = () => `
SELECT
dwr.id,
dwr.report_date,
dwr.worker_id,
dwr.user_id,
dwr.project_id,
dwr.work_type_id,
dwr.work_status_id,
@@ -805,19 +805,19 @@ const getSelectQueryV2 = () => `
u.name as created_by_name,
dwr.created_at
FROM daily_work_reports dwr
LEFT JOIN workers w ON dwr.worker_id = w.worker_id
LEFT JOIN workers w ON dwr.user_id = w.user_id
LEFT JOIN projects p ON dwr.project_id = p.project_id
LEFT JOIN tasks t ON dwr.work_type_id = t.task_id
LEFT JOIN work_types wt ON t.work_type_id = wt.id
LEFT JOIN work_status_types wst ON dwr.work_status_id = wst.id
LEFT JOIN issue_report_items iri ON dwr.error_type_id = iri.item_id
LEFT JOIN issue_report_categories irc ON iri.category_id = irc.category_id
LEFT JOIN users u ON dwr.created_by = u.user_id
LEFT JOIN sso_users u ON dwr.created_by = u.user_id
`;
/**
* [V2] 옵션 기반으로 작업 보고서를 조회합니다.
* @param {object} options - 조회 조건 (date, worker_id, created_by_user_id 등)
* @param {object} options - 조회 조건 (date, user_id, created_by_user_id 등)
* @returns {Promise<Array>} 조회된 작업 보고서 배열
*/
const getReportsWithOptions = async (options) => {
@@ -834,9 +834,9 @@ const getReportsWithOptions = async (options) => {
queryParams.push(options.start_date, options.end_date);
}
if (options.worker_id) {
whereConditions.push('dwr.worker_id = ?');
queryParams.push(options.worker_id);
if (options.user_id) {
whereConditions.push('dwr.user_id = ?');
queryParams.push(options.user_id);
}
if (options.created_by_user_id) {
whereConditions.push('dwr.created_by = ?');
@@ -890,7 +890,7 @@ const updateReportById = async (reportId, updateData) => {
// [Sync] 업데이트 전 정보 조회 (동기화를 위해)
let targetInfo = null;
try {
const [rows] = await db.query('SELECT worker_id, report_date FROM daily_work_reports WHERE id = ?', [reportId]);
const [rows] = await db.query('SELECT user_id, report_date FROM daily_work_reports WHERE id = ?', [reportId]);
if (rows.length > 0) targetInfo = rows[0];
} catch (e) { console.warn('Sync fetch failed', e); }
@@ -902,7 +902,7 @@ const updateReportById = async (reportId, updateData) => {
if (targetInfo) {
try {
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(targetInfo.worker_id, targetInfo.report_date);
await AttendanceModel.syncWithWorkReports(targetInfo.user_id, targetInfo.report_date);
} catch (syncErr) {
console.error('근태 기록 동기화 실패 (V2 Update):', syncErr);
}
@@ -925,7 +925,7 @@ const removeReportById = async (reportId, deletedByUserId) => {
await conn.beginTransaction();
// 감사 로그를 위해 삭제 전 정보 조회
const [reportInfo] = await conn.query('SELECT id, report_date, worker_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_at, updated_at, created_by, updated_by FROM daily_work_reports WHERE id = ?', [reportId]);
const [reportInfo] = await conn.query('SELECT id, report_date, user_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_at, updated_at, created_by, updated_by FROM daily_work_reports WHERE id = ?', [reportId]);
// 실제 삭제 작업
const [result] = await conn.query('DELETE FROM daily_work_reports WHERE id = ?', [reportId]);
@@ -940,9 +940,9 @@ const removeReportById = async (reportId, deletedByUserId) => {
// [Sync] 근태 기록 동기화
if (reportInfo.length > 0) {
try {
const { worker_id, report_date } = reportInfo[0];
const { user_id, report_date } = reportInfo[0];
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(worker_id, report_date);
await AttendanceModel.syncWithWorkReports(user_id, report_date);
} catch (syncErr) {
console.error('근태 기록 동기화 실패 (V2 Delete):', syncErr);
}
@@ -1075,7 +1075,7 @@ const createFromTbmAssignment = async (reportData) => {
const {
tbm_assignment_id,
tbm_session_id,
worker_id,
user_id,
project_id,
work_type_id,
report_date,
@@ -1098,7 +1098,7 @@ const createFromTbmAssignment = async (reportData) => {
// 1. 작업보고서 생성
const sql = `
INSERT INTO daily_work_reports
(tbm_session_id, tbm_assignment_id, report_date, worker_id, project_id, work_type_id,
(tbm_session_id, tbm_assignment_id, report_date, user_id, project_id, work_type_id,
start_time, end_time, work_hours, total_hours, regular_hours, error_hours,
work_status_id, error_type_id, created_by, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
@@ -1108,7 +1108,7 @@ const createFromTbmAssignment = async (reportData) => {
tbm_session_id,
tbm_assignment_id,
report_date,
worker_id,
user_id,
project_id,
work_type_id,
start_time || null,
@@ -1150,7 +1150,7 @@ const createFromTbmAssignment = async (reportData) => {
// 4. 근태 기록 동기화
try {
const AttendanceModel = require('./attendanceModel');
await AttendanceModel.syncWithWorkReports(worker_id, report_date);
await AttendanceModel.syncWithWorkReports(user_id, report_date);
} catch (syncErr) {
console.error('근태 기록 동기화 실패 (TBM Report):', syncErr);
}