31220/backend/src/db/api/course_files.js
2025-05-04 06:51:00 +00:00

422 lines
10 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 Course_filesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const course_files = await db.course_files.create(
{
id: data.id || undefined,
section_type: data.section_type || null,
filename: data.filename || null,
status: data.status || null,
notes: data.notes || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await course_files.setCourse(data.course || null, {
transaction,
});
await course_files.setUploaded_by(data.uploaded_by || null, {
transaction,
});
await course_files.setApproved_by(data.approved_by || null, {
transaction,
});
return course_files;
}
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 course_filesData = data.map((item, index) => ({
id: item.id || undefined,
section_type: item.section_type || null,
filename: item.filename || null,
status: item.status || null,
notes: item.notes || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const course_files = await db.course_files.bulkCreate(course_filesData, {
transaction,
});
// For each item created, replace relation files
return course_files;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const course_files = await db.course_files.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.section_type !== undefined)
updatePayload.section_type = data.section_type;
if (data.filename !== undefined) updatePayload.filename = data.filename;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await course_files.update(updatePayload, { transaction });
if (data.course !== undefined) {
await course_files.setCourse(
data.course,
{ transaction },
);
}
if (data.uploaded_by !== undefined) {
await course_files.setUploaded_by(
data.uploaded_by,
{ transaction },
);
}
if (data.approved_by !== undefined) {
await course_files.setApproved_by(
data.approved_by,
{ transaction },
);
}
return course_files;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const course_files = await db.course_files.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of course_files) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of course_files) {
await record.destroy({ transaction });
}
});
return course_files;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const course_files = await db.course_files.findByPk(id, options);
await course_files.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await course_files.destroy({
transaction,
});
return course_files;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const course_files = await db.course_files.findOne(
{ where },
{ transaction },
);
if (!course_files) {
return course_files;
}
const output = course_files.get({ plain: true });
output.workflow_logs_file = await course_files.getWorkflow_logs_file({
transaction,
});
output.course = await course_files.getCourse({
transaction,
});
output.uploaded_by = await course_files.getUploaded_by({
transaction,
});
output.approved_by = await course_files.getApproved_by({
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.courses,
as: 'course',
where: filter.course
? {
[Op.or]: [
{
id: {
[Op.in]: filter.course
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
title: {
[Op.or]: filter.course
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
{
model: db.users,
as: 'uploaded_by',
where: filter.uploaded_by
? {
[Op.or]: [
{
id: {
[Op.in]: filter.uploaded_by
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
firstName: {
[Op.or]: filter.uploaded_by
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
{
model: db.users,
as: 'approved_by',
where: filter.approved_by
? {
[Op.or]: [
{
id: {
[Op.in]: filter.approved_by
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
firstName: {
[Op.or]: filter.approved_by
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.filename) {
where = {
...where,
[Op.and]: Utils.ilike('course_files', 'filename', filter.filename),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike('course_files', 'notes', filter.notes),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.section_type) {
where = {
...where,
section_type: filter.section_type,
};
}
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.course_files.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('course_files', 'filename', query),
],
};
}
const records = await db.course_files.findAll({
attributes: ['id', 'filename'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['filename', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.filename,
}));
}
};