Add Context APIs and refactor vocab models
All checks were successful
Deploy to Production / deploy (push) Successful in 25s

Introduce Context and ContextGuide features: add Sequelize models (models/Context.js, models/ContextGuide.js), controllers (controllers/contextController.js, controllers/contextGuideController.js) and authenticated route handlers (routes/contextRoutes.js, routes/contextGuideRoutes.js). Wire the new routes into app.js and export the models from models/index.js. Refactor vocabulary: remove VocabForm, VocabMapping and VocabRelation models and relationships, update models/Vocab.js schema and indexes, and add migrate-vocab.js to drop/recreate the vocab table for the new schema. Also add a lesson editor UI (public/lesson-editor.html) and a small cleanup in models/Lesson.js.
This commit is contained in:
silverpro89
2026-02-02 10:05:46 +07:00
parent 81a7ab5b6c
commit 97dbbd4d12
15 changed files with 1018 additions and 248 deletions

92
models/Context.js Normal file
View File

@@ -0,0 +1,92 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const Context = sequelize.define('Context', {
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
comment: 'Unique identifier for context'
},
title: {
type: DataTypes.STRING(255),
allowNull: false,
comment: 'Title of the context item'
},
context: {
type: DataTypes.TEXT,
allowNull: false,
comment: 'Context description'
},
knowledge: {
type: DataTypes.TEXT,
comment: 'Additional knowledge or information'
},
type: {
type: DataTypes.STRING(50),
allowNull: false,
comment: 'Context type (e.g., vocabulary, grammar)'
},
desc: {
type: DataTypes.TEXT,
comment: 'Detailed description or requirement'
},
prompt: {
type: DataTypes.JSON,
comment: 'Prompt configuration object'
},
image: {
type: DataTypes.JSON,
comment: 'Array of image URLs'
},
difficulty: {
type: DataTypes.INTEGER,
defaultValue: 1,
comment: 'Difficulty level (1-10)'
},
max: {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: 'Maximum number of images or items'
},
isPrompt: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: 'Prompt created (0/1)'
},
isList: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: 'Waiting for more images (0/1)'
},
isApprove: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: 'Teacher approval status (0/1)'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'context',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [
{
name: 'idx_context_type',
fields: ['type']
},
{
name: 'idx_context_title',
fields: ['title']
}
]
});
module.exports = Context;

51
models/ContextGuide.js Normal file
View File

@@ -0,0 +1,51 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const ContextGuide = sequelize.define('ContextGuide', {
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
comment: 'Unique identifier for context guide'
},
title: {
type: DataTypes.STRING(255),
allowNull: false,
comment: 'Guide title'
},
name: {
type: DataTypes.STRING(255),
allowNull: false,
comment: 'Target field name for AI processing'
},
data: {
type: DataTypes.JSON,
allowNull: false,
comment: 'Guide data (text or json)'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'context_guide',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [
{
name: 'idx_context_guide_title',
fields: ['title']
},
{
name: 'idx_context_guide_name',
fields: ['name']
}
]
});
module.exports = ContextGuide;

View File

