Files
sena_db_api_layer/models/LessonLeaderboard.js
silverpro89 53d97ba5db update
2026-01-20 20:29:07 +07:00

89 lines
1.9 KiB
JavaScript

const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
/**
* LessonLeaderboard Model
* Cache bảng xếp hạng cho lessons
*/
const LessonLeaderboard = sequelize.define('lesson_leaderboard', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
lesson_id: {
type: DataTypes.UUID,
allowNull: false,
comment: 'ID bài học'
},
user_id: {
type: DataTypes.UUID,
allowNull: false,
comment: 'ID học viên'
},
// Ranking data
total_score: {
type: DataTypes.INTEGER,
comment: 'Tổng điểm'
},
accuracy: {
type: DataTypes.DECIMAL(5, 2),
comment: 'Độ chính xác (%)'
},
completion_time: {
type: DataTypes.INTEGER,
comment: 'Thời gian hoàn thành (giây)'
},
rank: {
type: DataTypes.INTEGER,
comment: 'Thứ hạng'
},
// Scope filters
scope: {
type: DataTypes.ENUM('class', 'school', 'global'),
allowNull: false,
comment: 'Phạm vi xếp hạng'
},
scope_id: {
type: DataTypes.UUID,
comment: 'ID của class hoặc school (tùy scope)'
},
time_range: {
type: DataTypes.ENUM('day', 'week', 'month', 'all_time'),
allowNull: false,
comment: 'Khoảng thời gian'
},
// Metadata
computed_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
comment: 'Thời gian tính toán'
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW
},
updated_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'lesson_leaderboard',
timestamps: true,
underscored: true,
indexes: [
{ fields: ['lesson_id', 'scope', 'time_range'] },
{ fields: ['rank'] },
{ fields: ['user_id'] },
{ fields: ['lesson_id', 'user_id', 'scope', 'time_range'], unique: true }
]
});
module.exports = LessonLeaderboard;