504 lines
12 KiB
JavaScript
504 lines
12 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 Quiz_answersDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quiz_answers = await db.quiz_answers.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
number_value: data.number_value
|
|
||
|
|
null
|
|
,
|
|
|
|
text_value: data.text_value
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await quiz_answers.setQuiz_attempt( data.quiz_attempt || null, {
|
|
transaction,
|
|
});
|
|
|
|
await quiz_answers.setQuiz_question( data.quiz_question || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await quiz_answers.setSelected_choices(data.selected_choices || [], {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
return quiz_answers;
|
|
}
|
|
|
|
|
|
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 quiz_answersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
number_value: item.number_value
|
|
||
|
|
null
|
|
,
|
|
|
|
text_value: item.text_value
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const quiz_answers = await db.quiz_answers.bulkCreate(quiz_answersData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return quiz_answers;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const quiz_answers = await db.quiz_answers.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.number_value !== undefined) updatePayload.number_value = data.number_value;
|
|
|
|
|
|
if (data.text_value !== undefined) updatePayload.text_value = data.text_value;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await quiz_answers.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.quiz_attempt !== undefined) {
|
|
await quiz_answers.setQuiz_attempt(
|
|
|
|
data.quiz_attempt,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.quiz_question !== undefined) {
|
|
await quiz_answers.setQuiz_question(
|
|
|
|
data.quiz_question,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.selected_choices !== undefined) {
|
|
await quiz_answers.setSelected_choices(data.selected_choices, { transaction });
|
|
}
|
|
|
|
|
|
|
|
|
|
return quiz_answers;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quiz_answers = await db.quiz_answers.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of quiz_answers) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of quiz_answers) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return quiz_answers;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quiz_answers = await db.quiz_answers.findByPk(id, options);
|
|
|
|
await quiz_answers.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await quiz_answers.destroy({
|
|
transaction
|
|
});
|
|
|
|
return quiz_answers;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quiz_answers = await db.quiz_answers.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!quiz_answers) {
|
|
return quiz_answers;
|
|
}
|
|
|
|
const output = quiz_answers.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.quiz_attempt = await quiz_answers.getQuiz_attempt({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.quiz_question = await quiz_answers.getQuiz_question({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.selected_choices = await quiz_answers.getSelected_choices({
|
|
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.quiz_attempts,
|
|
as: 'quiz_attempt',
|
|
|
|
where: filter.quiz_attempt ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.quiz_attempt.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
overall_score: {
|
|
[Op.or]: filter.quiz_attempt.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.quiz_questions,
|
|
as: 'quiz_question',
|
|
|
|
where: filter.quiz_question ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.quiz_question.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
prompt: {
|
|
[Op.or]: filter.quiz_question.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
{
|
|
model: db.question_choices,
|
|
as: 'selected_choices',
|
|
required: false,
|
|
},
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.text_value) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'quiz_answers',
|
|
'text_value',
|
|
filter.text_value,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.number_valueRange) {
|
|
const [start, end] = filter.number_valueRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
number_value: {
|
|
...where.number_value,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
number_value: {
|
|
...where.number_value,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.selected_choices) {
|
|
const searchTerms = filter.selected_choices.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.question_choices,
|
|
as: 'selected_choices_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
label: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
|
|
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.quiz_answers.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(
|
|
'quiz_answers',
|
|
'text_value',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.quiz_answers.findAll({
|
|
attributes: [ 'id', 'text_value' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['text_value', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.text_value,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|