TK-FB(공장관리+신고)와 M-Project(부적합관리)를 3개 독립 시스템으로 분리하기 위한 전체 코드 구조 작성. - SSO 인증 서비스 (bcrypt + pbkdf2 이중 해시 지원) - System 1: 공장관리 (TK-FB 기반, 신고 코드 제거) - System 2: 신고 (TK-FB에서 workIssue 코드 추출) - System 3: 부적합관리 (M-Project 기반) - Gateway 포털 (path-based 라우팅) - 통합 docker-compose.yml 및 배포 스크립트 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
147 lines
4.1 KiB
JavaScript
147 lines
4.1 KiB
JavaScript
// models/notificationRecipientModel.js
|
|
const { getDb } = require('../dbPool');
|
|
|
|
const NOTIFICATION_TYPES = {
|
|
repair: '설비 수리',
|
|
safety: '안전 신고',
|
|
nonconformity: '부적합 신고',
|
|
equipment: '설비 관련',
|
|
maintenance: '정기점검',
|
|
system: '시스템'
|
|
};
|
|
|
|
const notificationRecipientModel = {
|
|
// 알림 유형 목록 가져오기
|
|
getTypes() {
|
|
return NOTIFICATION_TYPES;
|
|
},
|
|
|
|
// 유형별 수신자 목록 조회
|
|
async getByType(notificationType) {
|
|
const db = await getDb();
|
|
const [rows] = await db.query(
|
|
`SELECT nr.*, u.username, u.name as user_name, r.name as role
|
|
FROM notification_recipients nr
|
|
JOIN users u ON nr.user_id = u.user_id
|
|
LEFT JOIN roles r ON u.role_id = r.id
|
|
WHERE nr.notification_type = ? AND nr.is_active = 1
|
|
ORDER BY u.name`,
|
|
[notificationType]
|
|
);
|
|
return rows;
|
|
},
|
|
|
|
// 전체 수신자 목록 조회 (유형별 그룹화)
|
|
async getAll() {
|
|
const db = await getDb();
|
|
const [rows] = await db.query(
|
|
`SELECT nr.*, u.username, u.name as user_name, r.name as role
|
|
FROM notification_recipients nr
|
|
JOIN users u ON nr.user_id = u.user_id
|
|
LEFT JOIN roles r ON u.role_id = r.id
|
|
WHERE nr.is_active = 1
|
|
ORDER BY nr.notification_type, u.name`
|
|
);
|
|
|
|
// 유형별로 그룹화
|
|
const grouped = {};
|
|
for (const type in NOTIFICATION_TYPES) {
|
|
grouped[type] = {
|
|
label: NOTIFICATION_TYPES[type],
|
|
recipients: []
|
|
};
|
|
}
|
|
|
|
rows.forEach(row => {
|
|
if (grouped[row.notification_type]) {
|
|
grouped[row.notification_type].recipients.push(row);
|
|
}
|
|
});
|
|
|
|
return grouped;
|
|
},
|
|
|
|
// 수신자 추가
|
|
async add(notificationType, userId, createdBy = null) {
|
|
const db = await getDb();
|
|
const [result] = await db.query(
|
|
`INSERT INTO notification_recipients (notification_type, user_id, created_by)
|
|
VALUES (?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE is_active = 1`,
|
|
[notificationType, userId, createdBy]
|
|
);
|
|
return result.insertId || result.affectedRows > 0;
|
|
},
|
|
|
|
// 수신자 제거 (soft delete)
|
|
async remove(notificationType, userId) {
|
|
const db = await getDb();
|
|
const [result] = await db.query(
|
|
`UPDATE notification_recipients SET is_active = 0
|
|
WHERE notification_type = ? AND user_id = ?`,
|
|
[notificationType, userId]
|
|
);
|
|
return result.affectedRows > 0;
|
|
},
|
|
|
|
// 수신자 완전 삭제
|
|
async delete(notificationType, userId) {
|
|
const db = await getDb();
|
|
const [result] = await db.query(
|
|
`DELETE FROM notification_recipients
|
|
WHERE notification_type = ? AND user_id = ?`,
|
|
[notificationType, userId]
|
|
);
|
|
return result.affectedRows > 0;
|
|
},
|
|
|
|
// 유형별 수신자 user_id 목록만 가져오기 (알림 생성용)
|
|
async getRecipientIds(notificationType) {
|
|
const db = await getDb();
|
|
const [rows] = await db.query(
|
|
`SELECT user_id FROM notification_recipients
|
|
WHERE notification_type = ? AND is_active = 1`,
|
|
[notificationType]
|
|
);
|
|
return rows.map(r => r.user_id);
|
|
},
|
|
|
|
// 사용자가 특정 유형의 수신자인지 확인
|
|
async isRecipient(notificationType, userId) {
|
|
const db = await getDb();
|
|
const [[row]] = await db.query(
|
|
`SELECT 1 FROM notification_recipients
|
|
WHERE notification_type = ? AND user_id = ? AND is_active = 1`,
|
|
[notificationType, userId]
|
|
);
|
|
return !!row;
|
|
},
|
|
|
|
// 유형별 수신자 일괄 설정
|
|
async setRecipients(notificationType, userIds, createdBy = null) {
|
|
const db = await getDb();
|
|
|
|
// 기존 수신자 비활성화
|
|
await db.query(
|
|
`UPDATE notification_recipients SET is_active = 0
|
|
WHERE notification_type = ?`,
|
|
[notificationType]
|
|
);
|
|
|
|
// 새 수신자 추가
|
|
if (userIds && userIds.length > 0) {
|
|
const values = userIds.map(userId => [notificationType, userId, createdBy]);
|
|
await db.query(
|
|
`INSERT INTO notification_recipients (notification_type, user_id, created_by)
|
|
VALUES ?
|
|
ON DUPLICATE KEY UPDATE is_active = 1`,
|
|
[values]
|
|
);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
module.exports = notificationRecipientModel;
|