Files
2025-04-17 23:32:39 +09:00

110 lines
3.2 KiB
JavaScript

/**
* 초기 알림 데이터 생성 스크립트
*/
require('dotenv').config();
const mongoose = require('mongoose');
const User = require('../models/User');
const Notification = require('../models/Notification');
// MongoDB 연결
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/bowlingManager', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('MongoDB 연결 성공'))
.catch(err => {
console.error('MongoDB 연결 오류:', err);
process.exit(1);
});
// 초기 알림 데이터 생성
const createNotifications = async () => {
try {
// 모든 사용자 가져오기
const users = await User.find();
if (users.length === 0) {
console.log('사용자가 없습니다. 먼저 사용자를 생성해주세요.');
process.exit(0);
}
// 기존 알림 삭제
await Notification.deleteMany({});
console.log('기존 알림 데이터 삭제 완료');
// 각 사용자에 대한 알림 생성
const notificationPromises = users.map(async (user) => {
// 알림 타입 및 메시지 배열
const notificationTemplates = [
{
title: '시스템 알림',
message: '볼링 클럽 매니저 시스템에 오신 것을 환영합니다.',
icon: 'pi pi-info-circle',
type: 'info',
read: false
},
{
title: '클럽 알림',
message: '새로운 클럽 회원이 가입했습니다.',
icon: 'pi pi-users',
type: 'success',
read: false
},
{
title: '예약 알림',
message: '레인 예약이 확정되었습니다.',
icon: 'pi pi-calendar',
type: 'info',
read: true
},
{
title: '대회 알림',
message: '다음 주 토요일 클럽 대회가 예정되어 있습니다.',
icon: 'pi pi-flag',
type: 'warning',
read: false
},
{
title: '시스템 업데이트',
message: '시스템이 업데이트 되었습니다. 새로운 기능을 확인해보세요.',
icon: 'pi pi-refresh',
type: 'info',
read: true
}
];
// 사용자별 알림 생성 (시간 차이를 두고)
const notifications = notificationTemplates.map((template, index) => {
const hoursAgo = (index + 1) * 12; // 12시간, 24시간, 36시간... 간격
const createdAt = new Date(Date.now() - (hoursAgo * 3600000));
return new Notification({
user: user._id,
title: template.title,
message: template.message,
icon: template.icon,
type: template.type,
read: template.read,
createdAt
});
});
return Notification.insertMany(notifications);
});
await Promise.all(notificationPromises);
console.log('알림 데이터 생성 완료');
// 연결 종료
mongoose.connection.close();
console.log('MongoDB 연결 종료');
process.exit(0);
} catch (error) {
console.error('알림 데이터 생성 오류:', error);
mongoose.connection.close();
process.exit(1);
}
};
createNotifications();