Initial commit: teaching institution management API.
Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
// /seed.js
|
||||
const mongoose = require('mongoose');
|
||||
const config = require('./config/config');
|
||||
const Role = require('./components/roles/roleModel');
|
||||
const User = require('./components/users/userModel');
|
||||
const { ALL_PERMISSIONS, PERMISSIONS } = require('./constants/permissions');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const defaultRoles = [
|
||||
{
|
||||
name: 'SuperAdmin',
|
||||
description: 'مدیر ارشد سیستم با دسترسی کامل به تمام بخشها',
|
||||
permissions: ALL_PERMISSIONS,
|
||||
isSystem: true
|
||||
},
|
||||
{
|
||||
name: 'Admin',
|
||||
description: 'مدیر سیستم',
|
||||
permissions: ALL_PERMISSIONS.filter(p => !p.startsWith('roles:')),
|
||||
isSystem: true
|
||||
},
|
||||
{
|
||||
name: 'Secretary',
|
||||
description: 'منشی و مسئول ثبتنام، امور دانشجویان و پرداختها',
|
||||
permissions: [
|
||||
PERMISSIONS.USERS_CREATE,
|
||||
PERMISSIONS.USERS_READ,
|
||||
PERMISSIONS.USERS_UPDATE,
|
||||
PERMISSIONS.USERS_SEARCH,
|
||||
PERMISSIONS.USERS_ENROLL,
|
||||
PERMISSIONS.CLASSES_READ,
|
||||
PERMISSIONS.CLASSES_SEARCH,
|
||||
PERMISSIONS.CLASSES_REGISTER_USERS,
|
||||
PERMISSIONS.COURSES_READ,
|
||||
PERMISSIONS.COURSES_SEARCH,
|
||||
PERMISSIONS.SESSIONS_READ,
|
||||
PERMISSIONS.SESSIONS_SEARCH,
|
||||
PERMISSIONS.SESSIONS_ATTENDANCE,
|
||||
PERMISSIONS.PAYMENTS_CREATE,
|
||||
PERMISSIONS.PAYMENTS_READ,
|
||||
PERMISSIONS.PAYMENTS_UPDATE,
|
||||
PERMISSIONS.PAYMENTS_SEARCH,
|
||||
PERMISSIONS.NOTIFICATIONS_CREATE,
|
||||
PERMISSIONS.NOTIFICATIONS_READ,
|
||||
PERMISSIONS.NOTIFICATIONS_SEARCH,
|
||||
PERMISSIONS.NOTIFICATIONS_RETRY,
|
||||
PERMISSIONS.CERTIFICATES_CREATE,
|
||||
PERMISSIONS.CERTIFICATES_READ,
|
||||
PERMISSIONS.CERTIFICATES_SEARCH,
|
||||
PERMISSIONS.FILES_UPLOAD,
|
||||
PERMISSIONS.FILES_READ,
|
||||
PERMISSIONS.LOGS_READ,
|
||||
PERMISSIONS.LOGS_SEARCH,
|
||||
PERMISSIONS.CONTACT_INQUIRIES_READ,
|
||||
PERMISSIONS.CONTACT_INQUIRIES_UPDATE
|
||||
],
|
||||
isSystem: true
|
||||
},
|
||||
{
|
||||
name: 'User',
|
||||
description: 'کاربر معمولی و دانشجو',
|
||||
permissions: [
|
||||
PERMISSIONS.COURSES_READ,
|
||||
PERMISSIONS.COURSES_SEARCH,
|
||||
PERMISSIONS.CLASSES_READ,
|
||||
PERMISSIONS.CLASSES_SEARCH,
|
||||
PERMISSIONS.SESSIONS_READ,
|
||||
PERMISSIONS.SESSIONS_SEARCH,
|
||||
PERMISSIONS.PAYMENTS_READ,
|
||||
PERMISSIONS.NOTIFICATIONS_READ,
|
||||
PERMISSIONS.CERTIFICATES_READ,
|
||||
PERMISSIONS.FILES_READ
|
||||
],
|
||||
isSystem: true
|
||||
},
|
||||
{
|
||||
name: 'Professor',
|
||||
description: 'استاد مدرس دوره',
|
||||
permissions: [
|
||||
PERMISSIONS.COURSES_READ,
|
||||
PERMISSIONS.COURSES_SEARCH,
|
||||
PERMISSIONS.CLASSES_READ,
|
||||
PERMISSIONS.CLASSES_SEARCH,
|
||||
PERMISSIONS.SESSIONS_READ,
|
||||
PERMISSIONS.SESSIONS_SEARCH,
|
||||
PERMISSIONS.SESSIONS_ATTENDANCE,
|
||||
PERMISSIONS.NOTIFICATIONS_READ,
|
||||
PERMISSIONS.FILES_READ
|
||||
],
|
||||
isSystem: true
|
||||
}
|
||||
];
|
||||
|
||||
const seedDatabase = async () => {
|
||||
try {
|
||||
console.log('Connecting to MongoDB...');
|
||||
await mongoose.connect(config.MONGO_URI);
|
||||
console.log('Connected to MongoDB successfully.');
|
||||
|
||||
for (const roleDef of defaultRoles) {
|
||||
const existing = await Role.findOne({ name: roleDef.name });
|
||||
if (existing) {
|
||||
existing.description = roleDef.description;
|
||||
existing.permissions = roleDef.permissions;
|
||||
existing.isSystem = roleDef.isSystem;
|
||||
await existing.save();
|
||||
console.log(`Updated existing role: ${roleDef.name}`);
|
||||
} else {
|
||||
await Role.create(roleDef);
|
||||
console.log(`Created new role: ${roleDef.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure SuperAdmin user exists
|
||||
const superAdminRole = await Role.findOne({ name: 'SuperAdmin' });
|
||||
if (superAdminRole) {
|
||||
const adminUser = await User.findOne({ username: config.SUPERADMIN_USERNAME });
|
||||
if (!adminUser) {
|
||||
const passwordHash = await bcrypt.hash(config.SUPERADMIN_PASSWORD, 10);
|
||||
await User.create({
|
||||
name: 'مدیر',
|
||||
surname: 'ارشد',
|
||||
nationalId: config.SUPERADMIN_NATIONAL_ID,
|
||||
phone: config.SUPERADMIN_PHONE,
|
||||
email: config.SUPERADMIN_EMAIL,
|
||||
username: config.SUPERADMIN_USERNAME,
|
||||
passwordHash,
|
||||
role: superAdminRole._id,
|
||||
isActive: true
|
||||
});
|
||||
console.log(`SuperAdmin user created (${config.SUPERADMIN_USERNAME}).`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Database seeding completed successfully.');
|
||||
} catch (error) {
|
||||
console.error('Database seeding failed:', error);
|
||||
} finally {
|
||||
await mongoose.disconnect();
|
||||
console.log('Disconnected from MongoDB.');
|
||||
}
|
||||
};
|
||||
|
||||
seedDatabase();
|
||||
Reference in New Issue
Block a user