597 lines
15 KiB
JavaScript
597 lines
15 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 Exercise_submissionsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercise_submissions = await db.exercise_submissions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
answer_text: data.answer_text
|
|
||
|
|
null
|
|
,
|
|
|
|
score: data.score
|
|
||
|
|
null
|
|
,
|
|
|
|
is_correct: data.is_correct
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
submitted_at: data.submitted_at
|
|
||
|
|
null
|
|
,
|
|
|
|
graded_at: data.graded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await exercise_submissions.setExercise( data.exercise || null, {
|
|
transaction,
|
|
});
|
|
|
|
await exercise_submissions.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercise_submissions.getTableName(),
|
|
belongsToColumn: 'answer_files',
|
|
belongsToId: exercise_submissions.id,
|
|
},
|
|
data.answer_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return exercise_submissions;
|
|
}
|
|
|
|
|
|
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 exercise_submissionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
answer_text: item.answer_text
|
|
||
|
|
null
|
|
,
|
|
|
|
score: item.score
|
|
||
|
|
null
|
|
,
|
|
|
|
is_correct: item.is_correct
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
submitted_at: item.submitted_at
|
|
||
|
|
null
|
|
,
|
|
|
|
graded_at: item.graded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const exercise_submissions = await db.exercise_submissions.bulkCreate(exercise_submissionsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < exercise_submissions.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercise_submissions.getTableName(),
|
|
belongsToColumn: 'answer_files',
|
|
belongsToId: exercise_submissions[i].id,
|
|
},
|
|
data[i].answer_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return exercise_submissions;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const exercise_submissions = await db.exercise_submissions.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.answer_text !== undefined) updatePayload.answer_text = data.answer_text;
|
|
|
|
|
|
if (data.score !== undefined) updatePayload.score = data.score;
|
|
|
|
|
|
if (data.is_correct !== undefined) updatePayload.is_correct = data.is_correct;
|
|
|
|
|
|
if (data.submitted_at !== undefined) updatePayload.submitted_at = data.submitted_at;
|
|
|
|
|
|
if (data.graded_at !== undefined) updatePayload.graded_at = data.graded_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await exercise_submissions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.exercise !== undefined) {
|
|
await exercise_submissions.setExercise(
|
|
|
|
data.exercise,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.user !== undefined) {
|
|
await exercise_submissions.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercise_submissions.getTableName(),
|
|
belongsToColumn: 'answer_files',
|
|
belongsToId: exercise_submissions.id,
|
|
},
|
|
data.answer_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return exercise_submissions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercise_submissions = await db.exercise_submissions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of exercise_submissions) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of exercise_submissions) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return exercise_submissions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercise_submissions = await db.exercise_submissions.findByPk(id, options);
|
|
|
|
await exercise_submissions.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await exercise_submissions.destroy({
|
|
transaction
|
|
});
|
|
|
|
return exercise_submissions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercise_submissions = await db.exercise_submissions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!exercise_submissions) {
|
|
return exercise_submissions;
|
|
}
|
|
|
|
const output = exercise_submissions.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.exercise = await exercise_submissions.getExercise({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.user = await exercise_submissions.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.answer_files = await exercise_submissions.getAnswer_files({
|
|
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.exercises,
|
|
as: 'exercise',
|
|
|
|
where: filter.exercise ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.exercise.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
prompt: {
|
|
[Op.or]: filter.exercise.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'user',
|
|
|
|
where: filter.user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'answer_files',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.answer_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'exercise_submissions',
|
|
'answer_text',
|
|
filter.answer_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.scoreRange) {
|
|
const [start, end] = filter.scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
score: {
|
|
...where.score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
score: {
|
|
...where.score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.submitted_atRange) {
|
|
const [start, end] = filter.submitted_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
submitted_at: {
|
|
...where.submitted_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
submitted_at: {
|
|
...where.submitted_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.graded_atRange) {
|
|
const [start, end] = filter.graded_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
graded_at: {
|
|
...where.graded_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
graded_at: {
|
|
...where.graded_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.is_correct) {
|
|
where = {
|
|
...where,
|
|
is_correct: filter.is_correct,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.exercise_submissions.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(
|
|
'exercise_submissions',
|
|
'answer_text',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.exercise_submissions.findAll({
|
|
attributes: [ 'id', 'answer_text' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['answer_text', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.answer_text,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|