const db = require('../models'); const FileDBApi = require('./file'); const crypto = require('crypto'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; module.exports = class Student_coursesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const student_courses = await db.student_courses.create( { id: data.id || undefined, custom_title: data.custom_title || null , custom_code: data.custom_code || null , semester: data.semester || null , difficulty: data.difficulty || null , target_grade: data.target_grade || null , credit_hours: data.credit_hours || null , instructor_name: data.instructor_name || null , meeting_location: data.meeting_location || null , meeting_pattern: data.meeting_pattern || null , start_on: data.start_on || null , end_on: data.end_on || null , is_archived: data.is_archived || false , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await student_courses.setStudent( data.student || null, { transaction, }); await student_courses.setCatalog_course( data.catalog_course || null, { transaction, }); return student_courses; } static async bulkImport(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; // Prepare data - wrapping individual data transformations in a map() method const student_coursesData = data.map((item, index) => ({ id: item.id || undefined, custom_title: item.custom_title || null , custom_code: item.custom_code || null , semester: item.semester || null , difficulty: item.difficulty || null , target_grade: item.target_grade || null , credit_hours: item.credit_hours || null , instructor_name: item.instructor_name || null , meeting_location: item.meeting_location || null , meeting_pattern: item.meeting_pattern || null , start_on: item.start_on || null , end_on: item.end_on || null , is_archived: item.is_archived || false , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const student_courses = await db.student_courses.bulkCreate(student_coursesData, { transaction }); // For each item created, replace relation files return student_courses; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const student_courses = await db.student_courses.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.custom_title !== undefined) updatePayload.custom_title = data.custom_title; if (data.custom_code !== undefined) updatePayload.custom_code = data.custom_code; if (data.semester !== undefined) updatePayload.semester = data.semester; if (data.difficulty !== undefined) updatePayload.difficulty = data.difficulty; if (data.target_grade !== undefined) updatePayload.target_grade = data.target_grade; if (data.credit_hours !== undefined) updatePayload.credit_hours = data.credit_hours; if (data.instructor_name !== undefined) updatePayload.instructor_name = data.instructor_name; if (data.meeting_location !== undefined) updatePayload.meeting_location = data.meeting_location; if (data.meeting_pattern !== undefined) updatePayload.meeting_pattern = data.meeting_pattern; if (data.start_on !== undefined) updatePayload.start_on = data.start_on; if (data.end_on !== undefined) updatePayload.end_on = data.end_on; if (data.is_archived !== undefined) updatePayload.is_archived = data.is_archived; updatePayload.updatedById = currentUser.id; await student_courses.update(updatePayload, {transaction}); if (data.student !== undefined) { await student_courses.setStudent( data.student, { transaction } ); } if (data.catalog_course !== undefined) { await student_courses.setCatalog_course( data.catalog_course, { transaction } ); } return student_courses; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const student_courses = await db.student_courses.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of student_courses) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of student_courses) { await record.destroy({transaction}); } }); return student_courses; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const student_courses = await db.student_courses.findByPk(id, options); await student_courses.update({ deletedBy: currentUser.id }, { transaction, }); await student_courses.destroy({ transaction }); return student_courses; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const student_courses = await db.student_courses.findOne( { where }, { transaction }, ); if (!student_courses) { return student_courses; } const output = student_courses.get({plain: true}); output.modules_student_course = await student_courses.getModules_student_course({ transaction }); output.tasks_student_course = await student_courses.getTasks_student_course({ transaction }); output.grade_items_student_course = await student_courses.getGrade_items_student_course({ transaction }); output.study_sessions_student_course = await student_courses.getStudy_sessions_student_course({ transaction }); output.chat_threads_student_course = await student_courses.getChat_threads_student_course({ transaction }); output.student = await student_courses.getStudent({ transaction }); output.catalog_course = await student_courses.getCatalog_course({ transaction }); return output; } static async findAll( filter, options ) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.users, as: 'student', where: filter.student ? { [Op.or]: [ { id: { [Op.in]: filter.student.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.student.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.course_catalogs, as: 'catalog_course', where: filter.catalog_course ? { [Op.or]: [ { id: { [Op.in]: filter.catalog_course.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.catalog_course.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.custom_title) { where = { ...where, [Op.and]: Utils.ilike( 'student_courses', 'custom_title', filter.custom_title, ), }; } if (filter.custom_code) { where = { ...where, [Op.and]: Utils.ilike( 'student_courses', 'custom_code', filter.custom_code, ), }; } if (filter.semester) { where = { ...where, [Op.and]: Utils.ilike( 'student_courses', 'semester', filter.semester, ), }; } if (filter.instructor_name) { where = { ...where, [Op.and]: Utils.ilike( 'student_courses', 'instructor_name', filter.instructor_name, ), }; } if (filter.meeting_location) { where = { ...where, [Op.and]: Utils.ilike( 'student_courses', 'meeting_location', filter.meeting_location, ), }; } if (filter.meeting_pattern) { where = { ...where, [Op.and]: Utils.ilike( 'student_courses', 'meeting_pattern', filter.meeting_pattern, ), }; } if (filter.target_gradeRange) { const [start, end] = filter.target_gradeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, target_grade: { ...where.target_grade, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, target_grade: { ...where.target_grade, [Op.lte]: end, }, }; } } if (filter.credit_hoursRange) { const [start, end] = filter.credit_hoursRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, credit_hours: { ...where.credit_hours, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, credit_hours: { ...where.credit_hours, [Op.lte]: end, }, }; } } if (filter.start_onRange) { const [start, end] = filter.start_onRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, start_on: { ...where.start_on, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, start_on: { ...where.start_on, [Op.lte]: end, }, }; } } if (filter.end_onRange) { const [start, end] = filter.end_onRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, end_on: { ...where.end_on, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, end_on: { ...where.end_on, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.difficulty) { where = { ...where, difficulty: filter.difficulty, }; } if (filter.is_archived) { where = { ...where, is_archived: filter.is_archived, }; } if (filter.createdAtRange) { const [start, end] = filter.createdAtRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, ['createdAt']: { ...where.createdAt, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, ['createdAt']: { ...where.createdAt, [Op.lte]: end, }, }; } } } const queryOptions = { where, include, distinct: true, order: filter.field && filter.sort ? [[filter.field, filter.sort]] : [['createdAt', 'desc']], transaction: options?.transaction, logging: console.log }; if (!options?.countOnly) { queryOptions.limit = limit ? Number(limit) : undefined; queryOptions.offset = offset ? Number(offset) : undefined; } try { const { rows, count } = await db.student_courses.findAndCountAll(queryOptions); return { rows: options?.countOnly ? [] : rows, count: count }; } catch (error) { console.error('Error executing query:', error); throw error; } } static async findAllAutocomplete(query, limit, offset, ) { let where = {}; if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike( 'student_courses', 'custom_title', query, ), ], }; } const records = await db.student_courses.findAll({ attributes: [ 'id', 'custom_title' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['custom_title', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.custom_title, })); } };