485 lines
12 KiB
JavaScript
485 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 SubmissionsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const submissions = await db.submissions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
submission_date: data.submission_date || null,
|
|
grade: data.grade || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await submissions.setAssignment(data.assignment || null, {
|
|
transaction,
|
|
});
|
|
|
|
await submissions.setStudent(data.student || null, {
|
|
transaction,
|
|
});
|
|
|
|
await submissions.setOrganizations(data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.submissions.getTableName(),
|
|
belongsToColumn: 'submitted_files',
|
|
belongsToId: submissions.id,
|
|
},
|
|
data.submitted_files,
|
|
options,
|
|
);
|
|
|
|
return 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 submissionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
submission_date: item.submission_date || null,
|
|
grade: item.grade || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const submissions = await db.submissions.bulkCreate(submissionsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < submissions.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.submissions.getTableName(),
|
|
belongsToColumn: 'submitted_files',
|
|
belongsToId: submissions[i].id,
|
|
},
|
|
data[i].submitted_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
return submissions;
|
|
}
|
|
|
|
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 submissions = await db.submissions.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.submission_date !== undefined)
|
|
updatePayload.submission_date = data.submission_date;
|
|
|
|
if (data.grade !== undefined) updatePayload.grade = data.grade;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await submissions.update(updatePayload, { transaction });
|
|
|
|
if (data.assignment !== undefined) {
|
|
await submissions.setAssignment(
|
|
data.assignment,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.student !== undefined) {
|
|
await submissions.setStudent(
|
|
data.student,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await submissions.setOrganizations(
|
|
data.organizations,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.submissions.getTableName(),
|
|
belongsToColumn: 'submitted_files',
|
|
belongsToId: submissions.id,
|
|
},
|
|
data.submitted_files,
|
|
options,
|
|
);
|
|
|
|
return submissions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const submissions = await db.submissions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of submissions) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of submissions) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return submissions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const submissions = await db.submissions.findByPk(id, options);
|
|
|
|
await submissions.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await submissions.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return submissions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const submissions = await db.submissions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!submissions) {
|
|
return submissions;
|
|
}
|
|
|
|
const output = submissions.get({ plain: true });
|
|
|
|
output.reviews_submission = await submissions.getReviews_submission({
|
|
transaction,
|
|
});
|
|
|
|
output.assignment = await submissions.getAssignment({
|
|
transaction,
|
|
});
|
|
|
|
output.student = await submissions.getStudent({
|
|
transaction,
|
|
});
|
|
|
|
output.submitted_files = await submissions.getSubmitted_files({
|
|
transaction,
|
|
});
|
|
|
|
output.organizations = await submissions.getOrganizations({
|
|
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 userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.assignments,
|
|
as: 'assignment',
|
|
|
|
where: filter.assignment
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.assignment
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.assignment
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'student',
|
|
|
|
where: filter.student
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.student
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.student
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'submitted_files',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.submission_dateRange) {
|
|
const [start, end] = filter.submission_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
submission_date: {
|
|
...where.submission_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
submission_date: {
|
|
...where.submission_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.gradeRange) {
|
|
const [start, end] = filter.gradeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
grade: {
|
|
...where.grade,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
grade: {
|
|
...where.grade,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: { [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.organizationsId;
|
|
}
|
|
|
|
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.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,
|
|
globalAccess,
|
|
organizationId,
|
|
) {
|
|
let where = {};
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike('submissions', 'submission_date', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.submissions.findAll({
|
|
attributes: ['id', 'submission_date'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['submission_date', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.submission_date,
|
|
}));
|
|
}
|
|
};
|