From 1f37c7078c60a78d75473dd90401d4bc4ccb9838 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 09:28:57 +0800 Subject: [PATCH 01/47] refactor: migrate Department model to ES module syntax --- src/api/Models/department.js | 86 ++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/src/api/Models/department.js b/src/api/Models/department.js index 702375e..94be5e1 100644 --- a/src/api/Models/department.js +++ b/src/api/Models/department.js @@ -1,54 +1,56 @@ -'use strict'; -const { Model } = require('sequelize'); +import { Model, DataTypes } from 'sequelize'; -module.exports = (sequelize, DataTypes) => { +export default (sequelize) => { class Department extends Model { static associate(models) { - // 2. One department optionally has one department head (an instructor) + // One department optionally has one department head (an instructor) Department.belongsTo(models.Instructor, { foreignKey: 'head_id', - as: 'head' // This lets you do department.getHead() + as: 'head', // Allows department.getHead() }); } } - Department.init({ - department_id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true + Department.init( + { + department_id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true, + }, + department_name: { + type: DataTypes.STRING(100), + allowNull: false, + }, + department_code: { + type: DataTypes.STRING(20), + allowNull: false, + unique: true, + }, + head_id: { + type: DataTypes.INTEGER, + allowNull: true, + }, + email: { + type: DataTypes.STRING(150), + allowNull: true, + }, + phone_number: { + type: DataTypes.STRING(20), + allowNull: true, + }, + building: { + type: DataTypes.STRING(100), + allowNull: true, + }, }, - department_name: { - type: DataTypes.STRING(100), - allowNull: false - }, - department_code: { - type: DataTypes.STRING(20), - allowNull: false, - unique: true - }, - head_id: { - type: DataTypes.INTEGER, - allowNull: true - }, - email: { - type: DataTypes.STRING(150), - allowNull: true - }, - phone_number: { - type: DataTypes.STRING(20), - allowNull: true - }, - building: { - type: DataTypes.STRING(100), - allowNull: true - }, - }, { - sequelize, - modelName: 'Department', - tableName: 'Departments', - timestamps: false // uses created_at and updated_at - }); + { + sequelize, + modelName: 'Department', + tableName: 'Departments', + timestamps: false, + } + ); return Department; -}; \ No newline at end of file +}; From 4f483b6cf33b9e1890c605095059403dd315ddee Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 09:30:11 +0800 Subject: [PATCH 02/47] refactor: migrate index.js to ES module syntax and improve model loading --- src/api/Models/index.js | 49 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/api/Models/index.js b/src/api/Models/index.js index a8b2997..5907d80 100644 --- a/src/api/Models/index.js +++ b/src/api/Models/index.js @@ -1,35 +1,46 @@ -const fs = require('fs'); -const path = require('path'); -const Sequelize = require('sequelize'); +import fs from 'fs'; +import path from 'path'; +import Sequelize, { DataTypes } from 'sequelize'; +import configFile from '../../config/config.js'; + +// __dirname replacement for ES modules +const __dirname = path.dirname(new URL(import.meta.url).pathname); const basename = path.basename(__filename); -// Adjust the config path to point to src/config/config.json -const configPath = path.resolve(__dirname, '../../config/config.js'); -const config = require(configPath)['development']; +// Use the correct environment (you can switch to process.env.NODE_ENV if needed) +const config = configFile.development; const db = {}; +// Initialize Sequelize const sequelize = new Sequelize(config.database, config.username, config.password, config); -fs +// Dynamically import all models in this folder +const files = fs .readdirSync(__dirname) - .filter(file => { - return ( + .filter( + (file) => file.indexOf('.') !== 0 && file !== basename && - file.slice(-3) === '.js' - ); - }) - .forEach(file => { - const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); - db[model.name] = model; - }); - -Object.keys(db).forEach(modelName => { + file.endsWith('.js') + ); + +for (const file of files) { + const { default: modelDefiner } = await import(path.join(__dirname, file)); + const model = modelDefiner(sequelize, DataTypes); + db[model.name] = model; +} + +// Set up associations +for (const modelName of Object.keys(db)) { if (db[modelName].associate) { db[modelName].associate(db); } -}); +} + +db.sequelize = sequelize; + +export default db; db.sequelize = sequelize; // db.Sequelize = Sequelize; From 169929aa376c614f3bc4c35a490406e1813c5295 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 09:31:13 +0800 Subject: [PATCH 03/47] refactor: migrate Instructor model to ES module syntax --- src/api/Models/instructor.js | 53 +++++++++++++++++------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/api/Models/instructor.js b/src/api/Models/instructor.js index 3dcbc61..33929b1 100644 --- a/src/api/Models/instructor.js +++ b/src/api/Models/instructor.js @@ -1,35 +1,32 @@ -'use strict'; -const { - Model -} = require('sequelize'); -const department = require('./department'); -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; + +export default (sequelize) => { class Instructor extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association here + // Define association here Instructor.belongsTo(models.Department, { - foreignKey: 'department_id', - as: 'department' + foreignKey: 'department_id', + as: 'department', }); } } - Instructor.init({ - first_name: DataTypes.STRING, - last_name: DataTypes.STRING, - email: DataTypes.STRING, - phone_number: DataTypes.STRING, - hire_date: DataTypes.DATE, - department: DataTypes.INTEGER, - is_active: DataTypes.BOOLEAN - }, { - sequelize, - modelName: 'Instructor', - timestamps: true - }); + + Instructor.init( + { + first_name: DataTypes.STRING, + last_name: DataTypes.STRING, + email: DataTypes.STRING, + phone_number: DataTypes.STRING, + hire_date: DataTypes.DATE, + department: DataTypes.INTEGER, + is_active: DataTypes.BOOLEAN, + }, + { + sequelize, + modelName: 'Instructor', + timestamps: true, + } + ); + return Instructor; -}; \ No newline at end of file +}; From 0cf5423b8048402a6a33534d445a664e7ef10882 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 09:32:14 +0800 Subject: [PATCH 04/47] refactor: migrate ProgramCourse model to ES module syntax --- src/api/Models/programcourse.js | 85 ++++++++++++++++----------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/api/Models/programcourse.js b/src/api/Models/programcourse.js index e9220b7..9e09bbc 100644 --- a/src/api/Models/programcourse.js +++ b/src/api/Models/programcourse.js @@ -1,60 +1,57 @@ -'use strict'; -const { - Model -} = require('sequelize'); -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; + +export default (sequelize) => { class ProgramCourse extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association here - + // Self-referencing association ProgramCourse.belongsTo(models.ProgramCourse, { - as: 'parent', - foreignKey: 'parent_id' + as: 'parent', + foreignKey: 'parent_id', }); ProgramCourse.hasMany(models.ProgramCourse, { - as: 'children', - foreignKey: 'parent_id' + as: 'children', + foreignKey: 'parent_id', }); // Department association ProgramCourse.belongsTo(models.Department, { foreignKey: 'department_id', - as: 'department' + as: 'department', }); } } - ProgramCourse.init({ - parent_id: DataTypes.INTEGER, - type: { - type:DataTypes.ENUM('PROGRAM','COURSE'), - allowNull:false, - defaultValue:null, - }, - code: DataTypes.STRING, - name: DataTypes.STRING, - department_id: DataTypes.INTEGER, - credit_hours: DataTypes.INTEGER, - semester: DataTypes.INTEGER, - level: { - type: DataTypes.ENUM('DIPLOMA','DEGREE','MASTER','DOCTORATE'), - allowNull:false + + ProgramCourse.init( + { + parent_id: DataTypes.INTEGER, + type: { + type: DataTypes.ENUM('PROGRAM', 'COURSE'), + allowNull: false, + defaultValue: null, + }, + code: DataTypes.STRING, + name: DataTypes.STRING, + department_id: DataTypes.INTEGER, + credit_hours: DataTypes.INTEGER, + semester: DataTypes.INTEGER, + level: { + type: DataTypes.ENUM('DIPLOMA', 'DEGREE', 'MASTER', 'DOCTORATE'), + allowNull: false, + }, + is_active: DataTypes.BOOLEAN, + mode: { + type: DataTypes.ENUM('FULLTIME', 'PARTTIME', 'ONLINE', 'HYBRID', 'DISTANCE'), + allowNull: false, + defaultValue: '', + }, }, - is_active: DataTypes.BOOLEAN, - mode: { - type: DataTypes.ENUM('FULLTIME', 'PARTTIME', 'ONLINE', 'HYBRID', 'DISTANCE'), - allowNull: false, // or false if it's required - defaultValue:'' -} - }, { - sequelize, - modelName: 'ProgramCourse', - timestamps: true - }); + { + sequelize, + modelName: 'ProgramCourse', + timestamps: true, + } + ); + return ProgramCourse; -}; \ No newline at end of file +}; From c75925491df03d2b8b9d7d4ec52f2bf1dfd19f54 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 09:34:42 +0800 Subject: [PATCH 05/47] refactor: migrate Student model to ES module syntax --- src/api/Models/student.js | 62 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/api/Models/student.js b/src/api/Models/student.js index cb647fa..e978be8 100644 --- a/src/api/Models/student.js +++ b/src/api/Models/student.js @@ -1,39 +1,37 @@ -'use strict'; -const { - Model -} = require('sequelize'); -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; + +export default (sequelize) => { class Student extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association - Student.belongsTo(models.User, { - foreignKey: 'userId', + // Define association + Student.belongsTo(models.User, { + foreignKey: 'user_id', as: 'user', }); } } - Student.init({ - user_id: DataTypes.INTEGER, - student_number: DataTypes.STRING, - first_name: DataTypes.STRING, - last_name: DataTypes.STRING, - email: DataTypes.STRING, - phone_number: DataTypes.STRING, - date_of_birth: DataTypes.DATE, - gender: DataTypes.STRING, - enrollment_year: DataTypes.INTEGER, - department_id: DataTypes.INTEGER, - is_active: DataTypes.BOOLEAN, - created_at: DataTypes.DATE, - updated_at: DataTypes.DATE - }, { - sequelize, - modelName: 'Student', - }); + + Student.init( + { + user_id: DataTypes.INTEGER, + student_number: DataTypes.STRING, + first_name: DataTypes.STRING, + last_name: DataTypes.STRING, + email: DataTypes.STRING, + phone_number: DataTypes.STRING, + date_of_birth: DataTypes.DATE, + gender: DataTypes.STRING, + enrollment_year: DataTypes.INTEGER, + department_id: DataTypes.INTEGER, + is_active: DataTypes.BOOLEAN, + created_at: DataTypes.DATE, + updated_at: DataTypes.DATE, + }, + { + sequelize, + modelName: 'Student', + } + ); + return Student; -}; \ No newline at end of file +}; From f439f61a72b6a5b9c1c93115b13089e65c4a8697 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 09:35:22 +0800 Subject: [PATCH 06/47] refactor: migrate User model to ES module syntax --- src/api/Models/user.js | 77 ++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/src/api/Models/user.js b/src/api/Models/user.js index d4686e7..8e6ade1 100644 --- a/src/api/Models/user.js +++ b/src/api/Models/user.js @@ -1,49 +1,46 @@ -'use strict'; -const { Model, DataTypes} = require('sequelize'); - -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; +export default (sequelize) => { class User extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association here + // Define associations here (if needed) } } - User.init({ - username: { - allowNull: false, - unique: true, - type: DataTypes.STRING, - }, - email: { - allowNull: false, - unique: true, - type: DataTypes.STRING, - }, - password: { - type: DataTypes.STRING, - }, - role: { - type: DataTypes.ENUM("STUDENT", "ADMIN", "INSTRUCTOR"), - allowNull: false, - defaultValue: "STUDENT" - }, - is_active: { - type: DataTypes.BOOLEAN, - }, - lastLogin: { - type: DataTypes.DATE, - allowNull: true, + + User.init( + { + username: { + allowNull: false, + unique: true, + type: DataTypes.STRING, + }, + email: { + allowNull: false, + unique: true, + type: DataTypes.STRING, + }, + password: { + type: DataTypes.STRING, + }, + role: { + type: DataTypes.ENUM('STUDENT', 'ADMIN', 'INSTRUCTOR'), + allowNull: false, + defaultValue: 'STUDENT', + }, + is_active: { + type: DataTypes.BOOLEAN, + }, + lastLogin: { + type: DataTypes.DATE, + allowNull: true, + }, }, - }, { - sequelize, - modelName: 'User', - timestamps: true, - }); + { + sequelize, + modelName: 'User', + timestamps: true, + } + ); return User; }; From 28badbc3daff39052365e5218131325ee4a4d4ca Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:08:06 +0800 Subject: [PATCH 07/47] refactor: migrate DepartmentController to ES module syntax and improve query handling in getAllDepartments. --- src/api/Controllers/DepartmentController.js | 70 ++++++++++----------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/src/api/Controllers/DepartmentController.js b/src/api/Controllers/DepartmentController.js index 824ebb2..53dc128 100644 --- a/src/api/Controllers/DepartmentController.js +++ b/src/api/Controllers/DepartmentController.js @@ -1,58 +1,52 @@ -const DepartmentService = require('../Services/DepartmentServices'); -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const { UpdateProgram } = require('./ProgramCourseController'); -const { error } = require('console'); -const DepartmentModel = require('../Models/department')(sequelize, DataTypes) +import DepartmentService from '../Services/DepartmentServices.js'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import { DataTypes } from 'sequelize'; +import { UpdateProgram } from './ProgramCourseController.js'; +import sequelize from '../../../src/config/db.js'; +import defineDepartmentModel from '../Models/department.js'; -exports.getAllDeparments = async(req, res)=>{ - try{ +// Fix __dirname in ES modules +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); - let {top, page, limit} = req.body +// Initialize model +const DepartmentModel = defineDepartmentModel(sequelize, DataTypes); + +// ✅ Get All Departments +export const getAllDepartments = async (req, res) => { + try { + let { top, page, limit } = req.body; // Convert string query params to numbers top = parseInt(top); page = parseInt(page); limit = parseInt(limit); - let queryOptions = { - order: [['createdAt', 'DESC']] // latest first + const queryOptions = { + order: [['createdAt', 'DESC']], // latest first }; if (top) { // Return only top X records queryOptions.limit = top; - } - else if (page && limit) { - // Apply pagination - const offset = (page - 1) * limit; + } else if (page && limit) { queryOptions.limit = limit; - queryOptions.offset = offset; + queryOptions.offset = (page - 1) * limit; } - DepartmentModel.findAll(queryOptions) - .then((departments) => { - res.status(200).json({ - status: 'success', - data: departments.toJson() - }); - }) - .catch((error) => { - res.status(500).json({ - status: 'error', - message: error.message - }); - }); - - }catch(error){ - res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' }); - } -} + const departments = await DepartmentModel.findAll(queryOptions); - -exports.getDepartmentByID = async (req, res) => { + res.status(200).json({ + status: 'success', + data: departments, // No need for toJson() + }); + } catch (error) { + res + .status(error.status || 500) + .json({ error: error.message || 'Internal Server Error' }); + } +}; try { const { id } = req.params; const department = await DepartmentService.getDepartmentByID(id); From eb6a06da816d37ff6d06a5651d2883c0a5d040ec Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:09:48 +0800 Subject: [PATCH 08/47] refactor: update createDepartment function to use ES module syntax and fix variable naming --- src/api/Controllers/DepartmentController.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/api/Controllers/DepartmentController.js b/src/api/Controllers/DepartmentController.js index 53dc128..a842357 100644 --- a/src/api/Controllers/DepartmentController.js +++ b/src/api/Controllers/DepartmentController.js @@ -62,10 +62,11 @@ export const getAllDepartments = async (req, res) => { } }; -exports.createDeparment = async (req, res) => { +// ✅ Create Department +export const createDepartment = async (req, res) => { try { - const addDeparment = await DepartmentService.CreateDeparment(req.body); - if(addDeparment){ + const addDepartment = await DepartmentService.CreateDeparment(req.body); + if (addDepartment) { res.status(201).json({ message: addDeparment.message, data: addDeparment.data From 786f9134a23372399473013ef8f39ce3c4df3aeb Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:10:54 +0800 Subject: [PATCH 09/47] refactor: update updateDepartment function to use ES module syntax --- src/api/Controllers/DepartmentController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/Controllers/DepartmentController.js b/src/api/Controllers/DepartmentController.js index a842357..58473fd 100644 --- a/src/api/Controllers/DepartmentController.js +++ b/src/api/Controllers/DepartmentController.js @@ -77,7 +77,8 @@ export const createDepartment = async (req, res) => { } }; -exports.UpdateDepartment = async (req, res) => { +// ✅ Update Department +export const updateDepartment = async (req, res) => { try { const update = await DepartmentService.UpdateDepartment(req, res); res.status(201).json({ message: update.message }); From 9800a2e62effaf2dc4cd31d8272aeff1282d3eaa Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:12:52 +0800 Subject: [PATCH 10/47] refactor: add getDepartmentByID and deleteDepartment functions with proper export syntax --- src/api/Controllers/DepartmentController.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/api/Controllers/DepartmentController.js b/src/api/Controllers/DepartmentController.js index 58473fd..4076923 100644 --- a/src/api/Controllers/DepartmentController.js +++ b/src/api/Controllers/DepartmentController.js @@ -47,6 +47,8 @@ export const getAllDepartments = async (req, res) => { .json({ error: error.message || 'Internal Server Error' }); } }; +// ✅ Get Department by ID +export const getDepartmentByID = async (req, res) => { try { const { id } = req.params; const department = await DepartmentService.getDepartmentByID(id); @@ -87,11 +89,11 @@ export const updateDepartment = async (req, res) => { } }; -exports.DeleteDepartment = async(req, res)=>{ - try{ - const deleteDepartment = await DepartmentService.DeleteDepartment(req,res) - }catch(error){ +// ✅ Delete Department +export const deleteDepartment = async (req, res) => { + try { + await DepartmentService.DeleteDepartment(req, res); + } catch (error) { res.status(500).json({ message: error.message }); } -} - +}; From 14de92833e8cb024e61420e266f4413a00aabc7a Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:18:24 +0800 Subject: [PATCH 11/47] refactor: migrate InstructorController to ES module syntax and improve import statements --- src/api/Controllers/InstructorController.js | 28 +++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/api/Controllers/InstructorController.js b/src/api/Controllers/InstructorController.js index b4b7d1e..2cef9b2 100644 --- a/src/api/Controllers/InstructorController.js +++ b/src/api/Controllers/InstructorController.js @@ -1,14 +1,22 @@ -const InstructorService = require('../Services/InstructorServices'); -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const { error } = require('console'); -const User = require("../Models/User")(sequelize, DataTypes) -const InstructorModel = require('../Models/instructor')(sequelize, DataTypes) -const { verifyWebToken } = require("../../Middlewares/authMiddleware"); +import InstructorService from "../Services/InstructorServices.js"; +import path from "path"; +import { fileURLToPath } from "url"; +import { DataTypes } from "sequelize"; +import sequelize from "../../config/db.js"; +import UserModelDefiner from "../Models/user.js"; +import InstructorModelDefiner from "../Models/instructor.js"; +import { verifyWebToken } from "../../Middlewares/authMiddleware.js"; -exports.CreateInstructor = async (req, res) => { +// Resolve __dirname (ESM compatible) +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const base = path.resolve(__dirname, "../../../"); + +const User = UserModelDefiner(sequelize, DataTypes); +const Instructor = InstructorModelDefiner(sequelize, DataTypes); + +// ✅ Create Instructor +export const CreateInstructor = async (req, res) => { try { const {id, ...data} = req.body From 5aa7217cc6cdf0d538133af23e82b671ec9b1653 Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:20:24 +0800 Subject: [PATCH 12/47] refactor: update UpdateInstructor and DeleteInstructor functions to use ES module syntax. error message handled properly --- src/api/Controllers/InstructorController.js | 42 ++++++++------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/api/Controllers/InstructorController.js b/src/api/Controllers/InstructorController.js index 2cef9b2..70bf6c9 100644 --- a/src/api/Controllers/InstructorController.js +++ b/src/api/Controllers/InstructorController.js @@ -43,29 +43,19 @@ export const CreateInstructor = async (req, res) => { }); } }; -exports.UpdateInstructor = async(req, res)=>{ - try{ - const update = await InstructorService.UpdateInstructor(req.param.id, req.body); - - return res.status(201).JSON({ - success: true, - message: update.message - }) - }catch(error ){ - throw error; - } -} -exports.DeleteInstructor = async (req, res) => { - try { - const DeleteInstructor = await InstructorService.DeleteInstructor(req.params.id); - return res.status(200).json({ - success:true, - message: "Instructor deleted successfully" - }); - } catch (error) { - return res.status(500).json({ - message: "Error deleting instructor", - error: error.message - }); - } -}; +// ✅ Update Instructor +export const UpdateInstructor = async (req, res) => { + try { + const update = await InstructorService.UpdateInstructor(req.params.id, req.body); + + return res.status(201).json({ + success: true, + message: update.message, + }); + } catch (error) { + return res.status(500).json({ + message: "Error updating instructor", + error: error.message, + }); + } +}; From 3feca0bdb1a6861d98c0d9eb8fbc17668b0bdbcc Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:20:40 +0800 Subject: [PATCH 13/47] refactor: clean up CreateInstructor and UpdateInstructor functions for better readability --- src/api/Controllers/InstructorController.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/api/Controllers/InstructorController.js b/src/api/Controllers/InstructorController.js index 70bf6c9..acd12c1 100644 --- a/src/api/Controllers/InstructorController.js +++ b/src/api/Controllers/InstructorController.js @@ -18,9 +18,7 @@ const Instructor = InstructorModelDefiner(sequelize, DataTypes); // ✅ Create Instructor export const CreateInstructor = async (req, res) => { try { - const {id, ...data} = req.body - const findInstructor = await Instructor.findOne({ where: { user_id: id } }); if (findInstructor) { @@ -59,3 +57,19 @@ export const UpdateInstructor = async (req, res) => { }); } }; +// ✅ Delete Instructor +export const DeleteInstructor = async (req, res) => { + try { + await InstructorService.DeleteInstructor(req.params.id); + + return res.status(200).json({ + success: true, + message: "Instructor deleted successfully", + }); + } catch (error) { + return res.status(500).json({ + message: "Error deleting instructor", + error: error.message, + }); + } +}; From f8909c5dac232ad3cd8e561ffde1686e804836b6 Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:24:29 +0800 Subject: [PATCH 14/47] refactor: migrate ProgramCourseController to ES module syntax and improve imports --- src/api/Controllers/ProgramCourseController.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/api/Controllers/ProgramCourseController.js b/src/api/Controllers/ProgramCourseController.js index 98bd42a..5916435 100644 --- a/src/api/Controllers/ProgramCourseController.js +++ b/src/api/Controllers/ProgramCourseController.js @@ -1,11 +1,15 @@ -const ProgramService = require("../Services/ProgramCourseServices") -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const ProgramCourse = require('../Models/programcourse')(sequelize, DataTypes) +import ProgramService from "../Services/ProgramCourseServices.js"; +import path from "path"; +import { fileURLToPath } from "url"; +import { DataTypes } from "sequelize"; +import sequelize from "../../../src/config/db.js"; +import defineProgramCourse from "../Models/programcourse.js"; -exports.createProgram = async (req, res) => { +const ProgramCourse = defineProgramCourse(sequelize, DataTypes); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +// ✅ Create Program +export const createProgram = async (req, res) => { try { const addProgram = await ProgramService.createProgram(req.body); if(addProgram){ From f06d06e2aa97b4fd050a3b565dd571e7ce44f8c3 Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:30:39 +0800 Subject: [PATCH 15/47] refactor: migrate getAllPrograms and UpdateProgram to ES module syntax --- .../Controllers/ProgramCourseController.js | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/api/Controllers/ProgramCourseController.js b/src/api/Controllers/ProgramCourseController.js index 5916435..63b3107 100644 --- a/src/api/Controllers/ProgramCourseController.js +++ b/src/api/Controllers/ProgramCourseController.js @@ -23,7 +23,8 @@ export const createProgram = async (req, res) => { } }; -exports.getAllPrograms = async (req, res) => { +// ✅ Get All Programs +export const getAllPrograms = async (req, res) => { try { let { top, page, limit } = req.body; @@ -32,8 +33,8 @@ exports.getAllPrograms = async (req, res) => { page = parseInt(page); limit = parseInt(limit); - let queryOptions = { - order: [['createdAt', 'DESC']] // latest first + const queryOptions = { + order: [["createdAt", "DESC"]], }; if (top) { @@ -50,18 +51,20 @@ exports.getAllPrograms = async (req, res) => { const programs = await ProgramCourse.findAll(queryOptions); res.status(200).json({ - status: 'success', - data: programs + status: "success", + data: programs, }); } catch (error) { - res.status(500).json({ - status: 'error', - message: error.message + res.status(500).json({ + status: "error", + message: error.message, }); } }; -exports.UpdateProgram = async (req, res) => { + +// ✅ Update Program +export const UpdateProgram = async (req, res) => { try { const result = await ProgramService.UpdateProgram(req.body); From fdaa8ae1baaa0fd9adec4850495c0bc17755e7cb Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:31:07 +0800 Subject: [PATCH 16/47] refactor: migrate DeleteProgram to ES module syntax --- src/api/Controllers/ProgramCourseController.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/api/Controllers/ProgramCourseController.js b/src/api/Controllers/ProgramCourseController.js index 63b3107..6e458c1 100644 --- a/src/api/Controllers/ProgramCourseController.js +++ b/src/api/Controllers/ProgramCourseController.js @@ -89,6 +89,8 @@ export const UpdateProgram = async (req, res) => { }); } }; -exports.DeleteProgram = async(id)=>{ - return await ProgramService.DeleteProgram(id) -} \ No newline at end of file + +// ✅ Delete Program +export const DeleteProgram = async (id) => { + return await ProgramService.DeleteProgram(id); +}; From b67abd44ae3d86782f919fe46c42390a0bac3b53 Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:38:19 +0800 Subject: [PATCH 17/47] refactor: migrate userController to ES module syntax and improve function exports --- src/api/Controllers/userController.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/api/Controllers/userController.js b/src/api/Controllers/userController.js index 74fecb8..b9f339a 100644 --- a/src/api/Controllers/userController.js +++ b/src/api/Controllers/userController.js @@ -1,6 +1,7 @@ -const userService = require('../Services/UserService'); +import userService from "../Services/UserService.js"; -exports.registerUser = async (req, res) => { +// ✅ Register User +export const registerUser = async (req, res) => { try { const newUser = await userService.registerUser(req.body); if(newUser){ @@ -14,12 +15,12 @@ exports.registerUser = async (req, res) => { } }; -exports.LoginUser = async(req,res)=>{ - try{ - - const isLogin = await userService.LoginUser(req.body) +// ✅ Login User +export const LoginUser = async (req, res) => { + try { + const isLogin = await userService.LoginUser(req.body); - res.cookie('token', isLogin.token, { + res.cookie("token", isLogin.token, { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'strict', @@ -34,4 +35,4 @@ exports.LoginUser = async(req,res)=>{ }catch(error){ res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' }); } -} +}; From 1ed4f272ca88f0bbdbee8cc6a426c623618a9510 Mon Sep 17 00:00:00 2001 From: agaesh Date: Fri, 10 Oct 2025 00:40:20 +0800 Subject: [PATCH 18/47] refactor: streamline LoginUser function by reorganizing cookie setting logic --- src/api/Controllers/userController.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/api/Controllers/userController.js b/src/api/Controllers/userController.js index b9f339a..54774b2 100644 --- a/src/api/Controllers/userController.js +++ b/src/api/Controllers/userController.js @@ -20,18 +20,19 @@ export const LoginUser = async (req, res) => { try { const isLogin = await userService.LoginUser(req.body); - res.cookie("token", isLogin.token, { - httpOnly: true, - secure: process.env.NODE_ENV === 'production', - sameSite: 'strict', - maxAge: 60 * 60 * 1000 - }); - if(isLogin) { - return res.status(200).json({ - message: 'Login successful', + if(isLogin) { + res.cookie("token", isLogin.token, { + httpOnly: true, + secure: process.env.NODE_ENV === 'production', + sameSite: 'strict', + maxAge: 60 * 60 * 1000 }); - } + + return res.status(200).json({ + message: 'Login successful', + }); + } }catch(error){ res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' }); } From 57d32382e1bbd90364d7dbfa30cb8a36267bc95b Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 22:33:19 +0800 Subject: [PATCH 19/47] refactor(routes): convert DepartmentRoutes to ES6 module syntax and fix typos --- src/api/Routes/DepartmentRoutes.js | 32 +++++++++++++++++------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/api/Routes/DepartmentRoutes.js b/src/api/Routes/DepartmentRoutes.js index b0f8b6f..2dd078a 100644 --- a/src/api/Routes/DepartmentRoutes.js +++ b/src/api/Routes/DepartmentRoutes.js @@ -1,21 +1,25 @@ -const express = require('express'); +import express from 'express'; +import DepartmentController from '../Controllers/DepartmentController.js'; +import { Sequelize, DataTypes } from 'sequelize'; + const router = express.Router(); -const DeparmentController = require('../Controllers/DepartmentController') -const { Sequelize, DataTypes } = require('sequelize'); -router.get('/', (req, res)=>{ - res.status(200).send("Welcome To Deparment API"); -}) +router.get('/', (req, res) => { + res.status(200).send('Welcome To Department API'); +}); + +router.get('/listing', async (req, res) => { + await DepartmentController.getAllDeparments(req, res); +}); -router.get('/listing', async(req, res)=>{ - await DeparmentController.getAllDeparments(req,res) -}) -// POST /api/deparment (create program) +// POST /api/department (create department) router.post('/', async (req, res) => { - await DeparmentController.createDeparment(req, res); + await DepartmentController.createDeparment(req, res); }); -// PUT /api/deparment (create program) + +// PUT /api/department (update department) router.put('/', async (req, res) => { - await DeparmentController.UpdateDepartment(req,res) + await DepartmentController.UpdateDepartment(req, res); }); -module.exports = router \ No newline at end of file + +export default router; From 0d9aa4f5ed31e81e37fb2c437d9a6f2b73b23de1 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 22:36:05 +0800 Subject: [PATCH 20/47] refactor(routes): convert InstructorRoutes to ES6 module syntax and improve code consistency -fixed all the remarks for the endpoints --- src/api/Routes/InstructorRoutes.js | 35 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/api/Routes/InstructorRoutes.js b/src/api/Routes/InstructorRoutes.js index 1254035..dd42aa4 100644 --- a/src/api/Routes/InstructorRoutes.js +++ b/src/api/Routes/InstructorRoutes.js @@ -1,26 +1,33 @@ -const express = require('express'); +import express from 'express'; +import InstructorController from '../Controllers/InstructorController.js'; +import validationId from '../Validators/RequiredID.js'; +import validationBody from '../Validators/RequiredBody.js'; + const router = express.Router(); -const InstructorController = require('../Controllers/InstructorController') -const validationId = require('../Validators/RequiredID'); -const validationBody = require('../Validators/RequiredBody') -// GET /api/programs + +// GET /api/instructor/home router.get('/home', (req, res) => { - res.status(200).send("Welcome To Instructor API"); + res.status(200).send('Welcome To Instructor API'); +}); + +// GET /api/instructor +router.get('/', async (req, res) => { + // await InstructorController.getAllInstructor(req, res); }); -router.get('/', async(req, res)=>{ - // await InstructorController.getAllInstructor(req,res) -}) -// POST /api/deparment (create program) +// POST /api/instructor (create instructor) router.post('/', async (req, res) => { await InstructorController.CreateInstructor(req, res); }); -// PUT /api/deparment (create program) + +// PUT /api/instructor/:id (update instructor) router.put('/:id', [...validationId, ...validationBody], async (req, res) => { - await InstructorController.UpdateInstructor(req,res) + await InstructorController.UpdateInstructor(req, res); }); +// DELETE /api/instructor/:id (delete instructor) router.delete('/:id', [...validationId, ...validationBody], async (req, res) => { - await InstructorController.DeleteInstructor(req,res) + await InstructorController.DeleteInstructor(req, res); }); -module.exports = router \ No newline at end of file + +export default router; From 0aba74f1dee8868f9841ca82283be1bdddd05a8d Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 22:37:24 +0800 Subject: [PATCH 21/47] refactor(routes): convert ProgramRoutes to ES6 module syntax and improve code formatting --- src/api/Routes/ProgramRoutes.js | 43 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/api/Routes/ProgramRoutes.js b/src/api/Routes/ProgramRoutes.js index 3bfc93b..72d9d62 100644 --- a/src/api/Routes/ProgramRoutes.js +++ b/src/api/Routes/ProgramRoutes.js @@ -1,35 +1,38 @@ -const express = require('express'); +import express from 'express'; +import programController from '../Controllers/ProgramCourseController.js'; +import { Sequelize, DataTypes } from 'sequelize'; + const router = express.Router(); -const programController = require('../Controllers/ProgramCourseController') -const { Sequelize, DataTypes } = require('sequelize'); // GET /api/programs router.get('/', (req, res) => { - res.status(200).send("Welcome To Program API"); + res.status(200).send('Welcome To Program API'); +}); + +// GET /api/programs/listing +router.get('/listing', async (req, res) => { + await programController.getAllPrograms(req, res); }); -router.get("/listing", async(req,res)=>{ - await programController.getAllPrograms(req,res); -}) -// POST /api/programs (create program) +// POST /api/programs/create (create program) router.post('/create', async (req, res) => { await programController.createProgram(req, res); }); +// PUT /api/programs/update (update program) router.put('/update', async (req, res) => { - await programController.UpdateProgram(req,res) + await programController.UpdateProgram(req, res); }); -// DELETE /delete/:id -router.delete("/delete", async (req, res) => { - try { - const {id} = req.body; // ✅ from body - const result = await programController.DeleteProgram(id); - res.status(200).json(result); - } catch (error) { - res.status(error.status || 500).json({ message: error.message }); - } +// DELETE /api/programs/delete (delete program) +router.delete('/delete', async (req, res) => { + try { + const { id } = req.body; // ✅ from body + const result = await programController.DeleteProgram(id); + res.status(200).json(result); + } catch (error) { + res.status(error.status || 500).json({ message: error.message }); + } }); - -module.exports = router \ No newline at end of file +export default router; \ No newline at end of file From b53d847a9f624c2148277355b3dc03d715737a38 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 22:39:40 +0800 Subject: [PATCH 22/47] refactor(routes): convert userRoute to ES6 module syntax and improve code formatting --- src/api/Routes/userRoute.js | 39 +++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/api/Routes/userRoute.js b/src/api/Routes/userRoute.js index 29bef73..18bf019 100644 --- a/src/api/Routes/userRoute.js +++ b/src/api/Routes/userRoute.js @@ -1,34 +1,39 @@ -const express = require('express'); +import express from 'express'; +import userController from '../Controllers/userController.js'; const router = express.Router(); -const userController = require('../Controllers/userController'); -const { json } = require('sequelize'); -// POST /api/users/register -router.get("/", (req, res) => { - res.status(200).send("Welcome To User API"); +// GET /api/users +router.get('/', (req, res) => { + res.status(200).send('Welcome To User API'); }); + +// POST /api/users/register router.post('/register', async (req, res) => { - await userController.registerUser(req, res); + await userController.registerUser(req, res); }); -router.post('/login', async(req,res)=>{ - await userController.LoginUser(req,res); -}) +// POST /api/users/login +router.post('/login', async (req, res) => { + await userController.LoginUser(req, res); +}); +// POST /api/users/logout router.post('/logout', async (req, res) => { res.clearCookie('token', { httpOnly: true, secure: process.env.NODE_ENV === 'production', - sameSite: 'strict' + sameSite: 'strict', }); res.status(200).json({ message: 'Logged out successfully' }); }); -router.get("/testProtectRoutes", (req, res) => { - console.log("Token from cookie:", req.cookies.token); // ✅ This prints to your terminal - res.status(200).json({ - message: "Protected route accessed", - token: req.cookies.token +// GET /api/users/testProtectRoutes +router.get('/testProtectRoutes', (req, res) => { + console.log('Token from cookie:', req.cookies.token); // ✅ Logs token to terminal + res.status(200).json({ + message: 'Protected route accessed', + token: req.cookies.token, }); }); -module.exports = router; + +export default router; From c5e52d4a12a7e28dbf5845fd3289ab30f9282a60 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 22:58:05 +0800 Subject: [PATCH 23/47] refactor(DepartmentServices): migrate to ES modules and improve error messages --- src/api/Services/DepartmentServices.js | 74 +++++++++++++++----------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/api/Services/DepartmentServices.js b/src/api/Services/DepartmentServices.js index f817080..413e2d6 100644 --- a/src/api/Services/DepartmentServices.js +++ b/src/api/Services/DepartmentServices.js @@ -1,86 +1,96 @@ -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const { error } = require('console'); -const { DeleteDepartment } = require('../Controllers/DepartmentController'); -const DepartmentModel= require('../Models/department')(sequelize, DataTypes); - -exports.getDepartmentById = async (id) => { +import path from 'path'; +import { fileURLToPath } from 'url'; +import { DataTypes } from 'sequelize'; +import sequelize from '../../../src/config/db.js'; +import DepartmentModelFactory from '../Models/department.js'; + +// Required to simulate __dirname in ES modules +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +// Initialize model +const DepartmentModel = DepartmentModelFactory(sequelize, DataTypes); + +// 🟩 Get Department By ID +export const getDepartmentById = async (id) => { if (!id) { - throw new Error("Department ID is required"); + throw new Error('Department ID is required'); } const department = await DepartmentModel.findByPk(id); return department; // null if not found }; -exports.CreateDeparment = async (departmentData) => { + +// 🟩 Create Department +export const CreateDeparment = async (departmentData) => { try { const department = await DepartmentModel.create(departmentData); if (!department) { - throw new Error("Department creation failed"); + throw new Error('Department creation failed'); } return { - message: "Department created successfully", - department: department.toJSON() + message: 'Department created successfully', + department: department.toJSON(), }; } catch (error) { throw error; } }; -exports.UpdateDepartment = async (departmentData) => { - try{ + +// 🟩 Update Department +export const UpdateDepartment = async (departmentData) => { + try { const { department_id, ...updateData } = departmentData; if (department_id === undefined) { - throw new Error("ID must be provided"); + throw new Error('ID must be provided'); } if (Object.keys(updateData).length === 0) { - throw new Error("At-Least One Department fields must be provided to update"); + throw new Error('At least one department field must be provided to update'); } const findExistingDeparment = await DepartmentModel.findOne({ - where: { department_id} + where: { department_id }, }); - if(!findExistingDeparment){ - throw new Error("No Deparment found with the given department id") + if (!findExistingDeparment) { + throw new Error('No Department found with the given department ID'); } const [updatedRows] = await DepartmentModel.update(updateData, { - where: {department_id} + where: { department_id }, }); if (updatedRows > 0) { - return "Department updated successfully"; + return 'Department updated successfully'; } else { - throw new Error("No Department found with the given ID") + throw new Error('No Department found with the given ID'); } - }catch(error){ - throw error + } catch (error) { + throw error; } }; -exports.DeleteDepartment = async (id) => { +// 🟩 Delete Department +export const DeleteDepartment = async (id) => { try { if (id === undefined) { - throw new Error("ID must be provided"); + throw new Error('ID must be provided'); } const deletedRows = await DepartmentModel.destroy({ - where: { department_id: id } + where: { department_id: id }, }); if (deletedRows > 0) { - return "Department deleted successfully"; + return 'Department deleted successfully'; } else { - throw new Error("No department found with the given ID") + throw new Error('No department found with the given ID'); } } catch (error) { throw error; } }; - From a980a5adb01c87e623f1df3875fe8766483b1492 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 23:00:02 +0800 Subject: [PATCH 24/47] refactor(InstructorServices): migrate to ES modules and improve code structure --- src/api/Services/InstructorServices.js | 109 ++++++++++++++----------- 1 file changed, 62 insertions(+), 47 deletions(-) diff --git a/src/api/Services/InstructorServices.js b/src/api/Services/InstructorServices.js index bd0e4fe..989c026 100644 --- a/src/api/Services/InstructorServices.js +++ b/src/api/Services/InstructorServices.js @@ -1,75 +1,90 @@ -const { error } = require('console'); -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const Instructor = require('../Models/instructor')(sequelize, DataTypes) -const ProgramCourse = require("../Models/programcourse")(sequelize, DataTypes) +import path from 'path'; +import { fileURLToPath } from 'url'; +import { DataTypes } from 'sequelize'; +import sequelize from '../../../src/config/db.js'; +import InstructorModelFactory from '../Models/instructor.js'; +import ProgramCourseModelFactory from '../Models/programcourse.js'; +// 🔧 Setup __dirname for ES modules +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); -exports.CreateInstructor= async(InstructorData)=>{ - try{ +// 🧩 Initialize models +const Instructor = InstructorModelFactory(sequelize, DataTypes); +const ProgramCourse = ProgramCourseModelFactory(sequelize, DataTypes); + +// 🟩 Create Instructor +export const CreateInstructor = async (InstructorData) => { + try { const instructor = await Instructor.create(InstructorData); - + return { success: true, - message: "Instructor created successfully", - data: instructor.toJSON() + message: 'Instructor created successfully', + data: instructor.toJSON(), }; - }catch(error){ + } catch (error) { throw error; } -} -exports.UpdateInstructor = async (id,InstructorData) => { +}; + +// 🟩 Update Instructor +export const UpdateInstructor = async (id, InstructorData) => { try { - // Find instructor by primary key (id) const instructor = await Instructor.findByPk(id); if (!instructor) { - const error = new Error("Instructor not found with the given id"); - error.status = 404; // attach custom property + const error = new Error('Instructor not found with the given id'); + error.status = 404; throw error; } - // Update instructor with new data - const update = await instructor.update(InstructorData); - - if(update != undefined){ - return { + const updatedInstructor = await instructor.update(InstructorData); + + if (updatedInstructor) { + return { success: true, - message: "Instructor updated successfully", - data: updateFields.toJSON() - }; + message: 'Instructor updated successfully', + data: updatedInstructor.toJSON(), + }; + } else { + throw new Error('Instructor update failed'); } } catch (error) { throw error; } }; -exports.DeleteInstructor = async(InstructorData)=>{ - try{ - - // Check courses - const courseCount = await ProgramCourse.count({ where: { instructor_Id: id, type: 'course'} }); +// 🟩 Delete Instructor +export const DeleteInstructor = async (id) => { + try { + // Check assigned courses + const courseCount = await ProgramCourse.count({ + where: { instructor_Id: id, type: 'course' }, + }); - if (courseCount > 0) { - throw new Error("Cannot delete instructor: assigned to courses"); - } + if (courseCount > 0) { + throw new Error('Cannot delete instructor: assigned to courses'); + } - // Check programs - const programs = await Instructor.findByPk(id, { include: Program }); + // Check assigned programs + const programs = await Instructor.findByPk(id, { include: 'Programs' }); - if (programs.Programs && programs.Programs.length > 0) { - throw new Error("Cannot delete instructor: assigned to programs"); - } + if (programs?.Programs?.length > 0) { + throw new Error('Cannot delete instructor: assigned to programs'); + } - const deleteInstructor = await Instructor.destroy(InstructorData) + const deletedRows = await Instructor.destroy({ where: { instructor_Id: id } }); - return{ - success:true, - message: "Instructor Deleted Successfully" - } - }catch(error){ - throw error; + if (deletedRows > 0) { + return { + success: true, + message: 'Instructor deleted successfully', + }; + } else { + throw new Error('Instructor not found or already deleted'); } -} + } catch (error) { + throw error; + } +}; From f45768fe24c2e4034b5d4c78899ad85bf617c2df Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 23:01:21 +0800 Subject: [PATCH 25/47] refactor(ProgramCourseServices): migrate to ES modules and improve code structure --- src/api/Services/ProgramCourseServices.js | 55 ++++++++++++++--------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/api/Services/ProgramCourseServices.js b/src/api/Services/ProgramCourseServices.js index 8be1980..b5f043e 100644 --- a/src/api/Services/ProgramCourseServices.js +++ b/src/api/Services/ProgramCourseServices.js @@ -1,20 +1,29 @@ -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const ProgramCourse = require('../Models/programcourse')(sequelize, DataTypes) -exports.createProgram = async (programData) => { +import path from 'path'; +import { fileURLToPath } from 'url'; +import { DataTypes } from 'sequelize'; +import sequelize from '../../../src/config/db.js'; +import ProgramCourseModelFactory from '../Models/programcourse.js'; + +// 🔧 Setup __dirname for ES modules +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +// 🧩 Initialize model +const ProgramCourse = ProgramCourseModelFactory(sequelize, DataTypes); + +// 🟩 Create Program +export const createProgram = async (programData) => { try { // Check if program with same code already exists const existingProgram = await ProgramCourse.findOne({ - where: { code: programData.code } + where: { code: programData.code }, }); if (existingProgram) { const error = new Error('Program code already exists'); error.status = 400; throw error; - } + } // Create new program const newProgram = await ProgramCourse.create({ @@ -27,7 +36,7 @@ exports.createProgram = async (programData) => { semester: programData.semester || null, level: programData.level, is_active: programData.is_active ?? true, - mode: programData.mode || 'FULLTIME' + mode: programData.mode || 'FULLTIME', }); return newProgram; @@ -36,39 +45,43 @@ exports.createProgram = async (programData) => { } }; -exports.DeleteProgram = async (id) => { +// 🟩 Delete Program +export const DeleteProgram = async (id) => { + try { const findProgram = await ProgramCourse.findOne({ where: { id } }); if (!findProgram) { - const error = new Error("Program cannot be found"); - error.status = 404; - throw error; + const error = new Error('Program cannot be found'); + error.status = 404; + throw error; } await findProgram.destroy(); - - return { message: "Program deleted successfully" }; + return { message: 'Program deleted successfully' }; + } catch (error) { + throw error; + } }; -exports.UpdateProgram = async (programData) => { +// 🟩 Update Program +export const UpdateProgram = async (programData) => { try { const { id, ...updateFields } = programData; if (!id) { - throw new Error("Program ID is required to update"); + throw new Error('Program ID is required to update'); } const [rowsUpdated] = await ProgramCourse.update(updateFields, { - where: { id } + where: { id }, }); if (rowsUpdated === 0) { - return { success: false, message: "No program found with the given ID" }; + return { success: false, message: 'No program found with the given ID' }; } - return { success: true, message: "Program updated successfully" }; + return { success: true, message: 'Program updated successfully' }; } catch (error) { return { success: false, message: error.message }; } }; - From 1f5fd8ef279686c6556b20e1d516a6eebff6359d Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 23:04:08 +0800 Subject: [PATCH 26/47] refactor(StudentService): migrate to ES modules and improve code structure --- src/api/Services/StudentService.js | 55 ++++++++++++++++-------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/api/Services/StudentService.js b/src/api/Services/StudentService.js index e707c32..6461ff3 100644 --- a/src/api/Services/StudentService.js +++ b/src/api/Services/StudentService.js @@ -1,14 +1,18 @@ +import bcrypt from "bcrypt"; +import path from "path"; +import { fileURLToPath } from "url"; +import { DataTypes } from "sequelize"; +import sequelize from "../../../src/config/db.js"; +import StudentModel from "../Models/Student.js"; +import User from "../Models/user.js"; -const bcrypt = require("bcrypt"); -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const user = require("../Models/user"); -const Student = require('../Models/Student')(sequelize, DataTypes); +// Resolve __dirname for ES modules +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const Student = StudentModel(sequelize, DataTypes); -exports.createStudent = async (studentData) => { +export const createStudent = async (studentData) => { try { const newStudent = await Student.create(studentData); return newStudent; @@ -17,7 +21,7 @@ exports.createStudent = async (studentData) => { } }; -exports.getAllStudents = async () => { +export const getAllStudents = async () => { try { const students = await Student.findAll(); return students; @@ -26,11 +30,11 @@ exports.getAllStudents = async () => { } }; -exports.getStudentById = async (id) => { +export const getStudentById = async (id) => { try { const student = await Student.findByPk(id); if (!student) { - const error = new Error('Student not found'); + const error = new Error("Student not found"); error.status = 404; throw error; } @@ -40,11 +44,11 @@ exports.getStudentById = async (id) => { } }; -exports.updateStudent = async (id, updatedData) => { +export const updateStudent = async (id, updatedData) => { try { const student = await Student.findByPk(id); if (!student) { - const error = new Error('Student not found'); + const error = new Error("Student not found"); error.status = 404; throw error; } @@ -56,40 +60,39 @@ exports.updateStudent = async (id, updatedData) => { } }; -exports.deleteStudent = async (id) => { +export const deleteStudent = async (id) => { try { const deleted = await Student.destroy({ where: { student_id: id } }); if (!deleted) { - const error = new Error('Student not found or already deleted'); + const error = new Error("Student not found or already deleted"); error.status = 404; throw error; } - return { message: 'Student deleted successfully' }; + return { message: "Student deleted successfully" }; } catch (error) { throw error; } }; -exports.changePassword = async(email, password, confirmpass) =>{ - try{ - +export const changePassword = async (email, password, confirmpass) => { + try { if (password !== confirmpass) { - throw new Error('Passwords do not match'); + throw new Error("Passwords do not match"); } + const hashedPassword = await bcrypt.hash(password, 10); - // Update user by email - const [updatedRows] = await user.update( + const [updatedRows] = await User.update( { password: hashedPassword }, { where: { email } } ); if (updatedRows === 0) { - throw new Error('User not found or password not updated'); + throw new Error("User not found or password not updated"); } - return { message: 'Password updated successfully' }; - }catch(error){ + return { message: "Password updated successfully" }; + } catch (error) { throw error; } -} \ No newline at end of file +}; From 4ba7bb3b959b3bed7af6606e5f721ce17d18b74b Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 23:07:02 +0800 Subject: [PATCH 27/47] refactor(UserService): migrate to ES modules and improve code structure --- src/api/Services/UserService.js | 104 ++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 44 deletions(-) diff --git a/src/api/Services/UserService.js b/src/api/Services/UserService.js index 328e666..3fb3feb 100644 --- a/src/api/Services/UserService.js +++ b/src/api/Services/UserService.js @@ -1,70 +1,86 @@ -const bcrypt = require("bcrypt"); -const path = require('path'); -const base = path.resolve(__dirname, '../../../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const jwt = require('jsonwebtoken'); -const User = require('../Models/user')(sequelize, DataTypes); +import bcrypt from "bcrypt"; +import jwt from "jsonwebtoken"; +import path from "path"; +import { fileURLToPath } from "url"; +import { DataTypes } from "sequelize"; +import sequelize from "../../../src/config/db.js"; +import UserModel from "../Models/user.js"; -exports.registerUser = async (body) => { +// Resolve __dirname for ES module +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const User = UserModel(sequelize, DataTypes); + +// ✅ Register User +export const registerUser = async (body) => { try { - // Handle user registration - const username = body.username; - const email = body.email; - let password = body.password; + const { username, email, password } = body; - // Call DB or validation logic + // Check existing user const existingUser = await User.findOne({ where: { email } }); if (existingUser) { - const error = new Error('Email already in use'); + const error = new Error("Email already in use"); error.status = 409; throw error; } - const hashedPassword = await bcrypt.hash(password, 10) - password = hashedPassword - const newUser = await User.create({ username, email, password }); - return newUser; // controller will format and send response - + // Hash password + const hashedPassword = await bcrypt.hash(password, 10); + + // Create new user + const newUser = await User.create({ + username, + email, + password: hashedPassword, + }); + + return newUser; } catch (error) { throw error; } }; -exports.LoginUser = async(body)=>{ - try{ - - const email = body. email; - const password = body.password; - const findUser = await User.findOne({ - where: { email } - }); - - const validatePassword = await bcrypt.compare(password, findUser.password) +// ✅ Login User +export const loginUser = async (body) => { + try { + const { email, password } = body; - if(!findUser){ - const error = new Error('Email does not exist'); + // Find user + const findUser = await User.findOne({ where: { email } }); + if (!findUser) { + const error = new Error("Email does not exist"); error.status = 401; - throw error + throw error; } - if(!validatePassword){ - const error = new Error('User Account does not exist. Please Register your account' ) + + // Validate password + const validatePassword = await bcrypt.compare(password, findUser.password); + if (!validatePassword) { + const error = new Error( + "Invalid credentials. Please check your password or register an account." + ); error.status = 400; - throw error + throw error; } - const token = jwt.sign({ id: findUser.id, email: findUser.email }, process.env.JWT_SECRET, { - expiresIn: '1h' - }); + // Generate token + const token = jwt.sign( + { id: findUser.id, email: findUser.email }, + process.env.JWT_SECRET, + { expiresIn: "1h" } + ); + + // Return token + sanitized user + const { password: _, ...userData } = findUser.toJSON(); - // Return both token and user data (excluding password) return { + success: true, + message: "Login successful", token, - findUser + user: userData, }; - - }catch(error){ + } catch (error) { throw error; } -} - +}; From e7dfeabfeabf5a77b58a52a9afeb18ac6104b560 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 23:49:16 +0800 Subject: [PATCH 28/47] refactor: convert RequiredBody.js to use ES6 import/export syntax --- src/api/Validators/RequiredBody.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/api/Validators/RequiredBody.js b/src/api/Validators/RequiredBody.js index 90ec6af..5cdd112 100644 --- a/src/api/Validators/RequiredBody.js +++ b/src/api/Validators/RequiredBody.js @@ -1,4 +1,4 @@ -const { body, validationResult } = require('express-validator'); +import { body, validationResult } from 'express-validator'; const requiredBody = [ body().custom((value, { req }) => { @@ -8,4 +8,5 @@ const requiredBody = [ return true; }) ]; -module.exports = requiredBody \ No newline at end of file + +export default requiredBody; From 2049f2107f96f20eef60ea2a776a72d386c71eb8 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sat, 11 Oct 2025 23:50:28 +0800 Subject: [PATCH 29/47] refactor: convert RequiredID.js to use ES6 import/export syntax --- src/api/Validators/RequiredID.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/Validators/RequiredID.js b/src/api/Validators/RequiredID.js index 3386cef..f3fb66f 100644 --- a/src/api/Validators/RequiredID.js +++ b/src/api/Validators/RequiredID.js @@ -1,4 +1,4 @@ -const { param, validationResult } = require('express-validator'); +import { param, validationResult } from 'express-validator'; const RequiredId = [ // Check the id param @@ -19,4 +19,4 @@ const RequiredId = [ } ]; -module.exports = RequiredId; +export default RequiredId; From 9c83530134e66bbf9077fa807601754a9fc0833b Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 07:49:38 +0800 Subject: [PATCH 30/47] refactor(config): update to ES6 module syntax and enhance comments for clarity --- src/Config/config.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Config/config.js b/src/Config/config.js index 7f60c0f..22cad9b 100644 --- a/src/Config/config.js +++ b/src/Config/config.js @@ -1,9 +1,23 @@ -module.exports = { +/** + * Sequelize database configuration for the 'development' environment. + * This file uses ES6 module syntax (`export default`). + */ + +export default { development: { + // Uses environment variable DB_USER, falling back to 'root' username: process.env.DB_USER || 'root', + + // Uses environment variable DB_PASS, falling back to null (no password) password: process.env.DB_PASS || null, + + // Uses environment variable DB_NAME, falling back to 'EnrolNow' database: process.env.DB_NAME || 'EnrolNow', + + // Uses environment variable DB_HOST, falling back to '127.0.0.1' (localhost) host: process.env.DB_HOST || '127.0.0.1', + + // Specifies the database dialect dialect: 'mysql' } }; \ No newline at end of file From fe5a4ca7fa0d1b260df69ea990f15cb873b7b352 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 07:52:25 +0800 Subject: [PATCH 31/47] refactor(db): migrate to ES6 module syntax and update configuration import --- src/Config/db.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Config/db.js b/src/Config/db.js index ec99cbb..703e8bb 100644 --- a/src/Config/db.js +++ b/src/Config/db.js @@ -1,7 +1,17 @@ -// db.js -const { Sequelize } = require('sequelize'); -const config = require('./config').development; +/** + * Sequelize database connection setup using ES6 modules. + * It imports the database configuration from './database.config.js'. + */ +import { Sequelize } from 'sequelize'; +// Import the default export (the configuration object) from the config file. +// Note: The original CJS referred to './config', but we use the actual file name. +import configModule from './database.config.js'; + +// Destructure the specific 'development' environment configuration. +const config = configModule.development; + +// Initialize Sequelize instance const sequelize = new Sequelize( config.database, config.username, @@ -11,5 +21,5 @@ const sequelize = new Sequelize( dialect: config.dialect, } ); - -module.exports = sequelize; \ No newline at end of file +// Export the initialized Sequelize instance +export default sequelize; From ed0cd94ed4a86fce038c5ac10123b543a8f55047 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 08:10:42 +0800 Subject: [PATCH 32/47] refactor(config): migrate to ES module syntax and reorganize imports for clarity --- src/Config/index.js | 58 +++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/Config/index.js b/src/Config/index.js index 08ea05e..486c42e 100644 --- a/src/Config/index.js +++ b/src/Config/index.js @@ -1,30 +1,52 @@ -global.__basedir = __dirname; -const express = require('express'); +// --- ES Module Setup for __dirname and __filename --- +// Import necessary modules for path resolution in ES modules +import path from 'path'; +import { fileURLToPath } from 'url'; + +// Derive ES module equivalents for CJS context +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +global.__basedir = __dirname; // Set the global basedir + +// --- Imports --- +import express from 'express'; +import cookieParser from 'cookie-parser'; +import { config } from 'dotenv'; + +// Initialize dotenv configuration immediately +config(); + +// Service Imports (Note: Must include .js extension for local modules) +import UserRegister from "../api/Services/UserService.js"; +import ProgramService from '../api/Services/ProgramCourseServices.js'; + +// Route Imports (Note: Must include .js extension for local modules) +import userRoutes from '../api/Routes/userRoute.js'; +import ProgramRoutes from '../api/Routes/ProgramRoutes.js'; +import departmentRoutes from '../api/Routes/DepartmentRoutes.js'; +import InstructorRoutes from "../api/Routes/InstructorRoutes.js"; + +// --- Application Setup --- const app = express(); const port = 3000; -const cookieparser = require('cookie-parser'); -app.use(express.json()); -app.use(cookieparser()); -require('dotenv').config(); -const UserRegister = require("../api/Services/UserService"); -const ProgramService = require('../api/Services/ProgramCourseServices') -// Require the router -const userRoutes = require('../api/Routes/userRoute'); -const ProgramRoutes = require('../api/Routes/ProgramRoutes'); -const departmentRoutes = require('../api/Routes/DepartmentRoutes') -const InstructorRoutes = require("../api/Routes/InstructorRoutes") +// Middleware +app.use(express.json()); +app.use(cookieParser()); +app.use(express.urlencoded({ extended: true })); // Example route app.get('/', (req, res) => { res.send('Hello from Express API!'); }); -app.use(express.urlencoded({ extended: true })); -// Use the router at a mount path + +// Use the routers at their mount paths app.use('/users', userRoutes); -app.use('/program', ProgramRoutes) -app.use('/department', departmentRoutes) -app.use("/instructor", InstructorRoutes) +app.use('/program', ProgramRoutes); +app.use('/department', departmentRoutes); +app.use("/instructor", InstructorRoutes); + +// --- Start Server --- app.listen(port, () => { console.log(`Express API listening at http://localhost:${port}`); }); \ No newline at end of file From a5ac6864530e036573c6eb4db38c237380a1612b Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 08:33:55 +0800 Subject: [PATCH 33/47] fix(package): add "type": "module" to enable ES module support --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 071a9f1..f9d5880 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "enroll", "version": "1.0.0", "description": "Student Management System", + "type": "module", "main": "index.js", "scripts": { "start": "node ./src/config/index.js", From f34d5a7246be3684aa025c45b2c81eaa182fc63b Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 09:02:35 +0800 Subject: [PATCH 34/47] fix(auth): correct token retrieval from cookies and update export syntax --- src/Middlewares/authMiddleware.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Middlewares/authMiddleware.js b/src/Middlewares/authMiddleware.js index ec05e31..576e1dd 100644 --- a/src/Middlewares/authMiddleware.js +++ b/src/Middlewares/authMiddleware.js @@ -1,16 +1,17 @@ -const jwt = require('jsonwebtoken') +import jwt from 'jsonwebtoken'; -exports.verifyWebToken =(req, res, next)=>{ - const token = res.cookie.token; +export const verifyWebToken = (req, res, next) => { + const token = req.cookies?.token; // corrected from res.cookie.token → req.cookies.token - if(!token){ - return res.status(501).json({message:"No access"}) - } - try { - const decoded = jwt.verify(token, process.env.JWT_SECRET); - req.user = decoded; // attach user data to request - next(); - } catch (err) { - res.status(401).json({ message: 'Invalid or expired token.' }); - } -} \ No newline at end of file + if (!token) { + return res.status(501).json({ message: 'No access' }); + } + + try { + const decoded = jwt.verify(token, process.env.JWT_SECRET); + req.user = decoded; // attach user data to request + next(); + } catch (err) { + res.status(401).json({ message: 'Invalid or expired token.' }); + } +}; From 2c17edcab9944ee49ecf91c6018e3516f5ebdd23 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 09:30:00 +0800 Subject: [PATCH 35/47] refactor(tests): update department tests to use ES module syntax and improve error handling --- tests/department_test.js | 55 ++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/tests/department_test.js b/tests/department_test.js index 40d61a6..288f16b 100644 --- a/tests/department_test.js +++ b/tests/department_test.js @@ -1,11 +1,12 @@ // tests/DepartmentService.test.js import { describe, it, expect, beforeEach, vi } from 'vitest'; -const path = require('path'); -const base = path.resolve(__dirname, '../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const DepartmentModel = require('../src/api/Models/department')(sequelize, DataTypes) -const DepartmentService = require('../src/api/Services/DepartmentServices') +import path from 'path'; +import sequelize from '../src/Config/db.js' +import { DataTypes } from 'sequelize'; +import DepartmentModelFactory from '../src/api/Models/department.js'; +import * as DepartmentService from '../src/api/Services/DepartmentServices.js'; + +const DepartmentModel = DepartmentModelFactory(sequelize, DataTypes); describe('DepartmentService - CreateDepartment', () => { const mockDepartmentData = { @@ -21,16 +22,12 @@ describe('DepartmentService - CreateDepartment', () => { }); it('should create a department successfully', async () => { - // Mock Sequelize create method -// vi.spyOn(DepartmentModel, 'create').mockResolvedValue(null); - const result = await DepartmentService.CreateDeparment(mockDepartmentData); expect(result).toEqual({ message: 'Department created successfully', department: result.department }); - // expect(DepartmentModel.create).toHaveBeenCalledWith(mockDepartmentData); }); it('should throw error if creation fails due to empty department code', async () => { @@ -39,9 +36,6 @@ describe('DepartmentService - CreateDepartment', () => { department_code: '' // force empty code }; - // // Mock create to simulate DB returning null - // vi.spyOn(DepartmentModel, 'create').mockResolvedValue(null); - await expect(DepartmentService.CreateDeparment(invalidDepartmentData)) .rejects .toThrow('Validation error'); @@ -65,26 +59,24 @@ describe("UpdateDepartment Service", () => { ).rejects.toThrow("At-Least One Department fields must be provided to update"); }); -it("should throw error if department not found", async () => { - vi.spyOn(DepartmentModel, "findOne").mockResolvedValue(null); - await expect( - DepartmentService.UpdateDepartment({ - department_id: 123, // make sure this matches your service - department_code:"DDSF", - department_name: "Finance", - }) - ).rejects.toThrow("No Deparment found with the given department id"); -}); + it("should throw error if department not found", async () => { + vi.spyOn(DepartmentModel, "findOne").mockResolvedValue(null); + await expect( + DepartmentService.UpdateDepartment({ + department_id: 123, + department_code:"DDSF", + department_name: "Finance", + }) + ).rejects.toThrow("No Deparment found with the given department id"); + }); it("should update department successfully using real DB record", async () => { - // First, get an existing department from the database - const existingDepartment = await DepartmentModel.findOne(); // fetch first available record + const existingDepartment = await DepartmentModel.findOne(); if (!existingDepartment) { throw new Error("No department found in DB to test update"); } - // Update the department name - const newName = "Finance_" + Date.now(); // to avoid duplicate names + const newName = "Finance_" + Date.now(); const result = await DepartmentService.UpdateDepartment({ department_id: existingDepartment.department_id, department_name: newName, @@ -92,13 +84,13 @@ it("should throw error if department not found", async () => { expect(result).toBe("Department updated successfully"); - // Verify the update in DB const updatedDepartment = await DepartmentModel.findOne({ where: { department_id: existingDepartment.department_id }, }); expect(updatedDepartment.department_name).toBe(newName); }); }); + describe("Delete-Department", () => { it("should throw 'ID Must Be Provided'", async () => { await expect(DepartmentService.DeleteDepartment()) @@ -106,8 +98,9 @@ describe("Delete-Department", () => { .toThrow("ID must be provided"); }); - it("should throw 'No department found with the given ID'", async()=>{ + it("should throw 'No department found with the given ID'", async () => { await expect(DepartmentService.DeleteDepartment(1231)) - .rejects.toThrow("No department found with the given ID"); - }) + .rejects + .toThrow("No department found with the given ID"); + }); }); From 7a3bf4e5cb3f692bb3b47240651c4a660f46ecbd Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 09:33:08 +0800 Subject: [PATCH 36/47] refactor(tests): update Instructor tests to use ES module syntax and improve structure --- tests/Instructor.test.js | 180 ++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 96 deletions(-) diff --git a/tests/Instructor.test.js b/tests/Instructor.test.js index db83671..0e35ac5 100644 --- a/tests/Instructor.test.js +++ b/tests/Instructor.test.js @@ -1,106 +1,94 @@ -// tests/DepartmentService.test.js -import { describe, it, expect, beforeEach, vi } from 'vitest'; -import { options } from '../src/api/Routes/InstructorRoutes'; -import { readSync } from 'fs'; -const path = require('path'); -const base = path.resolve(__dirname, '../'); -const sequelize = require(path.join(base, 'src', 'config', 'db.js')); -const { DataTypes } = require('sequelize'); -const InstructorModel = require('../src/api/Models/instructor')(sequelize, DataTypes); -const InstructorService = require('../src/api/Services/InstructorServices'); -const newInstructor = undefined - -describe("Instructor Create", ()=>{ - const mockInstructorData = { - first_name: 'Kirukkan', - last_name: 'Kirruki', - email: 'Kirrukki@gmail.com', - phone_number: '017-7454678', - hire_date: Date.now(), - department: null - }; - - beforeEach(() => { - // reset all mocks before each test - vi.restoreAllMocks(); - }); - - it('should create a successfully', async () => { - // Mock Sequelize create method - // vi.spyOn(DepartmentModel, 'create').mockResolvedValue(null); - - const result = await InstructorService.CreateInstructor(mockInstructorData); - - - expect(result).toEqual({ - success: true, - message: "Instructor created successfully", - data: result.data, - }); - // expect(DepartmentModel.create).toHaveBeenCalledWith(mockDepartmentData); - }); -}) - -describe('test update method of', ()=>{ - const mockInstructorData = { - first_name: 'Kiruskkan', - last_name: 'Kirruki', - email: 'Kirrukki@gmais.com', - phone_number: '017-7454678', - hire_date: Date.now(), - department: null - }; - - beforeEach(() => { - // reset all mocks before each test - vi.restoreAllMocks(); - }); - - it('should throw error when Instructor not found with the given id', async () => { - // Mock Sequelize findByPk to return null - vi.spyOn(InstructorModel, 'findByPk').mockResolvedValue(null); - - // Call service and expect rejection - await expect( - InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) - ).rejects.toThrow("Instructor not found with the given id"); - - // Optional: also check the custom status property - await expect( - InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) - ).rejects.toMatchObject({ status: 404 }); - }); - - - it("should succesfull update the Instructor Data"), async() =>{ - const newInstructor = await InstructorModel.create({ - first_name: 'Maths Teacher', - last_name: 'Kayalvizhi', - email: 'Kayal@gmais.com', - phone_number: '017-7454678', - hire_date: Date.now(), - department: null +// tests/InstructorService.test.js +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; +import path from 'path'; +import sequelize from '../src/Config/db.js'; +import { DataTypes } from 'sequelize'; +import InstructorModelFactory from '../src/api/Models/instructor.js'; +import * as InstructorService from '../src/api/Services/InstructorServices.js'; + +const InstructorModel = InstructorModelFactory(sequelize, DataTypes); +let newInstructor; + +describe("Instructor Create", () => { + const mockInstructorData = { + first_name: 'Kirukkan', + last_name: 'Kirruki', + email: 'Kirrukki@gmail.com', + phone_number: '017-7454678', + hire_date: Date.now(), + department: null + }; + + beforeEach(() => { + vi.restoreAllMocks(); + }); + + it('should create successfully', async () => { + const result = await InstructorService.CreateInstructor(mockInstructorData); + + expect(result).toEqual({ + success: true, + message: "Instructor created successfully", + data: result.data, }); + }); +}); + +describe("Instructor Update", () => { + const mockInstructorData = { + first_name: 'Kiruskkan', + last_name: 'Kirruki', + email: 'Kirrukki@gmais.com', + phone_number: '017-7454678', + hire_date: Date.now(), + department: null + }; + + beforeEach(() => { + vi.restoreAllMocks(); + }); + + it('should throw error when instructor not found with the given id', async () => { + vi.spyOn(InstructorModel, 'findByPk').mockResolvedValue(null); + + await expect( + InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) + ).rejects.toThrow("Instructor not found with the given id"); + + await expect( + InstructorService.UpdateInstructor(999, { name: 'Mock Instructor' }) + ).rejects.toMatchObject({ status: 404 }); + }); + + it("should successfully update the instructor data", async () => { + newInstructor = await InstructorModel.create({ + first_name: 'Maths Teacher', + last_name: 'Kayalvizhi', + email: 'Kayal@gmais.com', + phone_number: '017-7454678', + hire_date: Date.now(), + department: null + }); + + const mockInstructorId = newInstructor.id; + + const result = await InstructorService.UpdateInstructor(mockInstructorId, mockInstructorData); - const mockInstructorId = newInstructor.id; // 👈 real generated id - - const result = await InstructorService.UpdateInstructor(mockInstructorData); expect(result).toEqual({ - success:true, + success: true, message: "Instructor updated successfully", data: expect.objectContaining(mockInstructorData) }); - console.log(result) - // Fetch from DB again to ensure it's really updated - const updatedInstructor = await Instructor.findByPk(mockInstructorId); + const updatedInstructor = await InstructorModel.findByPk(mockInstructorId); expect(updatedInstructor).not.toBeNull(); expect(updatedInstructor.toJSON()).toEqual(expect.objectContaining(mockInstructorData)); + }); + + afterEach(async () => { + if (newInstructor) { + await InstructorModel.destroy({ where: { id: newInstructor.id } }); + newInstructor = undefined; } - afterEach(async () => { - // Delete the test record to clean DB - if (newInstructor) { - await InstructorModel.destroy({ where: { id: newInstructor.id } }); - } - }); -}) \ No newline at end of file + }); +}); From c463583a9efda96034c4537c3f3bc130ac73679d Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 09:36:51 +0800 Subject: [PATCH 37/47] added department_test.test.js. and old department_test.js have been deleted. Just changed the file type --- tests/{department_test.js => department_test.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{department_test.js => department_test.test.js} (100%) diff --git a/tests/department_test.js b/tests/department_test.test.js similarity index 100% rename from tests/department_test.js rename to tests/department_test.test.js From 2d4c88ef2998ff3c1cdd09fbf70ba650ec0301c3 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 09:59:02 +0800 Subject: [PATCH 38/47] refactor - importing config should be same as importing in another. it is case sensitive. so handled everywhere same --- src/Config/db.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Config/db.js b/src/Config/db.js index 703e8bb..f52c18a 100644 --- a/src/Config/db.js +++ b/src/Config/db.js @@ -6,7 +6,7 @@ import { Sequelize } from 'sequelize'; // Import the default export (the configuration object) from the config file. // Note: The original CJS referred to './config', but we use the actual file name. -import configModule from './database.config.js'; +import configModule from '../config/config.js'; // Destructure the specific 'development' environment configuration. const config = configModule.development; From 2837ad002a78d97ed6556ac77e77628ae253831e Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 10:01:04 +0800 Subject: [PATCH 39/47] refactor - fixed sequelize variable path casing problem for confiq. fixed by changing to Config from config --- src/api/Services/StudentService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/Services/StudentService.js b/src/api/Services/StudentService.js index 6461ff3..3423d6c 100644 --- a/src/api/Services/StudentService.js +++ b/src/api/Services/StudentService.js @@ -2,7 +2,7 @@ import bcrypt from "bcrypt"; import path from "path"; import { fileURLToPath } from "url"; import { DataTypes } from "sequelize"; -import sequelize from "../../../src/config/db.js"; +import sequelize from "../../../src/Config/db.js"; import StudentModel from "../Models/Student.js"; import User from "../Models/user.js"; From 8086c3e7f8c535699fd9842af3f475a103ab6a39 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 10:03:17 +0800 Subject: [PATCH 40/47] Casing changed to Config From Config --- src/api/Controllers/DepartmentController.js | 2 +- src/api/Controllers/ProgramCourseController.js | 2 +- src/api/Models/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/Controllers/DepartmentController.js b/src/api/Controllers/DepartmentController.js index 4076923..ca8e2a4 100644 --- a/src/api/Controllers/DepartmentController.js +++ b/src/api/Controllers/DepartmentController.js @@ -3,7 +3,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import { DataTypes } from 'sequelize'; import { UpdateProgram } from './ProgramCourseController.js'; -import sequelize from '../../../src/config/db.js'; +import sequelize from '../../../src/Config/db.js'; import defineDepartmentModel from '../Models/department.js'; // Fix __dirname in ES modules diff --git a/src/api/Controllers/ProgramCourseController.js b/src/api/Controllers/ProgramCourseController.js index 6e458c1..cc54016 100644 --- a/src/api/Controllers/ProgramCourseController.js +++ b/src/api/Controllers/ProgramCourseController.js @@ -2,7 +2,7 @@ import ProgramService from "../Services/ProgramCourseServices.js"; import path from "path"; import { fileURLToPath } from "url"; import { DataTypes } from "sequelize"; -import sequelize from "../../../src/config/db.js"; +import sequelize from "../../../src/Config/db.js"; import defineProgramCourse from "../Models/programcourse.js"; const ProgramCourse = defineProgramCourse(sequelize, DataTypes); diff --git a/src/api/Models/index.js b/src/api/Models/index.js index 5907d80..7cca4a5 100644 --- a/src/api/Models/index.js +++ b/src/api/Models/index.js @@ -1,7 +1,7 @@ import fs from 'fs'; import path from 'path'; import Sequelize, { DataTypes } from 'sequelize'; -import configFile from '../../config/config.js'; +import configFile from '../../Config/config.js'; // __dirname replacement for ES modules const __dirname = path.dirname(new URL(import.meta.url).pathname); From ac7a5e4f1455c689aefeed9641f53cf743225a1c Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 10:12:11 +0800 Subject: [PATCH 41/47] refactored all the path that import db.js and config --- src/Config/db.js | 2 +- src/api/Controllers/InstructorController.js | 2 +- src/api/Services/DepartmentServices.js | 2 +- src/api/Services/InstructorServices.js | 2 +- src/api/Services/ProgramCourseServices.js | 2 +- src/api/Services/UserService.js | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Config/db.js b/src/Config/db.js index f52c18a..437f2f6 100644 --- a/src/Config/db.js +++ b/src/Config/db.js @@ -6,7 +6,7 @@ import { Sequelize } from 'sequelize'; // Import the default export (the configuration object) from the config file. // Note: The original CJS referred to './config', but we use the actual file name. -import configModule from '../config/config.js'; +import configModule from '../Config/config.js'; // Destructure the specific 'development' environment configuration. const config = configModule.development; diff --git a/src/api/Controllers/InstructorController.js b/src/api/Controllers/InstructorController.js index acd12c1..4e91a87 100644 --- a/src/api/Controllers/InstructorController.js +++ b/src/api/Controllers/InstructorController.js @@ -2,7 +2,7 @@ import InstructorService from "../Services/InstructorServices.js"; import path from "path"; import { fileURLToPath } from "url"; import { DataTypes } from "sequelize"; -import sequelize from "../../config/db.js"; +import sequelize from "../../Config/db.js"; import UserModelDefiner from "../Models/user.js"; import InstructorModelDefiner from "../Models/instructor.js"; import { verifyWebToken } from "../../Middlewares/authMiddleware.js"; diff --git a/src/api/Services/DepartmentServices.js b/src/api/Services/DepartmentServices.js index 413e2d6..1aa380a 100644 --- a/src/api/Services/DepartmentServices.js +++ b/src/api/Services/DepartmentServices.js @@ -1,7 +1,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import { DataTypes } from 'sequelize'; -import sequelize from '../../../src/config/db.js'; +import sequelize from '../../src/Config/db.js'; import DepartmentModelFactory from '../Models/department.js'; // Required to simulate __dirname in ES modules diff --git a/src/api/Services/InstructorServices.js b/src/api/Services/InstructorServices.js index 989c026..3eff02c 100644 --- a/src/api/Services/InstructorServices.js +++ b/src/api/Services/InstructorServices.js @@ -1,7 +1,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import { DataTypes } from 'sequelize'; -import sequelize from '../../../src/config/db.js'; +import sequelize from '../../Config/db.js'; import InstructorModelFactory from '../Models/instructor.js'; import ProgramCourseModelFactory from '../Models/programcourse.js'; diff --git a/src/api/Services/ProgramCourseServices.js b/src/api/Services/ProgramCourseServices.js index b5f043e..cfe374c 100644 --- a/src/api/Services/ProgramCourseServices.js +++ b/src/api/Services/ProgramCourseServices.js @@ -1,7 +1,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import { DataTypes } from 'sequelize'; -import sequelize from '../../../src/config/db.js'; +import sequelize from '../../src/config/db.js'; import ProgramCourseModelFactory from '../Models/programcourse.js'; // 🔧 Setup __dirname for ES modules diff --git a/src/api/Services/UserService.js b/src/api/Services/UserService.js index 3fb3feb..03e9c60 100644 --- a/src/api/Services/UserService.js +++ b/src/api/Services/UserService.js @@ -3,7 +3,7 @@ import jwt from "jsonwebtoken"; import path from "path"; import { fileURLToPath } from "url"; import { DataTypes } from "sequelize"; -import sequelize from "../../../src/config/db.js"; +import sequelize from "../../src/config/db.js"; import UserModel from "../Models/user.js"; // Resolve __dirname for ES module From 4d29dd06596ff428109af25a0a601d2741e878a0 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 10:24:17 +0800 Subject: [PATCH 42/47] Refactor: all the controllers method export together in the export default not exporting separately --- src/api/Controllers/DepartmentController.js | 47 ++++++++------- src/api/Controllers/InstructorController.js | 21 +++++-- .../Controllers/ProgramCourseController.js | 59 ++++++++++--------- src/api/Controllers/userController.js | 31 ++++++---- 4 files changed, 91 insertions(+), 67 deletions(-) diff --git a/src/api/Controllers/DepartmentController.js b/src/api/Controllers/DepartmentController.js index ca8e2a4..a8bfda6 100644 --- a/src/api/Controllers/DepartmentController.js +++ b/src/api/Controllers/DepartmentController.js @@ -2,7 +2,6 @@ import DepartmentService from '../Services/DepartmentServices.js'; import path from 'path'; import { fileURLToPath } from 'url'; import { DataTypes } from 'sequelize'; -import { UpdateProgram } from './ProgramCourseController.js'; import sequelize from '../../../src/Config/db.js'; import defineDepartmentModel from '../Models/department.js'; @@ -14,44 +13,42 @@ const __dirname = path.dirname(__filename); const DepartmentModel = defineDepartmentModel(sequelize, DataTypes); // ✅ Get All Departments -export const getAllDepartments = async (req, res) => { +const getAllDepartments = async (req, res) => { try { let { top, page, limit } = req.body; - // Convert string query params to numbers top = parseInt(top); page = parseInt(page); limit = parseInt(limit); const queryOptions = { - order: [['createdAt', 'DESC']], // latest first + order: [['createdAt', 'DESC']], }; if (top) { - // Return only top X records queryOptions.limit = top; } else if (page && limit) { + const offset = (page - 1) * limit; queryOptions.limit = limit; - queryOptions.offset = (page - 1) * limit; + queryOptions.offset = offset; } const departments = await DepartmentModel.findAll(queryOptions); res.status(200).json({ status: 'success', - data: departments, // No need for toJson() + data: departments, }); } catch (error) { - res - .status(error.status || 500) - .json({ error: error.message || 'Internal Server Error' }); + res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' }); } }; + // ✅ Get Department by ID -export const getDepartmentByID = async (req, res) => { +const getDepartmentByID = async (req, res) => { try { const { id } = req.params; - const department = await DepartmentService.getDepartmentByID(id); + const department = await DepartmentService.getDepartmentById(id); if (!department) { return res.status(404).json({ message: "Department not found" }); @@ -65,13 +62,13 @@ export const getDepartmentByID = async (req, res) => { }; // ✅ Create Department -export const createDepartment = async (req, res) => { +const createDepartment = async (req, res) => { try { const addDepartment = await DepartmentService.CreateDeparment(req.body); if (addDepartment) { res.status(201).json({ - message: addDeparment.message, - data: addDeparment.data + message: addDepartment.message, + data: addDepartment.department }); } } catch (error) { @@ -80,20 +77,30 @@ export const createDepartment = async (req, res) => { }; // ✅ Update Department -export const updateDepartment = async (req, res) => { +const updateDepartment = async (req, res) => { try { - const update = await DepartmentService.UpdateDepartment(req, res); - res.status(201).json({ message: update.message }); + const update = await DepartmentService.UpdateDepartment(req.body); + res.status(201).json({ message: update }); } catch (error) { res.status(500).json({ message: error.message }); } }; // ✅ Delete Department -export const deleteDepartment = async (req, res) => { +const deleteDepartment = async (req, res) => { try { - await DepartmentService.DeleteDepartment(req, res); + const result = await DepartmentService.DeleteDepartment(req.body.id); + res.status(200).json({ message: result }); } catch (error) { res.status(500).json({ message: error.message }); } }; + +// ✅ Single default export +export default { + getAllDepartments, + getDepartmentByID, + createDepartment, + updateDepartment, + deleteDepartment +}; diff --git a/src/api/Controllers/InstructorController.js b/src/api/Controllers/InstructorController.js index 4e91a87..db3b05a 100644 --- a/src/api/Controllers/InstructorController.js +++ b/src/api/Controllers/InstructorController.js @@ -16,17 +16,17 @@ const User = UserModelDefiner(sequelize, DataTypes); const Instructor = InstructorModelDefiner(sequelize, DataTypes); // ✅ Create Instructor -export const CreateInstructor = async (req, res) => { +const createInstructor = async (req, res) => { try { - const {id, ...data} = req.body + const { id, ...data } = req.body; const findInstructor = await Instructor.findOne({ where: { user_id: id } }); if (findInstructor) { return res.status(400).json({ - message: "This user already has an instructor profile" + message: "This user already has an instructor profile" }); } - // Create Instructor + const instructor = await InstructorService.CreateInstructor(req.body); return res.status(201).json({ @@ -41,8 +41,9 @@ export const CreateInstructor = async (req, res) => { }); } }; + // ✅ Update Instructor -export const UpdateInstructor = async (req, res) => { +const updateInstructor = async (req, res) => { try { const update = await InstructorService.UpdateInstructor(req.params.id, req.body); @@ -57,8 +58,9 @@ export const UpdateInstructor = async (req, res) => { }); } }; + // ✅ Delete Instructor -export const DeleteInstructor = async (req, res) => { +const deleteInstructor = async (req, res) => { try { await InstructorService.DeleteInstructor(req.params.id); @@ -73,3 +75,10 @@ export const DeleteInstructor = async (req, res) => { }); } }; + +// ✅ Single default export +export default { + createInstructor, + updateInstructor, + deleteInstructor +}; diff --git a/src/api/Controllers/ProgramCourseController.js b/src/api/Controllers/ProgramCourseController.js index cc54016..f0fed15 100644 --- a/src/api/Controllers/ProgramCourseController.js +++ b/src/api/Controllers/ProgramCourseController.js @@ -9,13 +9,13 @@ const ProgramCourse = defineProgramCourse(sequelize, DataTypes); const __dirname = path.dirname(fileURLToPath(import.meta.url)); // ✅ Create Program -export const createProgram = async (req, res) => { +const createProgram = async (req, res) => { try { const addProgram = await ProgramService.createProgram(req.body); - if(addProgram){ + if (addProgram) { res.status(201).json({ - message: 'Program Added Successfully', - userId: addProgram.id + message: 'Program Added Successfully', + userId: addProgram.id }); } } catch (error) { @@ -24,11 +24,10 @@ export const createProgram = async (req, res) => { }; // ✅ Get All Programs -export const getAllPrograms = async (req, res) => { +const getAllPrograms = async (req, res) => { try { let { top, page, limit } = req.body; - // Convert string query params to numbers top = parseInt(top); page = parseInt(page); limit = parseInt(limit); @@ -38,11 +37,8 @@ export const getAllPrograms = async (req, res) => { }; if (top) { - // Return only top X records queryOptions.limit = top; - } - else if (page && limit) { - // Apply pagination + } else if (page && limit) { const offset = (page - 1) * limit; queryOptions.limit = limit; queryOptions.offset = offset; @@ -54,7 +50,6 @@ export const getAllPrograms = async (req, res) => { status: "success", data: programs, }); - } catch (error) { res.status(500).json({ status: "error", @@ -64,33 +59,41 @@ export const getAllPrograms = async (req, res) => { }; // ✅ Update Program -export const UpdateProgram = async (req, res) => { +const updateProgram = async (req, res) => { try { const result = await ProgramService.UpdateProgram(req.body); - if (result.success) { - res.status(200).json({ - success: true, - message: result.message || "Operation successful", - data: result.data || null - }); - } else { - res.status(result.statusCode || 400).json({ - success: false, - message: result.message || "Operation failed", - error: result.error || null - }); - } - + if (result.success) { + res.status(200).json({ + success: true, + message: result.message || "Operation successful", + data: result.data || null + }); + } else { + res.status(result.statusCode || 400).json({ + success: false, + message: result.message || "Operation failed", + error: result.error || null + }); + } + } catch (error) { res.status(500).json({ message: "Update failed", error: error.message }); - } + } }; // ✅ Delete Program -export const DeleteProgram = async (id) => { +const deleteProgram = async (id) => { return await ProgramService.DeleteProgram(id); }; + +// ✅ Single default export +export default { + createProgram, + getAllPrograms, + updateProgram, + deleteProgram +}; diff --git a/src/api/Controllers/userController.js b/src/api/Controllers/userController.js index 54774b2..acc7e81 100644 --- a/src/api/Controllers/userController.js +++ b/src/api/Controllers/userController.js @@ -1,13 +1,13 @@ import userService from "../Services/UserService.js"; // ✅ Register User -export const registerUser = async (req, res) => { +const registerUser = async (req, res) => { try { const newUser = await userService.registerUser(req.body); - if(newUser){ + if (newUser) { res.status(201).json({ - message: 'User registered successfully!', - userId: newUser.id + message: 'User registered successfully!', + userId: newUser.id }); } } catch (error) { @@ -16,24 +16,29 @@ export const registerUser = async (req, res) => { }; // ✅ Login User -export const LoginUser = async (req, res) => { +const loginUser = async (req, res) => { try { const isLogin = await userService.LoginUser(req.body); - - if(isLogin) { + if (isLogin) { res.cookie("token", isLogin.token, { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'strict', - maxAge: 60 * 60 * 1000 + maxAge: 60 * 60 * 1000 // 1 hour }); - return res.status(200).json({ - message: 'Login successful', - }); - } - }catch(error){ + return res.status(200).json({ + message: 'Login successful', + }); + } + } catch (error) { res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' }); } }; + +// Export all controller functions as a single default object +export default { + registerUser, + loginUser +}; From b79407f751b525597bb7eefc2036660f01dae58f Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 10:35:43 +0800 Subject: [PATCH 43/47] all the Service methods are exported using default and verified and the all files imported correctly. by standardizing path --- src/api/Services/DepartmentServices.js | 22 +++++++++++------ src/api/Services/InstructorServices.js | 13 +++++++--- src/api/Services/ProgramCourseServices.js | 17 ++++++++----- src/api/Services/StudentService.js | 30 +++++++++++++++++------ src/api/Services/UserService.js | 12 ++++++--- 5 files changed, 68 insertions(+), 26 deletions(-) diff --git a/src/api/Services/DepartmentServices.js b/src/api/Services/DepartmentServices.js index 1aa380a..4f44dcc 100644 --- a/src/api/Services/DepartmentServices.js +++ b/src/api/Services/DepartmentServices.js @@ -1,7 +1,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import { DataTypes } from 'sequelize'; -import sequelize from '../../src/Config/db.js'; +import sequelize from '../../Config/db.js'; import DepartmentModelFactory from '../Models/department.js'; // Required to simulate __dirname in ES modules @@ -12,7 +12,7 @@ const __dirname = path.dirname(__filename); const DepartmentModel = DepartmentModelFactory(sequelize, DataTypes); // 🟩 Get Department By ID -export const getDepartmentById = async (id) => { +const getDepartmentById = async (id) => { if (!id) { throw new Error('Department ID is required'); } @@ -22,7 +22,7 @@ export const getDepartmentById = async (id) => { }; // 🟩 Create Department -export const CreateDeparment = async (departmentData) => { +const createDepartment = async (departmentData) => { try { const department = await DepartmentModel.create(departmentData); @@ -40,7 +40,7 @@ export const CreateDeparment = async (departmentData) => { }; // 🟩 Update Department -export const UpdateDepartment = async (departmentData) => { +const updateDepartment = async (departmentData) => { try { const { department_id, ...updateData } = departmentData; @@ -52,11 +52,11 @@ export const UpdateDepartment = async (departmentData) => { throw new Error('At least one department field must be provided to update'); } - const findExistingDeparment = await DepartmentModel.findOne({ + const findExistingDepartment = await DepartmentModel.findOne({ where: { department_id }, }); - if (!findExistingDeparment) { + if (!findExistingDepartment) { throw new Error('No Department found with the given department ID'); } @@ -75,7 +75,7 @@ export const UpdateDepartment = async (departmentData) => { }; // 🟩 Delete Department -export const DeleteDepartment = async (id) => { +const deleteDepartment = async (id) => { try { if (id === undefined) { throw new Error('ID must be provided'); @@ -94,3 +94,11 @@ export const DeleteDepartment = async (id) => { throw error; } }; + +// ✅ Single default export +export default { + getDepartmentById, + createDepartment, + updateDepartment, + deleteDepartment +}; diff --git a/src/api/Services/InstructorServices.js b/src/api/Services/InstructorServices.js index 3eff02c..2d3387f 100644 --- a/src/api/Services/InstructorServices.js +++ b/src/api/Services/InstructorServices.js @@ -14,7 +14,7 @@ const Instructor = InstructorModelFactory(sequelize, DataTypes); const ProgramCourse = ProgramCourseModelFactory(sequelize, DataTypes); // 🟩 Create Instructor -export const CreateInstructor = async (InstructorData) => { +const createInstructor = async (InstructorData) => { try { const instructor = await Instructor.create(InstructorData); @@ -29,7 +29,7 @@ export const CreateInstructor = async (InstructorData) => { }; // 🟩 Update Instructor -export const UpdateInstructor = async (id, InstructorData) => { +const updateInstructor = async (id, InstructorData) => { try { const instructor = await Instructor.findByPk(id); @@ -56,7 +56,7 @@ export const UpdateInstructor = async (id, InstructorData) => { }; // 🟩 Delete Instructor -export const DeleteInstructor = async (id) => { +const deleteInstructor = async (id) => { try { // Check assigned courses const courseCount = await ProgramCourse.count({ @@ -88,3 +88,10 @@ export const DeleteInstructor = async (id) => { throw error; } }; + +// ✅ Export all functions as a single default object +export default { + createInstructor, + updateInstructor, + deleteInstructor +}; diff --git a/src/api/Services/ProgramCourseServices.js b/src/api/Services/ProgramCourseServices.js index cfe374c..6a9e917 100644 --- a/src/api/Services/ProgramCourseServices.js +++ b/src/api/Services/ProgramCourseServices.js @@ -1,7 +1,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import { DataTypes } from 'sequelize'; -import sequelize from '../../src/config/db.js'; +import sequelize from '../../Config/db.js'; import ProgramCourseModelFactory from '../Models/programcourse.js'; // 🔧 Setup __dirname for ES modules @@ -12,9 +12,8 @@ const __dirname = path.dirname(__filename); const ProgramCourse = ProgramCourseModelFactory(sequelize, DataTypes); // 🟩 Create Program -export const createProgram = async (programData) => { +const createProgram = async (programData) => { try { - // Check if program with same code already exists const existingProgram = await ProgramCourse.findOne({ where: { code: programData.code }, }); @@ -25,7 +24,6 @@ export const createProgram = async (programData) => { throw error; } - // Create new program const newProgram = await ProgramCourse.create({ parent_id: null, type: 'PROGRAM', @@ -46,7 +44,7 @@ export const createProgram = async (programData) => { }; // 🟩 Delete Program -export const DeleteProgram = async (id) => { +const deleteProgram = async (id) => { try { const findProgram = await ProgramCourse.findOne({ where: { id } }); @@ -64,7 +62,7 @@ export const DeleteProgram = async (id) => { }; // 🟩 Update Program -export const UpdateProgram = async (programData) => { +const updateProgram = async (programData) => { try { const { id, ...updateFields } = programData; @@ -85,3 +83,10 @@ export const UpdateProgram = async (programData) => { return { success: false, message: error.message }; } }; + +// ✅ Single default export +export default { + createProgram, + updateProgram, + deleteProgram +}; diff --git a/src/api/Services/StudentService.js b/src/api/Services/StudentService.js index 3423d6c..3de2fd0 100644 --- a/src/api/Services/StudentService.js +++ b/src/api/Services/StudentService.js @@ -2,7 +2,7 @@ import bcrypt from "bcrypt"; import path from "path"; import { fileURLToPath } from "url"; import { DataTypes } from "sequelize"; -import sequelize from "../../../src/Config/db.js"; +import sequelize from "../../Config/db.js"; import StudentModel from "../Models/Student.js"; import User from "../Models/user.js"; @@ -12,7 +12,8 @@ const __dirname = path.dirname(__filename); const Student = StudentModel(sequelize, DataTypes); -export const createStudent = async (studentData) => { +// 🟩 Create Student +const createStudent = async (studentData) => { try { const newStudent = await Student.create(studentData); return newStudent; @@ -21,7 +22,8 @@ export const createStudent = async (studentData) => { } }; -export const getAllStudents = async () => { +// 🟩 Get All Students +const getAllStudents = async () => { try { const students = await Student.findAll(); return students; @@ -30,7 +32,8 @@ export const getAllStudents = async () => { } }; -export const getStudentById = async (id) => { +// 🟩 Get Student By ID +const getStudentById = async (id) => { try { const student = await Student.findByPk(id); if (!student) { @@ -44,7 +47,8 @@ export const getStudentById = async (id) => { } }; -export const updateStudent = async (id, updatedData) => { +// 🟩 Update Student +const updateStudent = async (id, updatedData) => { try { const student = await Student.findByPk(id); if (!student) { @@ -60,7 +64,8 @@ export const updateStudent = async (id, updatedData) => { } }; -export const deleteStudent = async (id) => { +// 🟩 Delete Student +const deleteStudent = async (id) => { try { const deleted = await Student.destroy({ where: { student_id: id } }); if (!deleted) { @@ -74,7 +79,8 @@ export const deleteStudent = async (id) => { } }; -export const changePassword = async (email, password, confirmpass) => { +// 🟩 Change Password +const changePassword = async (email, password, confirmpass) => { try { if (password !== confirmpass) { throw new Error("Passwords do not match"); @@ -96,3 +102,13 @@ export const changePassword = async (email, password, confirmpass) => { throw error; } }; + +// ✅ Single default export +export default { + createStudent, + getAllStudents, + getStudentById, + updateStudent, + deleteStudent, + changePassword +}; diff --git a/src/api/Services/UserService.js b/src/api/Services/UserService.js index 03e9c60..58847a3 100644 --- a/src/api/Services/UserService.js +++ b/src/api/Services/UserService.js @@ -3,7 +3,7 @@ import jwt from "jsonwebtoken"; import path from "path"; import { fileURLToPath } from "url"; import { DataTypes } from "sequelize"; -import sequelize from "../../src/config/db.js"; +import sequelize from "../../Config/db.js"; import UserModel from "../Models/user.js"; // Resolve __dirname for ES module @@ -13,7 +13,7 @@ const __dirname = path.dirname(__filename); const User = UserModel(sequelize, DataTypes); // ✅ Register User -export const registerUser = async (body) => { +const registerUser = async (body) => { try { const { username, email, password } = body; @@ -42,7 +42,7 @@ export const registerUser = async (body) => { }; // ✅ Login User -export const loginUser = async (body) => { +const loginUser = async (body) => { try { const { email, password } = body; @@ -84,3 +84,9 @@ export const loginUser = async (body) => { throw error; } }; + +// ✅ Single default export +export default { + registerUser, + loginUser +}; From 8673ff9d1faad0d5bd04c93a25577eab10ffaf43 Mon Sep 17 00:00:00 2001 From: agaesh Date: Sun, 12 Oct 2025 11:46:55 +0800 Subject: [PATCH 44/47] added department_integration.test.test --- tests/department_integration.test.js | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/department_integration.test.js diff --git a/tests/department_integration.test.js b/tests/department_integration.test.js new file mode 100644 index 0000000..afb462c --- /dev/null +++ b/tests/department_integration.test.js @@ -0,0 +1,63 @@ +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import request from 'supertest'; +import express from 'express'; +import DepartmentRouter from '../Routes/DepartmentRoute.js'; +import sequelize from '../Config/db.js'; // Your Sequelize instance +import DepartmentModelFactory from '../Models/department.js'; +import { DataTypes } from 'sequelize'; + +// Setup test app +const app = express(); +app.use(express.json()); +app.use('/api/department', DepartmentRouter); + +// Initialize Department model for testing +const Department = DepartmentModelFactory(sequelize, DataTypes); + +beforeAll(async () => { + // Sync database (force: true to reset) + await sequelize.sync({ force: true }); +}); + +afterAll(async () => { + // Close DB connection after tests + await sequelize.close(); +}); + +describe('Department Routes Integration Test', () => { + let departmentId; + + it('GET /api/department should return welcome message', async () => { + const res = await request(app).get('/api/department'); + expect(res.status).toBe(200); + expect(res.text).toBe('Welcome To Department API'); + }); + + it('POST /api/department should create a new department', async () => { + const res = await request(app) + .post('/api/department') + .send({ name: 'HR', description: 'Human Resources' }); + + expect(res.status).toBe(201); + expect(res.body).toHaveProperty('id'); + expect(res.body.name).toBe('HR'); + + departmentId = res.body.id; + }); + + it('GET /api/department/listing should return all departments', async () => { + const res = await request(app).get('/api/department/listing'); + expect(res.status).toBe(200); + expect(Array.isArray(res.body)).toBe(true); + expect(res.body.length).toBeGreaterThan(0); + }); + + it('PUT /api/department should update a department', async () => { + const res = await request(app) + .put('/api/department') + .send({ id: departmentId, name: 'HR Updated', description: 'Updated Description' }); + + expect(res.status).toBe(200); + expect(res.body.name).toBe('HR Updated'); + }); +}); From 301421a704d86a45f0cd75098c0c73a91ba544a9 Mon Sep 17 00:00:00 2001 From: agaesh <59405132+agaesh@users.noreply.github.com> Date: Sun, 28 Dec 2025 11:10:40 +0800 Subject: [PATCH 45/47] Refactor: migrate from CommonJS to ES6 module syntax in migration files --- .../Migrations/20250712044830-create-user.js | 3 +- .../20250713051943-create-student.js | 2 +- ...0250713052213-add-userId-fk-to-students.js | 2 +- .../20250713080222-create-department.js | 2 +- .../20250713081004-create-instructor.js | 2 +- .../20250714085001-create-program-course.js | 2 +- ...114202-add-timestamps-to-Program-course.js | 38 ------------------- .../20250716055011-add-ProgramCourse-FK.js | 2 +- ...145527-add-fk-department-to-instructors.js | 2 +- ...0807123944-create-department-instructor.js | 2 +- 10 files changed, 10 insertions(+), 47 deletions(-) delete mode 100644 src/Database/Migrations/20250714114202-add-timestamps-to-Program-course.js diff --git a/src/Database/Migrations/20250712044830-create-user.js b/src/Database/Migrations/20250712044830-create-user.js index bedf5b6..4886cf2 100644 --- a/src/Database/Migrations/20250712044830-create-user.js +++ b/src/Database/Migrations/20250712044830-create-user.js @@ -1,6 +1,6 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.createTable('Users', { id: { @@ -41,6 +41,7 @@ module.exports = { } }); }, + async down(queryInterface, Sequelize) { await queryInterface.dropTable('Users'); } diff --git a/src/Database/Migrations/20250713051943-create-student.js b/src/Database/Migrations/20250713051943-create-student.js index 7140533..01c39b6 100644 --- a/src/Database/Migrations/20250713051943-create-student.js +++ b/src/Database/Migrations/20250713051943-create-student.js @@ -1,6 +1,6 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.createTable('Students', { id: { diff --git a/src/Database/Migrations/20250713052213-add-userId-fk-to-students.js b/src/Database/Migrations/20250713052213-add-userId-fk-to-students.js index a41105d..ec238af 100644 --- a/src/Database/Migrations/20250713052213-add-userId-fk-to-students.js +++ b/src/Database/Migrations/20250713052213-add-userId-fk-to-students.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up (queryInterface, Sequelize) { await queryInterface.addConstraint('students', { diff --git a/src/Database/Migrations/20250713080222-create-department.js b/src/Database/Migrations/20250713080222-create-department.js index 7d67cdd..7d44519 100644 --- a/src/Database/Migrations/20250713080222-create-department.js +++ b/src/Database/Migrations/20250713080222-create-department.js @@ -1,6 +1,6 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.createTable('Departments', { department_id: { diff --git a/src/Database/Migrations/20250713081004-create-instructor.js b/src/Database/Migrations/20250713081004-create-instructor.js index b236432..0a2642e 100644 --- a/src/Database/Migrations/20250713081004-create-instructor.js +++ b/src/Database/Migrations/20250713081004-create-instructor.js @@ -1,6 +1,6 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.createTable('Instructors', { id: { diff --git a/src/Database/Migrations/20250714085001-create-program-course.js b/src/Database/Migrations/20250714085001-create-program-course.js index 33cf042..d305d41 100644 --- a/src/Database/Migrations/20250714085001-create-program-course.js +++ b/src/Database/Migrations/20250714085001-create-program-course.js @@ -1,6 +1,6 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.createTable('ProgramCourses', { id: { diff --git a/src/Database/Migrations/20250714114202-add-timestamps-to-Program-course.js b/src/Database/Migrations/20250714114202-add-timestamps-to-Program-course.js deleted file mode 100644 index 53e90de..0000000 --- a/src/Database/Migrations/20250714114202-add-timestamps-to-Program-course.js +++ /dev/null @@ -1,38 +0,0 @@ -'use strict'; - -/** @type {import('sequelize-cli').Migration} */ -module.exports = { - async up (queryInterface, Sequelize) { - /** - * Add altering commands here. - * - * Example: - * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); - */ - - await queryInterface.addColumn('ProgramCourses', 'createdAt', { - allowNull: false, - type: Sequelize.DATE, - defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') - }); - - // Add updatedAt - await queryInterface.addColumn('ProgramCourses', 'updatedAt', { - allowNull: false, - type: Sequelize.DATE, - defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') - }); - - }, - - async down (queryInterface, Sequelize) { - /** - * Add reverting commands here. - * - * Example: - * await queryInterface.dropTable('users'); - */ - await queryInterface.removeColumn('ProgramCourses','createdAt'); - await queryInterface.removeColumn("ProgramCourses",'updateAt') - } -}; diff --git a/src/Database/Migrations/20250716055011-add-ProgramCourse-FK.js b/src/Database/Migrations/20250716055011-add-ProgramCourse-FK.js index 3fb6832..e1e3d12 100644 --- a/src/Database/Migrations/20250716055011-add-ProgramCourse-FK.js +++ b/src/Database/Migrations/20250716055011-add-ProgramCourse-FK.js @@ -2,7 +2,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.addConstraint('programcourses', { fields: ['parent_id'], diff --git a/src/Database/Migrations/20250805145527-add-fk-department-to-instructors.js b/src/Database/Migrations/20250805145527-add-fk-department-to-instructors.js index 77c30fe..af91530 100644 --- a/src/Database/Migrations/20250805145527-add-fk-department-to-instructors.js +++ b/src/Database/Migrations/20250805145527-add-fk-department-to-instructors.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up (queryInterface, Sequelize) { /** * Add altering commands here. diff --git a/src/Database/Migrations/20250807123944-create-department-instructor.js b/src/Database/Migrations/20250807123944-create-department-instructor.js index ae1b86b..e99c766 100644 --- a/src/Database/Migrations/20250807123944-create-department-instructor.js +++ b/src/Database/Migrations/20250807123944-create-department-instructor.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up (queryInterface, Sequelize) { await queryInterface.createTable('DepartmentInstructors', { id: { From 151017514aae9dc80e0345d4ab4109a9b4e5cac8 Mon Sep 17 00:00:00 2001 From: agaesh <59405132+agaesh@users.noreply.github.com> Date: Sun, 28 Dec 2025 11:41:54 +0800 Subject: [PATCH 46/47] Refactor: migrate migration and seeder files from CommonJS to ES6 module syntax --- .../20250806132734-add-fk-department-head.js | 4 +-- ...6-remove-department-id-from-instructors.js | 33 ------------------- ...09014716-add-fk-ProgramCourse-Deparment.js | 2 +- ...0250831140916-add-Instructor-TimeStamps.js | 2 +- .../Migrations/20250909013953-Semesters.js | 4 +-- .../20250805140906-demo-departments.js | 5 ++- .../20250806125124-demo-instructors.js | 2 +- ...50807130913-demo-department-instructors.js | 4 +-- .../20250809012319-demo-ProgramCourse.js | 4 +-- 9 files changed, 13 insertions(+), 47 deletions(-) delete mode 100644 src/Database/Migrations/20250807134826-remove-department-id-from-instructors.js diff --git a/src/Database/Migrations/20250806132734-add-fk-department-head.js b/src/Database/Migrations/20250806132734-add-fk-department-head.js index 9a55bb0..25232a2 100644 --- a/src/Database/Migrations/20250806132734-add-fk-department-head.js +++ b/src/Database/Migrations/20250806132734-add-fk-department-head.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up (queryInterface, Sequelize) { /** * Add altering commands here. @@ -29,6 +29,6 @@ module.exports = { * Example: * await queryInterface.dropTable('users'); */ - await queryInterface.removeConstraint('Instructors', 'fk_head_department_id'); + await queryInterface.removeConstraint('Departments', 'fk_head_department_id'); } }; diff --git a/src/Database/Migrations/20250807134826-remove-department-id-from-instructors.js b/src/Database/Migrations/20250807134826-remove-department-id-from-instructors.js deleted file mode 100644 index 96bd35a..0000000 --- a/src/Database/Migrations/20250807134826-remove-department-id-from-instructors.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -/** @type {import('sequelize-cli').Migration} */ -module.exports = { - async up (queryInterface, Sequelize) { - /** - * Add altering commands here. - * - * Example: - * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); - */ - - 'use strict'; - -module.exports = { - async up(queryInterface, Sequelize) { - await queryInterface.removeColumn('instructors', 'department_id'); - }, - - async down(queryInterface, Sequelize) { - await queryInterface.addColumn('instructors', 'department_id', { - type: Sequelize.INTEGER, - references: { - model: 'departments', - key: 'id' - }, - onUpdate: 'CASCADE', - onDelete: 'SET NULL' - }); - } -} -} -} diff --git a/src/Database/Migrations/20250809014716-add-fk-ProgramCourse-Deparment.js b/src/Database/Migrations/20250809014716-add-fk-ProgramCourse-Deparment.js index 7ebbb51..f6c81ea 100644 --- a/src/Database/Migrations/20250809014716-add-fk-ProgramCourse-Deparment.js +++ b/src/Database/Migrations/20250809014716-add-fk-ProgramCourse-Deparment.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.addConstraint('ProgramCourses', { fields: ['department_id'], diff --git a/src/Database/Migrations/20250831140916-add-Instructor-TimeStamps.js b/src/Database/Migrations/20250831140916-add-Instructor-TimeStamps.js index 1758547..8e11ac5 100644 --- a/src/Database/Migrations/20250831140916-add-Instructor-TimeStamps.js +++ b/src/Database/Migrations/20250831140916-add-Instructor-TimeStamps.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up (queryInterface, Sequelize) { /** * Add altering commands here. diff --git a/src/Database/Migrations/20250909013953-Semesters.js b/src/Database/Migrations/20250909013953-Semesters.js index bc433d3..3ed4e3e 100644 --- a/src/Database/Migrations/20250909013953-Semesters.js +++ b/src/Database/Migrations/20250909013953-Semesters.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up (queryInterface, Sequelize) { /** * Add altering commands here. @@ -77,7 +77,7 @@ module.exports = { * await queryInterface.dropTable('users'); */ - await queryInterface.removeConstraint('Semester', 'uq_program_semester'); + // await queryInterface.removeConstraint('Semester', 'uq_program_semester'); await queryInterface.dropTable('Semester') } }; diff --git a/src/Database/seeders/20250805140906-demo-departments.js b/src/Database/seeders/20250805140906-demo-departments.js index c8b13bd..2b227d2 100644 --- a/src/Database/seeders/20250805140906-demo-departments.js +++ b/src/Database/seeders/20250805140906-demo-departments.js @@ -1,7 +1,6 @@ 'use strict'; -const department = require("../../api/Models/department"); - -module.exports = { +/** @type {import('sequelize-cli').Migration} */ +export default{ async up(queryInterface, Sequelize) { await queryInterface.bulkInsert('Departments', [ { diff --git a/src/Database/seeders/20250806125124-demo-instructors.js b/src/Database/seeders/20250806125124-demo-instructors.js index f853612..84b077b 100644 --- a/src/Database/seeders/20250806125124-demo-instructors.js +++ b/src/Database/seeders/20250806125124-demo-instructors.js @@ -1,7 +1,7 @@ 'use strict'; /** @type {import('sequelize-cli').Migration} */ -module.exports = { +export default{ async up(queryInterface, Sequelize) { await queryInterface.bulkInsert('Instructors', [ { diff --git a/src/Database/seeders/20250807130913-demo-department-instructors.js b/src/Database/seeders/20250807130913-demo-department-instructors.js index 0aad6ed..c7d135e 100644 --- a/src/Database/seeders/20250807130913-demo-department-instructors.js +++ b/src/Database/seeders/20250807130913-demo-department-instructors.js @@ -1,6 +1,6 @@ 'use strict'; - -module.exports = { +/** @type {import('sequelize-cli').Migration} */ +export default { async up(queryInterface, Sequelize) { // Fetch all instructors const [instructors] = await queryInterface.sequelize.query( diff --git a/src/Database/seeders/20250809012319-demo-ProgramCourse.js b/src/Database/seeders/20250809012319-demo-ProgramCourse.js index 0749be6..3ff4436 100644 --- a/src/Database/seeders/20250809012319-demo-ProgramCourse.js +++ b/src/Database/seeders/20250809012319-demo-ProgramCourse.js @@ -1,6 +1,6 @@ 'use strict'; - -module.exports = { +/** @type {import('sequelize-cli').Migration} */ +export default{ async up(queryInterface, Sequelize) { // 1. Find the Computer Science department const [csDept] = await queryInterface.sequelize.query( From 4272308c4178ce0f5536975fcdb525d843a8f4a5 Mon Sep 17 00:00:00 2001 From: agaesh <59405132+agaesh@users.noreply.github.com> Date: Sun, 28 Dec 2025 11:42:31 +0800 Subject: [PATCH 47/47] Fix: enable timestamps for Department model --- src/api/Models/department.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/Models/department.js b/src/api/Models/department.js index 94be5e1..09e5bbd 100644 --- a/src/api/Models/department.js +++ b/src/api/Models/department.js @@ -48,7 +48,7 @@ export default (sequelize) => { sequelize, modelName: 'Department', tableName: 'Departments', - timestamps: false, + timestamps: true, } );