@@ -63,7 +63,6 @@ const Lesson = sequelize.define('lessons', {
type: DataTypes.STRING(50),
comment: 'Loại content cho URL: video, audio, pdf, external_link, youtube, etc.'
},
duration_minutes: {
type: DataTypes.INTEGER,
comment: 'Thời lượng (phút)'

View File

@@ -8,21 +8,32 @@ const Vocab = sequelize.define('Vocab', {
primaryKey: true,
comment: 'Unique identifier for vocabulary entry'
},
vocab_code: {
type: DataTypes.STRING(50),
unique: true,
allowNull: false,
comment: 'Unique code for vocabulary (e.g., vocab-001-eat)'
// Từ thực tế (wash, washes, washing, ate, eaten...)
text: {
type: DataTypes.STRING(100),
allowNull: false,
index: true
},
base_word: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'Base form of the word'
// Từ gốc để nhóm lại (wash, eat...)
base_word: {
type: DataTypes.STRING(100),
allowNull: false,
index: true
},
// Đã xuất hiện trong khối nào, bài học nào, lesson nào
// Ví dụ 111 là grade 1, unit 1, lesson 1
location: {
type: DataTypes.INTEGER,
comment: 'Location or source of the vocabulary'
},
// Loại biến thể (V1, V2, V3, V_ing, Noun_Form...)
form_key: {
type: DataTypes.STRING(20),
defaultValue: 'base'
},
// Nội dung dùng chung (có thể lưu JSON để dễ quản lý hoặc dùng chung cho cả group)
translation: {
type: DataTypes.STRING(200),
allowNull: false,
comment: 'Vietnamese translation'
type: DataTypes.STRING(200)
},
difficulty_score: {
type: DataTypes.INTEGER,
@@ -33,6 +44,10 @@ const Vocab = sequelize.define('Vocab', {
type: DataTypes.STRING(100),
comment: 'Category of the word (e.g., Action Verbs, Nouns)'
},
topic: {
type: DataTypes.STRING(100),
comment: 'Topic of the word (e.g., Food, Travel, Education)'
},
images: {
type: DataTypes.JSON,
comment: 'Array of image URLs'
@@ -73,8 +88,8 @@ const Vocab = sequelize.define('Vocab', {
updatedAt: 'updated_at',
indexes: [
{
name: 'idx_vocab_code',
fields: ['vocab_code']
name: 'idx_vocab_text',
fields: ['text']
},
{
name: 'idx_base_word',
@@ -83,6 +98,10 @@ const Vocab = sequelize.define('Vocab', {
{
name: 'idx_category',
fields: ['category']
},
{
name: 'idx_location',
fields: ['location']
}
]
});

View File

@@ -1,78 +0,0 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const VocabForm = sequelize.define('VocabForm', {
form_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
comment: 'Unique identifier for word form'
},
vocab_id: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'vocab',
key: 'vocab_id'
},
onDelete: 'CASCADE',
comment: 'Reference to vocabulary entry'
},
form_key: {
type: DataTypes.STRING(50),
allowNull: false,
comment: 'Form identifier (v1, v2, v3, v_s_es, v_ing, etc.)'
},
text: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'The actual word form (e.g., eat, eats, eating, ate)'
},
phonetic: {
type: DataTypes.STRING(100),
comment: 'IPA phonetic transcription (e.g., /iːt/)'
},
audio_url: {
type: DataTypes.STRING(500),
comment: 'URL to audio pronunciation file'
},
min_grade: {
type: DataTypes.INTEGER,
defaultValue: 1,
comment: 'Minimum grade level to unlock this form'
},
description: {
type: DataTypes.TEXT,
comment: 'Description or usage note for this form'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'vocab_form',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [
{
name: 'idx_vocab_form_vocab',
fields: ['vocab_id']
},
{
name: 'idx_vocab_form_key',
fields: ['form_key']
},
{
name: 'idx_vocab_form_unique',
unique: true,
fields: ['vocab_id', 'form_key']
}
]
});
module.exports = VocabForm;

View File

@@ -1,72 +0,0 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const VocabMapping = sequelize.define('VocabMapping', {
mapping_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
comment: 'Unique identifier for curriculum mapping'
},
vocab_id: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'vocab',
key: 'vocab_id'
},
onDelete: 'CASCADE',
comment: 'Reference to vocabulary entry'
},
book_id: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'Book/curriculum identifier (e.g., global-success-1)'
},
grade: {
type: DataTypes.INTEGER,
allowNull: false,
comment: 'Grade level'
},
unit: {
type: DataTypes.INTEGER,
comment: 'Unit number in the book'
},
lesson: {
type: DataTypes.INTEGER,
comment: 'Lesson number in the unit'
},
form_key: {
type: DataTypes.STRING(50),
comment: 'Which form to use (v1, v_s_es, v_ing, v2, v3, etc.)'
},
context_note: {
type: DataTypes.TEXT,
comment: 'Additional context for this mapping'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'vocab_mapping',
timestamps: true,
createdAt: 'created_at',
updatedAt: false,
indexes: [
{
name: 'idx_vocab_mapping_vocab',
fields: ['vocab_id']
},
{
name: 'idx_vocab_mapping_book_grade',
fields: ['book_id', 'grade']
},
{
name: 'idx_vocab_mapping_unit_lesson',
fields: ['unit', 'lesson']
}
]
});
module.exports = VocabMapping;

View File

@@ -1,65 +0,0 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const VocabRelation = sequelize.define('VocabRelation', {
relation_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
comment: 'Unique identifier for vocabulary relation'
},
vocab_id: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'vocab',
key: 'vocab_id'
},
onDelete: 'CASCADE',
comment: 'Reference to vocabulary entry'
},
relation_type: {
type: DataTypes.ENUM('synonym', 'antonym', 'related'),
allowNull: false,
comment: 'Type of relation (synonym, antonym, related)'
},
related_word: {
type: DataTypes.STRING(100),
allowNull: false,
comment: 'The related word (e.g., consume, dine, fast)'
},
related_vocab_id: {
type: DataTypes.UUID,
references: {
model: 'vocab',
key: 'vocab_id'
},
onDelete: 'SET NULL',
comment: 'Reference to related vocab entry if it exists in system'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'vocab_relation',
timestamps: true,
createdAt: 'created_at',
updatedAt: false,
indexes: [
{
name: 'idx_vocab_relation_vocab',
fields: ['vocab_id']
},
{
name: 'idx_vocab_relation_type',
fields: ['relation_type']
},
{
name: 'idx_vocab_relation_related',
fields: ['related_vocab_id']
}
]
});
module.exports = VocabRelation;

View File

@@ -35,9 +35,6 @@ 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');
@@ -47,6 +44,10 @@ const GrammarMediaStory = require('./GrammarMediaStory');
// Group 3.4: Story System (NEW)
const Story = require('./Story');
// Group 3.5: Context (NEW)
const Context = require('./Context');
const ContextGuide = require('./ContextGuide');
// Group 4: Attendance
const AttendanceLog = require('./AttendanceLog');
const AttendanceDaily = require('./AttendanceDaily');
@@ -164,18 +165,7 @@ const setupRelationships = () => {
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' });
// No additional relationships needed
// Grammar relationships (NEW)
// Grammar -> GrammarMapping (1:N)
@@ -299,9 +289,6 @@ module.exports = {
// Group 3.2: Vocabulary System (NEW)
Vocab,
VocabMapping,
VocabForm,
VocabRelation,
// Group 3.3: Grammar System (NEW)
Grammar,
@@ -310,6 +297,10 @@ module.exports = {
// Group 3.4: Story System (NEW)
Story,
// Group 3.5: Context (NEW)
Context,
ContextGuide,
// Group 4: Attendance
AttendanceLog,