11개 모델 파일의 171개 콜백 메서드를 직접 return/throw 패턴으로 변환. 8개 컨트롤러에서 new Promise 래퍼와 중첩 콜백 제거, console.error→logger.error 교체. 미사용 pageAccessModel.js 삭제. 전체 -3,600줄 감소. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
373 lines
10 KiB
JavaScript
373 lines
10 KiB
JavaScript
/**
|
|
* 작업장 관리 컨트롤러
|
|
*
|
|
* 작업장 카테고리(공장) 및 작업장 CRUD API 엔드포인트 핸들러
|
|
*
|
|
* @author TK-FB-Project
|
|
* @since 2026-01-26
|
|
*/
|
|
|
|
const workplaceModel = require('../models/workplaceModel');
|
|
const { ValidationError, NotFoundError } = require('../utils/errors');
|
|
const { asyncHandler } = require('../middlewares/errorHandler');
|
|
const logger = require('../utils/logger');
|
|
|
|
// ==================== 카테고리(공장) 관련 ====================
|
|
|
|
exports.createCategory = asyncHandler(async (req, res) => {
|
|
const categoryData = req.body;
|
|
|
|
if (!categoryData.category_name) {
|
|
throw new ValidationError('카테고리명은 필수 입력 항목입니다');
|
|
}
|
|
|
|
logger.info('카테고리 생성 요청', { name: categoryData.category_name });
|
|
|
|
const id = await workplaceModel.createCategory(categoryData);
|
|
|
|
logger.info('카테고리 생성 성공', { category_id: id });
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
data: { category_id: id },
|
|
message: '카테고리가 성공적으로 생성되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.getAllCategories = asyncHandler(async (req, res) => {
|
|
const rows = await workplaceModel.getAllCategories();
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows,
|
|
message: '카테고리 목록 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.getActiveCategories = asyncHandler(async (req, res) => {
|
|
const rows = await workplaceModel.getActiveCategories();
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows,
|
|
message: '활성 카테고리 목록 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.getCategoryById = asyncHandler(async (req, res) => {
|
|
const categoryId = req.params.id;
|
|
const category = await workplaceModel.getCategoryById(categoryId);
|
|
|
|
if (!category) {
|
|
throw new NotFoundError('카테고리를 찾을 수 없습니다');
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: category,
|
|
message: '카테고리 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.updateCategory = asyncHandler(async (req, res) => {
|
|
const categoryId = req.params.id;
|
|
const categoryData = req.body;
|
|
|
|
if (!categoryData.category_name) {
|
|
throw new ValidationError('카테고리명은 필수 입력 항목입니다');
|
|
}
|
|
|
|
logger.info('카테고리 수정 요청', { category_id: categoryId });
|
|
|
|
const existingCategory = await workplaceModel.getCategoryById(categoryId);
|
|
if (!existingCategory) {
|
|
throw new NotFoundError('카테고리를 찾을 수 없습니다');
|
|
}
|
|
|
|
const updateData = {
|
|
...categoryData,
|
|
layout_image: (categoryData.layout_image !== undefined && categoryData.layout_image !== null)
|
|
? categoryData.layout_image
|
|
: existingCategory.layout_image
|
|
};
|
|
|
|
await workplaceModel.updateCategory(categoryId, updateData);
|
|
|
|
logger.info('카테고리 수정 성공', { category_id: categoryId });
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '카테고리가 성공적으로 수정되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.deleteCategory = asyncHandler(async (req, res) => {
|
|
const categoryId = req.params.id;
|
|
|
|
logger.info('카테고리 삭제 요청', { category_id: categoryId });
|
|
|
|
await workplaceModel.deleteCategory(categoryId);
|
|
|
|
logger.info('카테고리 삭제 성공', { category_id: categoryId });
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '카테고리가 성공적으로 삭제되었습니다'
|
|
});
|
|
});
|
|
|
|
// ==================== 작업장 관련 ====================
|
|
|
|
exports.createWorkplace = asyncHandler(async (req, res) => {
|
|
const workplaceData = req.body;
|
|
|
|
if (!workplaceData.workplace_name) {
|
|
throw new ValidationError('작업장명은 필수 입력 항목입니다');
|
|
}
|
|
|
|
logger.info('작업장 생성 요청', { name: workplaceData.workplace_name });
|
|
|
|
const id = await workplaceModel.createWorkplace(workplaceData);
|
|
|
|
logger.info('작업장 생성 성공', { workplace_id: id });
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
data: { workplace_id: id },
|
|
message: '작업장이 성공적으로 생성되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.getAllWorkplaces = asyncHandler(async (req, res) => {
|
|
const categoryId = req.query.category_id;
|
|
|
|
const rows = categoryId
|
|
? await workplaceModel.getWorkplacesByCategory(categoryId)
|
|
: await workplaceModel.getAllWorkplaces();
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows,
|
|
message: '작업장 목록 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.getActiveWorkplaces = asyncHandler(async (req, res) => {
|
|
const rows = await workplaceModel.getActiveWorkplaces();
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows,
|
|
message: '활성 작업장 목록 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.getWorkplaceById = asyncHandler(async (req, res) => {
|
|
const workplaceId = req.params.id;
|
|
const workplace = await workplaceModel.getWorkplaceById(workplaceId);
|
|
|
|
if (!workplace) {
|
|
throw new NotFoundError('작업장을 찾을 수 없습니다');
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: workplace,
|
|
message: '작업장 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.updateWorkplace = asyncHandler(async (req, res) => {
|
|
const workplaceId = req.params.id;
|
|
const workplaceData = req.body;
|
|
|
|
if (!workplaceData.workplace_name) {
|
|
throw new ValidationError('작업장명은 필수 입력 항목입니다');
|
|
}
|
|
|
|
logger.info('작업장 수정 요청', { workplace_id: workplaceId });
|
|
|
|
const existingWorkplace = await workplaceModel.getWorkplaceById(workplaceId);
|
|
if (!existingWorkplace) {
|
|
throw new NotFoundError('작업장을 찾을 수 없습니다');
|
|
}
|
|
|
|
const updateData = {
|
|
...workplaceData,
|
|
layout_image: (workplaceData.layout_image !== undefined && workplaceData.layout_image !== null)
|
|
? workplaceData.layout_image
|
|
: existingWorkplace.layout_image
|
|
};
|
|
|
|
await workplaceModel.updateWorkplace(workplaceId, updateData);
|
|
|
|
logger.info('작업장 수정 성공', { workplace_id: workplaceId });
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '작업장이 성공적으로 수정되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.deleteWorkplace = asyncHandler(async (req, res) => {
|
|
const workplaceId = req.params.id;
|
|
|
|
logger.info('작업장 삭제 요청', { workplace_id: workplaceId });
|
|
|
|
await workplaceModel.deleteWorkplace(workplaceId);
|
|
|
|
logger.info('작업장 삭제 성공', { workplace_id: workplaceId });
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '작업장이 성공적으로 삭제되었습니다'
|
|
});
|
|
});
|
|
|
|
// ==================== 작업장 지도 영역 관련 ====================
|
|
|
|
exports.uploadCategoryLayoutImage = asyncHandler(async (req, res) => {
|
|
const categoryId = req.params.id;
|
|
|
|
if (!req.file) {
|
|
throw new ValidationError('이미지 파일이 필요합니다');
|
|
}
|
|
|
|
const imagePath = `/uploads/${req.file.filename}`;
|
|
|
|
logger.info('카테고리 레이아웃 이미지 업로드 요청', { category_id: categoryId, path: imagePath });
|
|
|
|
const category = await workplaceModel.getCategoryById(categoryId);
|
|
if (!category) {
|
|
throw new NotFoundError('카테고리를 찾을 수 없습니다');
|
|
}
|
|
|
|
const updatedData = {
|
|
category_name: category.category_name,
|
|
description: category.description,
|
|
display_order: category.display_order,
|
|
is_active: category.is_active,
|
|
layout_image: imagePath
|
|
};
|
|
|
|
await workplaceModel.updateCategory(categoryId, updatedData);
|
|
|
|
logger.info('레이아웃 이미지 업로드 성공', { category_id: categoryId });
|
|
|
|
res.json({
|
|
success: true,
|
|
data: { image_path: imagePath },
|
|
message: '레이아웃 이미지가 성공적으로 업로드되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.uploadWorkplaceLayoutImage = asyncHandler(async (req, res) => {
|
|
const workplaceId = req.params.id;
|
|
|
|
if (!req.file) {
|
|
throw new ValidationError('이미지 파일이 필요합니다');
|
|
}
|
|
|
|
const imagePath = `/uploads/${req.file.filename}`;
|
|
|
|
logger.info('작업장 레이아웃 이미지 업로드 요청', { workplace_id: workplaceId, path: imagePath });
|
|
|
|
const workplace = await workplaceModel.getWorkplaceById(workplaceId);
|
|
if (!workplace) {
|
|
throw new NotFoundError('작업장을 찾을 수 없습니다');
|
|
}
|
|
|
|
const updatedData = {
|
|
workplace_name: workplace.workplace_name,
|
|
category_id: workplace.category_id,
|
|
description: workplace.description,
|
|
workplace_purpose: workplace.workplace_purpose,
|
|
display_priority: workplace.display_priority,
|
|
is_active: workplace.is_active,
|
|
layout_image: imagePath
|
|
};
|
|
|
|
await workplaceModel.updateWorkplace(workplaceId, updatedData);
|
|
|
|
logger.info('작업장 레이아웃 이미지 업로드 성공', { workplace_id: workplaceId });
|
|
|
|
res.json({
|
|
success: true,
|
|
data: { image_path: imagePath },
|
|
message: '작업장 레이아웃 이미지가 성공적으로 업로드되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.createMapRegion = asyncHandler(async (req, res) => {
|
|
const regionData = req.body;
|
|
|
|
if (!regionData.workplace_id || !regionData.category_id) {
|
|
throw new ValidationError('작업장 ID와 카테고리 ID는 필수 입력 항목입니다');
|
|
}
|
|
|
|
logger.info('지도 영역 생성 요청', { workplace_id: regionData.workplace_id });
|
|
|
|
const id = await workplaceModel.createMapRegion(regionData);
|
|
|
|
logger.info('지도 영역 생성 성공', { region_id: id });
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
data: { region_id: id },
|
|
message: '지도 영역이 성공적으로 생성되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.getMapRegionsByCategory = asyncHandler(async (req, res) => {
|
|
const categoryId = req.params.categoryId;
|
|
const rows = await workplaceModel.getMapRegionsByCategory(categoryId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows,
|
|
message: '지도 영역 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.getMapRegionByWorkplace = asyncHandler(async (req, res) => {
|
|
const workplaceId = req.params.workplaceId;
|
|
const region = await workplaceModel.getMapRegionByWorkplace(workplaceId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: region,
|
|
message: '지도 영역 조회 성공'
|
|
});
|
|
});
|
|
|
|
exports.updateMapRegion = asyncHandler(async (req, res) => {
|
|
const regionId = req.params.id;
|
|
const regionData = req.body;
|
|
|
|
logger.info('지도 영역 수정 요청', { region_id: regionId });
|
|
|
|
await workplaceModel.updateMapRegion(regionId, regionData);
|
|
|
|
logger.info('지도 영역 수정 성공', { region_id: regionId });
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '지도 영역이 성공적으로 수정되었습니다'
|
|
});
|
|
});
|
|
|
|
exports.deleteMapRegion = asyncHandler(async (req, res) => {
|
|
const regionId = req.params.id;
|
|
|
|
logger.info('지도 영역 삭제 요청', { region_id: regionId });
|
|
|
|
await workplaceModel.deleteMapRegion(regionId);
|
|
|
|
logger.info('지도 영역 삭제 성공', { region_id: regionId });
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '지도 영역이 성공적으로 삭제되었습니다'
|
|
});
|
|
});
|