38064-vm/backend/src/db/api/scholar_profiles.js
2026-02-01 15:00:22 +00:00

512 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 Scholar_profilesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const scholar_profiles = await db.scholar_profiles.create(
{
id: data.id || undefined,
id_number: data.id_number
||
null
,
course: data.course
||
null
,
college: data.college
||
null
,
scholar_grant: data.scholar_grant
||
null
,
required_hours: data.required_hours
||
null
,
initial_rendered_hours: data.initial_rendered_hours
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await scholar_profiles.setUser( data.user || null, {
transaction,
});
return scholar_profiles;
}
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 scholar_profilesData = data.map((item, index) => ({
id: item.id || undefined,
id_number: item.id_number
||
null
,
course: item.course
||
null
,
college: item.college
||
null
,
scholar_grant: item.scholar_grant
||
null
,
required_hours: item.required_hours
||
null
,
initial_rendered_hours: item.initial_rendered_hours
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const scholar_profiles = await db.scholar_profiles.bulkCreate(scholar_profilesData, { transaction });
// For each item created, replace relation files
return scholar_profiles;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const scholar_profiles = await db.scholar_profiles.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.id_number !== undefined) updatePayload.id_number = data.id_number;
if (data.course !== undefined) updatePayload.course = data.course;
if (data.college !== undefined) updatePayload.college = data.college;
if (data.scholar_grant !== undefined) updatePayload.scholar_grant = data.scholar_grant;
if (data.required_hours !== undefined) updatePayload.required_hours = data.required_hours;
if (data.initial_rendered_hours !== undefined) updatePayload.initial_rendered_hours = data.initial_rendered_hours;
updatePayload.updatedById = currentUser.id;
await scholar_profiles.update(updatePayload, {transaction});
if (data.user !== undefined) {
await scholar_profiles.setUser(
data.user,
{ transaction }
);
}
return scholar_profiles;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const scholar_profiles = await db.scholar_profiles.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of scholar_profiles) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of scholar_profiles) {
await record.destroy({transaction});
}
});
return scholar_profiles;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const scholar_profiles = await db.scholar_profiles.findByPk(id, options);
await scholar_profiles.update({
deletedBy: currentUser.id
}, {
transaction,
});
await scholar_profiles.destroy({
transaction
});
return scholar_profiles;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const scholar_profiles = await db.scholar_profiles.findOne(
{ where },
{ transaction },
);
if (!scholar_profiles) {
return scholar_profiles;
}
const output = scholar_profiles.get({plain: true});
output.user = await scholar_profiles.getUser({
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.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}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.id_number) {
where = {
...where,
[Op.and]: Utils.ilike(
'scholar_profiles',
'id_number',
filter.id_number,
),
};
}
if (filter.course) {
where = {
...where,
[Op.and]: Utils.ilike(
'scholar_profiles',
'course',
filter.course,
),
};
}
if (filter.scholar_grant) {
where = {
...where,
[Op.and]: Utils.ilike(
'scholar_profiles',
'scholar_grant',
filter.scholar_grant,
),
};
}
if (filter.required_hoursRange) {
const [start, end] = filter.required_hoursRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
required_hours: {
...where.required_hours,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
required_hours: {
...where.required_hours,
[Op.lte]: end,
},
};
}
}
if (filter.initial_rendered_hoursRange) {
const [start, end] = filter.initial_rendered_hoursRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
initial_rendered_hours: {
...where.initial_rendered_hours,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
initial_rendered_hours: {
...where.initial_rendered_hours,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.college) {
where = {
...where,
college: filter.college,
};
}
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.scholar_profiles.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(
'scholar_profiles',
'id_number',
query,
),
],
};
}
const records = await db.scholar_profiles.findAll({
attributes: [ 'id', 'id_number' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['id_number', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.id_number,
}));
}
};