39128-vm/backend/src/db/api/lesson_quizzes.js
2026-03-11 13:30:27 +00:00

540 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 Lesson_quizzesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lesson_quizzes = await db.lesson_quizzes.create(
{
id: data.id || undefined,
title: data.title
||
null
,
instructions: data.instructions
||
null
,
status: data.status
||
null
,
passing_score: data.passing_score
||
null
,
time_limit_minutes: data.time_limit_minutes
||
null
,
max_attempts: data.max_attempts
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await lesson_quizzes.setLesson( data.lesson || null, {
transaction,
});
return lesson_quizzes;
}
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 lesson_quizzesData = data.map((item, index) => ({
id: item.id || undefined,
title: item.title
||
null
,
instructions: item.instructions
||
null
,
status: item.status
||
null
,
passing_score: item.passing_score
||
null
,
time_limit_minutes: item.time_limit_minutes
||
null
,
max_attempts: item.max_attempts
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const lesson_quizzes = await db.lesson_quizzes.bulkCreate(lesson_quizzesData, { transaction });
// For each item created, replace relation files
return lesson_quizzes;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const lesson_quizzes = await db.lesson_quizzes.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.instructions !== undefined) updatePayload.instructions = data.instructions;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.passing_score !== undefined) updatePayload.passing_score = data.passing_score;
if (data.time_limit_minutes !== undefined) updatePayload.time_limit_minutes = data.time_limit_minutes;
if (data.max_attempts !== undefined) updatePayload.max_attempts = data.max_attempts;
updatePayload.updatedById = currentUser.id;
await lesson_quizzes.update(updatePayload, {transaction});
if (data.lesson !== undefined) {
await lesson_quizzes.setLesson(
data.lesson,
{ transaction }
);
}
return lesson_quizzes;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lesson_quizzes = await db.lesson_quizzes.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of lesson_quizzes) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of lesson_quizzes) {
await record.destroy({transaction});
}
});
return lesson_quizzes;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const lesson_quizzes = await db.lesson_quizzes.findByPk(id, options);
await lesson_quizzes.update({
deletedBy: currentUser.id
}, {
transaction,
});
await lesson_quizzes.destroy({
transaction
});
return lesson_quizzes;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const lesson_quizzes = await db.lesson_quizzes.findOne(
{ where },
{ transaction },
);
if (!lesson_quizzes) {
return lesson_quizzes;
}
const output = lesson_quizzes.get({plain: true});
output.quiz_questions_quiz = await lesson_quizzes.getQuiz_questions_quiz({
transaction
});
output.quiz_attempts_quiz = await lesson_quizzes.getQuiz_attempts_quiz({
transaction
});
output.lesson = await lesson_quizzes.getLesson({
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.lessons,
as: 'lesson',
where: filter.lesson ? {
[Op.or]: [
{ id: { [Op.in]: filter.lesson.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.lesson.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'lesson_quizzes',
'title',
filter.title,
),
};
}
if (filter.instructions) {
where = {
...where,
[Op.and]: Utils.ilike(
'lesson_quizzes',
'instructions',
filter.instructions,
),
};
}
if (filter.passing_scoreRange) {
const [start, end] = filter.passing_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
passing_score: {
...where.passing_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
passing_score: {
...where.passing_score,
[Op.lte]: end,
},
};
}
}
if (filter.time_limit_minutesRange) {
const [start, end] = filter.time_limit_minutesRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
time_limit_minutes: {
...where.time_limit_minutes,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
time_limit_minutes: {
...where.time_limit_minutes,
[Op.lte]: end,
},
};
}
}
if (filter.max_attemptsRange) {
const [start, end] = filter.max_attemptsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
max_attempts: {
...where.max_attempts,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
max_attempts: {
...where.max_attempts,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.lesson_quizzes.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(
'lesson_quizzes',
'title',
query,
),
],
};
}
const records = await db.lesson_quizzes.findAll({
attributes: [ 'id', 'title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title,
}));
}
};