- 순찰/점검 기능 개선 (zone-detail 페이지 추가) - 출근/근태 시스템 개선 (연차 조회, 근무현황) - 작업분석 대분류 그룹화 및 마이그레이션 스크립트 - 모바일 네비게이션 UI 추가 - NAS 배포 도구 및 문서 추가 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
166 lines
4.3 KiB
JavaScript
166 lines
4.3 KiB
JavaScript
// controllers/notificationController.js
|
|
const notificationModel = require('../models/notificationModel');
|
|
|
|
const notificationController = {
|
|
// 읽지 않은 알림 조회
|
|
async getUnread(req, res) {
|
|
try {
|
|
const userId = req.user?.id || null;
|
|
const notifications = await notificationModel.getUnread(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: notifications
|
|
});
|
|
} catch (error) {
|
|
console.error('읽지 않은 알림 조회 오류:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '알림 조회 중 오류가 발생했습니다.'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 전체 알림 조회
|
|
async getAll(req, res) {
|
|
try {
|
|
const userId = req.user?.id || null;
|
|
const page = parseInt(req.query.page) || 1;
|
|
const limit = parseInt(req.query.limit) || 20;
|
|
|
|
const result = await notificationModel.getAll(userId, page, limit);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result.notifications,
|
|
pagination: {
|
|
total: result.total,
|
|
page: result.page,
|
|
limit: result.limit,
|
|
totalPages: Math.ceil(result.total / result.limit)
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('알림 목록 조회 오류:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '알림 조회 중 오류가 발생했습니다.'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 읽지 않은 알림 개수
|
|
async getUnreadCount(req, res) {
|
|
try {
|
|
const userId = req.user?.id || null;
|
|
const count = await notificationModel.getUnreadCount(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: { count }
|
|
});
|
|
} catch (error) {
|
|
console.error('알림 개수 조회 오류:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '알림 개수 조회 중 오류가 발생했습니다.'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 알림 읽음 처리
|
|
async markAsRead(req, res) {
|
|
try {
|
|
const { id } = req.params;
|
|
const success = await notificationModel.markAsRead(id);
|
|
|
|
res.json({
|
|
success,
|
|
message: success ? '알림을 읽음 처리했습니다.' : '알림을 찾을 수 없습니다.'
|
|
});
|
|
} catch (error) {
|
|
console.error('알림 읽음 처리 오류:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '알림 처리 중 오류가 발생했습니다.'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 모든 알림 읽음 처리
|
|
async markAllAsRead(req, res) {
|
|
try {
|
|
const userId = req.user?.id || null;
|
|
const count = await notificationModel.markAllAsRead(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
message: `${count}개의 알림을 읽음 처리했습니다.`,
|
|
data: { count }
|
|
});
|
|
} catch (error) {
|
|
console.error('전체 읽음 처리 오류:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '알림 처리 중 오류가 발생했습니다.'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 알림 삭제
|
|
async delete(req, res) {
|
|
try {
|
|
const { id } = req.params;
|
|
const success = await notificationModel.delete(id);
|
|
|
|
res.json({
|
|
success,
|
|
message: success ? '알림을 삭제했습니다.' : '알림을 찾을 수 없습니다.'
|
|
});
|
|
} catch (error) {
|
|
console.error('알림 삭제 오류:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '알림 삭제 중 오류가 발생했습니다.'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 알림 생성 (시스템용)
|
|
async create(req, res) {
|
|
try {
|
|
const { type, title, message, link_url, user_id } = req.body;
|
|
|
|
if (!title) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: '알림 제목은 필수입니다.'
|
|
});
|
|
}
|
|
|
|
const notificationId = await notificationModel.create({
|
|
user_id,
|
|
type,
|
|
title,
|
|
message,
|
|
link_url,
|
|
created_by: req.user?.id
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '알림이 생성되었습니다.',
|
|
data: { notification_id: notificationId }
|
|
});
|
|
} catch (error) {
|
|
console.error('알림 생성 오류:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '알림 생성 중 오류가 발생했습니다.'
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = notificationController;
|