38995-vm/backend/src/db/api/subjects.js
2026-03-05 08:03:30 +00:00

557 lines
14 KiB
JavaScript

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 SubjectsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const subjects = await db.subjects.create(
{
id: data.id || undefined,
subject_code: data.subject_code
||
null
,
subject_title: data.subject_title
||
null
,
units: data.units
||
null
,
hours_per_week: data.hours_per_week
||
null
,
subject_category: data.subject_category
||
null
,
room_requirement: data.room_requirement
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await subjects.setDepartment( data.department || null, {
transaction,
});
return subjects;
}
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 subjectsData = data.map((item, index) => ({
id: item.id || undefined,
subject_code: item.subject_code
||
null
,
subject_title: item.subject_title
||
null
,
units: item.units
||
null
,
hours_per_week: item.hours_per_week
||
null
,
subject_category: item.subject_category
||
null
,
room_requirement: item.room_requirement
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const subjects = await db.subjects.bulkCreate(subjectsData, { transaction });
// For each item created, replace relation files
return subjects;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const subjects = await db.subjects.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.subject_code !== undefined) updatePayload.subject_code = data.subject_code;
if (data.subject_title !== undefined) updatePayload.subject_title = data.subject_title;
if (data.units !== undefined) updatePayload.units = data.units;
if (data.hours_per_week !== undefined) updatePayload.hours_per_week = data.hours_per_week;
if (data.subject_category !== undefined) updatePayload.subject_category = data.subject_category;
if (data.room_requirement !== undefined) updatePayload.room_requirement = data.room_requirement;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await subjects.update(updatePayload, {transaction});
if (data.department !== undefined) {
await subjects.setDepartment(
data.department,
{ transaction }
);
}
return subjects;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const subjects = await db.subjects.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of subjects) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of subjects) {
await record.destroy({transaction});
}
});
return subjects;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const subjects = await db.subjects.findByPk(id, options);
await subjects.update({
deletedBy: currentUser.id
}, {
transaction,
});
await subjects.destroy({
transaction
});
return subjects;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const subjects = await db.subjects.findOne(
{ where },
{ transaction },
);
if (!subjects) {
return subjects;
}
const output = subjects.get({plain: true});
output.professor_subject_capabilities_subject = await subjects.getProfessor_subject_capabilities_subject({
transaction
});
output.professor_preferences_subject = await subjects.getProfessor_preferences_subject({
transaction
});
output.class_schedules_subject = await subjects.getClass_schedules_subject({
transaction
});
output.professor_teaching_histories_subject = await subjects.getProfessor_teaching_histories_subject({
transaction
});
output.department = await subjects.getDepartment({
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.departments,
as: 'department',
where: filter.department ? {
[Op.or]: [
{ id: { [Op.in]: filter.department.split('|').map(term => Utils.uuid(term)) } },
{
department_name: {
[Op.or]: filter.department.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.subject_code) {
where = {
...where,
[Op.and]: Utils.ilike(
'subjects',
'subject_code',
filter.subject_code,
),
};
}
if (filter.subject_title) {
where = {
...where,
[Op.and]: Utils.ilike(
'subjects',
'subject_title',
filter.subject_title,
),
};
}
if (filter.unitsRange) {
const [start, end] = filter.unitsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
units: {
...where.units,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
units: {
...where.units,
[Op.lte]: end,
},
};
}
}
if (filter.hours_per_weekRange) {
const [start, end] = filter.hours_per_weekRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
hours_per_week: {
...where.hours_per_week,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
hours_per_week: {
...where.hours_per_week,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.subject_category) {
where = {
...where,
subject_category: filter.subject_category,
};
}
if (filter.room_requirement) {
where = {
...where,
room_requirement: filter.room_requirement,
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
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.subjects.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(
'subjects',
'subject_title',
query,
),
],
};
}
const records = await db.subjects.findAll({
attributes: [ 'id', 'subject_title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['subject_title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.subject_title,
}));
}
};