39361-vm/backend/src/db/api/grading_rules.js
2026-03-28 17:51:30 +00:00

545 lines
13 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 Grading_rulesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const grading_rules = await db.grading_rules.create(
{
id: data.id || undefined,
rule_name: data.rule_name
||
null
,
applies_to_type: data.applies_to_type
||
null
,
rule_description: data.rule_description
||
null
,
case_sensitive: data.case_sensitive
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await grading_rules.setSubject( data.subject || null, {
transaction,
});
await grading_rules.setTeacher( data.teacher || null, {
transaction,
});
await grading_rules.setSchools( data.schools || null, {
transaction,
});
return grading_rules;
}
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 grading_rulesData = data.map((item, index) => ({
id: item.id || undefined,
rule_name: item.rule_name
||
null
,
applies_to_type: item.applies_to_type
||
null
,
rule_description: item.rule_description
||
null
,
case_sensitive: item.case_sensitive
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const grading_rules = await db.grading_rules.bulkCreate(grading_rulesData, { transaction });
// For each item created, replace relation files
return grading_rules;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const globalAccess = currentUser.app_role?.globalAccess;
const grading_rules = await db.grading_rules.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.rule_name !== undefined) updatePayload.rule_name = data.rule_name;
if (data.applies_to_type !== undefined) updatePayload.applies_to_type = data.applies_to_type;
if (data.rule_description !== undefined) updatePayload.rule_description = data.rule_description;
if (data.case_sensitive !== undefined) updatePayload.case_sensitive = data.case_sensitive;
updatePayload.updatedById = currentUser.id;
await grading_rules.update(updatePayload, {transaction});
if (data.subject !== undefined) {
await grading_rules.setSubject(
data.subject,
{ transaction }
);
}
if (data.teacher !== undefined) {
await grading_rules.setTeacher(
data.teacher,
{ transaction }
);
}
if (data.schools !== undefined) {
await grading_rules.setSchools(
data.schools,
{ transaction }
);
}
return grading_rules;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const grading_rules = await db.grading_rules.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of grading_rules) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of grading_rules) {
await record.destroy({transaction});
}
});
return grading_rules;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const grading_rules = await db.grading_rules.findByPk(id, options);
await grading_rules.update({
deletedBy: currentUser.id
}, {
transaction,
});
await grading_rules.destroy({
transaction
});
return grading_rules;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const grading_rules = await db.grading_rules.findOne(
{ where },
{ transaction },
);
if (!grading_rules) {
return grading_rules;
}
const output = grading_rules.get({plain: true});
output.grading_keywords_grading_rule = await grading_rules.getGrading_keywords_grading_rule({
transaction
});
output.subject = await grading_rules.getSubject({
transaction
});
output.teacher = await grading_rules.getTeacher({
transaction
});
output.schools = await grading_rules.getSchools({
transaction
});
return output;
}
static async findAll(
filter,
globalAccess, options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
const user = (options && options.currentUser) || null;
const userSchools = (user && user.schools?.id) || null;
if (userSchools) {
if (options?.currentUser?.schoolsId) {
where.schoolsId = options.currentUser.schoolsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.subjects,
as: 'subject',
where: filter.subject ? {
[Op.or]: [
{ id: { [Op.in]: filter.subject.split('|').map(term => Utils.uuid(term)) } },
{
subject_name: {
[Op.or]: filter.subject.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'teacher',
where: filter.teacher ? {
[Op.or]: [
{ id: { [Op.in]: filter.teacher.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.teacher.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.schools,
as: 'schools',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.rule_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'grading_rules',
'rule_name',
filter.rule_name,
),
};
}
if (filter.rule_description) {
where = {
...where,
[Op.and]: Utils.ilike(
'grading_rules',
'rule_description',
filter.rule_description,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.applies_to_type) {
where = {
...where,
applies_to_type: filter.applies_to_type,
};
}
if (filter.case_sensitive) {
where = {
...where,
case_sensitive: filter.case_sensitive,
};
}
if (filter.schools) {
const listItems = filter.schools.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
schoolsId: {[Op.or]: listItems}
};
}
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,
},
};
}
}
}
if (globalAccess) {
delete where.schoolsId;
}
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.grading_rules.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, globalAccess, organizationId,) {
let where = {};
if (!globalAccess && organizationId) {
where.organizationId = organizationId;
}
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'grading_rules',
'rule_name',
query,
),
],
};
}
const records = await db.grading_rules.findAll({
attributes: [ 'id', 'rule_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['rule_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.rule_name,
}));
}
};