Files
sena_db_api_layer/models/index.js
silverpro89 2c7b4675a7 update
2026-01-26 20:23:08 +07:00

337 lines
12 KiB
JavaScript

const { sequelize } = require('../config/database');
/**
* Import all models
*/
// Group 1: System & Authorization
const School = require('./School');
const UsersAuth = require('./UsersAuth');
const Role = require('./Role');
const Permission = require('./Permission');
const RolePermission = require('./RolePermission');
const UserAssignment = require('./UserAssignment');
// Group 2: User Profiles
const UserProfile = require('./UserProfile');
const StudentDetail = require('./StudentDetail');
const TeacherDetail = require('./TeacherDetail');
const ParentStudentMap = require('./ParentStudentMap');
const StaffContract = require('./StaffContract');
// Group 3: Academic Structure
const AcademicYear = require('./AcademicYear');
const Subject = require('./Subject');
const Class = require('./Class');
const ClassSchedule = require('./ClassSchedule');
const Room = require('./Room');
// Group 3.1: Learning Content (NEW)
const Chapter = require('./Chapter');
const Lesson = require('./Lesson');
const Game = require('./Game');
const LessonComponentProgress = require('./LessonComponentProgress');
const LessonLeaderboard = require('./LessonLeaderboard');
// Group 3.2: Vocabulary System (NEW)
const Vocab = require('./Vocab');
const VocabMapping = require('./VocabMapping');
const VocabForm = require('./VocabForm');
const VocabRelation = require('./VocabRelation');
// Group 3.3: Grammar System (NEW)
const Grammar = require('./Grammar');
const GrammarMapping = require('./GrammarMapping');
const GrammarMediaStory = require('./GrammarMediaStory');
// Group 3.4: Story System (NEW)
const Story = require('./Story');
// Group 4: Attendance
const AttendanceLog = require('./AttendanceLog');
const AttendanceDaily = require('./AttendanceDaily');
const LeaveRequest = require('./LeaveRequest');
// Group 5: Gradebook
const GradeCategory = require('./GradeCategory');
const GradeItem = require('./GradeItem');
const Grade = require('./Grade');
const GradeSummary = require('./GradeSummary');
const GradeHistory = require('./GradeHistory');
// Group 6: Notifications
const Notification = require('./Notification');
const NotificationLog = require('./NotificationLog');
const Message = require('./Message');
const AuditLog = require('./AuditLog');
// Group 7: Subscription & Training
const SubscriptionPlan = require('./SubscriptionPlan');
const UserSubscription = require('./UserSubscription');
const StaffTrainingAssignment = require('./StaffTrainingAssignment');
const StaffAchievement = require('./StaffAchievement');
const ParentAssignedTask = require('./ParentAssignedTask');
/**
* Define relationships between models
*/
const setupRelationships = () => {
// UsersAuth relationships
UsersAuth.hasOne(UserProfile, { foreignKey: 'user_id', as: 'profile' });
UsersAuth.hasMany(UserAssignment, { foreignKey: 'user_id', as: 'assignments' });
// UserProfile relationships
UserProfile.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'auth' });
UserProfile.belongsTo(School, { foreignKey: 'school_id', as: 'school' });
UserProfile.hasOne(StudentDetail, { foreignKey: 'user_id', as: 'studentDetail' });
UserProfile.hasOne(TeacherDetail, { foreignKey: 'user_id', as: 'teacherDetail' });
// School relationships
School.hasMany(Class, { foreignKey: 'school_id', as: 'classes' });
School.hasMany(Room, { foreignKey: 'school_id', as: 'rooms' });
School.hasMany(UserAssignment, { foreignKey: 'school_id', as: 'assignments' });
School.hasMany(AttendanceLog, { foreignKey: 'school_id', as: 'attendanceLogs' });
// Role & Permission relationships
Role.belongsToMany(Permission, {
through: RolePermission,
foreignKey: 'role_id',
otherKey: 'permission_id',
as: 'permissions'
});
Permission.belongsToMany(Role, {
through: RolePermission,
foreignKey: 'permission_id',
otherKey: 'role_id',
as: 'roles'
});
// UserAssignment relationships
UserAssignment.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'user' });
UserAssignment.belongsTo(Role, { foreignKey: 'role_id', as: 'role' });
UserAssignment.belongsTo(School, { foreignKey: 'school_id', as: 'school' });
UserAssignment.belongsTo(Class, { foreignKey: 'class_id', as: 'class' });
// StudentDetail relationships
StudentDetail.belongsTo(UserProfile, {
foreignKey: 'user_id',
targetKey: 'user_id',
as: 'profile'
});
StudentDetail.belongsTo(Class, { foreignKey: 'current_class_id', as: 'currentClass' });
StudentDetail.belongsToMany(UserProfile, {
through: ParentStudentMap,
foreignKey: 'student_id',
otherKey: 'parent_id',
as: 'parents'
});
// TeacherDetail relationships
TeacherDetail.belongsTo(UserProfile, { foreignKey: 'user_id', as: 'profile' });
TeacherDetail.hasMany(StaffContract, { foreignKey: 'teacher_id', as: 'contracts' });
// Class relationships
Class.belongsTo(School, { foreignKey: 'school_id', as: 'school' });
Class.belongsTo(AcademicYear, { foreignKey: 'academic_year_id', as: 'academicYear' });
Class.belongsTo(TeacherDetail, { foreignKey: 'homeroom_teacher_id', as: 'homeroomTeacher' });
Class.hasMany(StudentDetail, { foreignKey: 'current_class_id', as: 'students' });
Class.hasMany(ClassSchedule, { foreignKey: 'class_id', as: 'schedules' });
Class.hasMany(Grade, { foreignKey: 'class_id', as: 'grades' });
// ClassSchedule relationships
ClassSchedule.belongsTo(Class, { foreignKey: 'class_id', as: 'class' });
ClassSchedule.belongsTo(Subject, { foreignKey: 'subject_id', as: 'subject' });
ClassSchedule.belongsTo(Room, { foreignKey: 'room_id', as: 'room' });
ClassSchedule.belongsTo(TeacherDetail, { foreignKey: 'teacher_id', as: 'teacher' });
// Learning Content relationships (NEW)
// Subject -> Chapter (1:N)
Subject.hasMany(Chapter, { foreignKey: 'subject_id', as: 'chapters' });
Chapter.belongsTo(Subject, { foreignKey: 'subject_id', as: 'subject' });
// Chapter -> Lesson (1:N)
Chapter.hasMany(Lesson, { foreignKey: 'chapter_id', as: 'lessons' });
Lesson.belongsTo(Chapter, { foreignKey: 'chapter_id', as: 'chapter' });
// Lesson Progress relationships
LessonComponentProgress.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'user' });
LessonComponentProgress.belongsTo(Lesson, { foreignKey: 'lesson_id', as: 'lesson' });
Lesson.hasMany(LessonComponentProgress, { foreignKey: 'lesson_id', as: 'progress' });
// Leaderboard relationships
LessonLeaderboard.belongsTo(Lesson, { foreignKey: 'lesson_id', as: 'lesson' });
LessonLeaderboard.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'user' });
Lesson.hasMany(LessonLeaderboard, { foreignKey: 'lesson_id', as: 'leaderboard' });
// Vocabulary relationships (NEW)
// Vocab -> VocabMapping (1:N)
Vocab.hasMany(VocabMapping, { foreignKey: 'vocab_id', as: 'mappings' });
VocabMapping.belongsTo(Vocab, { foreignKey: 'vocab_id', as: 'vocab' });
// Vocab -> VocabForm (1:N)
Vocab.hasMany(VocabForm, { foreignKey: 'vocab_id', as: 'forms' });
VocabForm.belongsTo(Vocab, { foreignKey: 'vocab_id', as: 'vocab' });
// Vocab -> VocabRelation (1:N)
Vocab.hasMany(VocabRelation, { foreignKey: 'vocab_id', as: 'relations' });
VocabRelation.belongsTo(Vocab, { foreignKey: 'vocab_id', as: 'vocab' });
VocabRelation.belongsTo(Vocab, { foreignKey: 'related_vocab_id', as: 'relatedVocab' });
// Grammar relationships (NEW)
// Grammar -> GrammarMapping (1:N)
Grammar.hasMany(GrammarMapping, { foreignKey: 'grammar_id', as: 'mappings' });
GrammarMapping.belongsTo(Grammar, { foreignKey: 'grammar_id', as: 'grammar' });
// Grammar -> GrammarMediaStory (1:N)
Grammar.hasMany(GrammarMediaStory, { foreignKey: 'grammar_id', as: 'mediaStories' });
GrammarMediaStory.belongsTo(Grammar, { foreignKey: 'grammar_id', as: 'grammar' });
// Attendance relationships
AttendanceLog.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'user' });
AttendanceLog.belongsTo(School, { foreignKey: 'school_id', as: 'school' });
AttendanceDaily.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'user' });
AttendanceDaily.belongsTo(School, { foreignKey: 'school_id', as: 'school' });
AttendanceDaily.belongsTo(Class, { foreignKey: 'class_id', as: 'class' });
LeaveRequest.belongsTo(StudentDetail, { foreignKey: 'student_id', as: 'student' });
LeaveRequest.belongsTo(UserProfile, { foreignKey: 'requested_by', as: 'requester' });
LeaveRequest.belongsTo(TeacherDetail, { foreignKey: 'approved_by', as: 'approver' });
// Grade relationships
GradeCategory.belongsTo(Subject, { foreignKey: 'subject_id', as: 'subject' });
GradeCategory.hasMany(GradeItem, { foreignKey: 'category_id', as: 'items' });
GradeItem.belongsTo(GradeCategory, { foreignKey: 'category_id', as: 'category' });
GradeItem.belongsTo(Class, { foreignKey: 'class_id', as: 'class' });
GradeItem.hasMany(Grade, { foreignKey: 'grade_item_id', as: 'grades' });
Grade.belongsTo(GradeItem, { foreignKey: 'grade_item_id', as: 'gradeItem' });
Grade.belongsTo(StudentDetail, { foreignKey: 'student_id', as: 'student' });
Grade.belongsTo(Class, { foreignKey: 'class_id', as: 'class' });
Grade.belongsTo(TeacherDetail, { foreignKey: 'graded_by', as: 'gradedBy' });
Grade.hasMany(GradeHistory, { foreignKey: 'grade_id', as: 'history' });
GradeSummary.belongsTo(StudentDetail, { foreignKey: 'student_id', as: 'student' });
GradeSummary.belongsTo(Subject, { foreignKey: 'subject_id', as: 'subject' });
GradeSummary.belongsTo(AcademicYear, { foreignKey: 'academic_year_id', as: 'academicYear' });
// Notification relationships
Notification.hasMany(NotificationLog, { foreignKey: 'notification_id', as: 'logs' });
NotificationLog.belongsTo(Notification, { foreignKey: 'notification_id', as: 'notification' });
NotificationLog.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'user' });
Message.belongsTo(UserProfile, { foreignKey: 'sender_id', as: 'sender' });
Message.belongsTo(UserProfile, { foreignKey: 'recipient_id', as: 'recipient' });
// Subscription relationships
SubscriptionPlan.hasMany(UserSubscription, { foreignKey: 'plan_id', as: 'subscriptions' });
UserSubscription.belongsTo(SubscriptionPlan, { foreignKey: 'plan_id', as: 'plan' });
UserSubscription.belongsTo(UsersAuth, { foreignKey: 'user_id', as: 'user' });
// Training relationships
StaffTrainingAssignment.belongsTo(UsersAuth, { foreignKey: 'staff_id', as: 'staff' });
StaffTrainingAssignment.belongsTo(UsersAuth, { foreignKey: 'assigned_by', as: 'assigner' });
StaffTrainingAssignment.belongsTo(Subject, { foreignKey: 'subject_id', as: 'subject' });
StaffAchievement.belongsTo(UsersAuth, { foreignKey: 'staff_id', as: 'staff' });
StaffAchievement.belongsTo(Subject, { foreignKey: 'course_id', as: 'course' });
StaffAchievement.belongsTo(UsersAuth, { foreignKey: 'verified_by', as: 'verifier' });
// Parent assigned task relationships
ParentAssignedTask.belongsTo(UsersAuth, { foreignKey: 'parent_id', as: 'parent' });
ParentAssignedTask.belongsTo(StudentDetail, { foreignKey: 'student_id', as: 'student' });
ParentAssignedTask.belongsTo(GradeItem, { foreignKey: 'grade_item_id', as: 'gradeItem' });
console.log('✅ Model relationships configured');
};
/**
* Sync database (only in development)
*/
const syncDatabase = async (options = {}) => {
try {
await sequelize.sync(options);
console.log('✅ Database synchronized');
} catch (error) {
console.error('❌ Database sync failed:', error.message);
throw error;
}
};
/**
* Export all models and utilities
*/
module.exports = {
sequelize,
setupRelationships,
syncDatabase,
// Group 1: System & Authorization
School,
UsersAuth,
Role,
Permission,
RolePermission,
UserAssignment,
// Group 2: User Profiles
UserProfile,
StudentDetail,
TeacherDetail,
ParentStudentMap,
StaffContract,
// Group 3: Academic Structure
AcademicYear,
Subject,
Class,
ClassSchedule,
Room,
// Group 3.1: Learning Content (NEW)
Chapter,
Lesson,
Game,
LessonComponentProgress,
LessonLeaderboard,
// Group 3.2: Vocabulary System (NEW)
Vocab,
VocabMapping,
VocabForm,
VocabRelation,
// Group 3.3: Grammar System (NEW)
Grammar,
GrammarMapping,
GrammarMediaStory,
// Group 3.4: Story System (NEW)
Story,
// Group 4: Attendance
AttendanceLog,
AttendanceDaily,
LeaveRequest,
// Group 5: Gradebook
GradeCategory,
GradeItem,
Grade,
GradeSummary,
GradeHistory,
// Group 6: Notifications
Notification,
NotificationLog,
Message,
AuditLog,
// Group 7: Subscription & Training
SubscriptionPlan,
UserSubscription,
StaffTrainingAssignment,
StaffAchievement,
ParentAssignedTask,
};