38884-vm/backend/src/db/api/learner_profiles.js
2026-02-28 19:55:21 +00:00

485 lines
11 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 Learner_profilesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const learner_profiles = await db.learner_profiles.create(
{
id: data.id || undefined,
main_goal: data.main_goal
||
null
,
preferred_session_format: data.preferred_session_format
||
null
,
session_language_preference: data.session_language_preference
||
null
,
budget_range: data.budget_range
||
null
,
availability_json: data.availability_json
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await learner_profiles.setUser( data.user || null, {
transaction,
});
return learner_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 learner_profilesData = data.map((item, index) => ({
id: item.id || undefined,
main_goal: item.main_goal
||
null
,
preferred_session_format: item.preferred_session_format
||
null
,
session_language_preference: item.session_language_preference
||
null
,
budget_range: item.budget_range
||
null
,
availability_json: item.availability_json
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const learner_profiles = await db.learner_profiles.bulkCreate(learner_profilesData, { transaction });
// For each item created, replace relation files
return learner_profiles;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const learner_profiles = await db.learner_profiles.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.main_goal !== undefined) updatePayload.main_goal = data.main_goal;
if (data.preferred_session_format !== undefined) updatePayload.preferred_session_format = data.preferred_session_format;
if (data.session_language_preference !== undefined) updatePayload.session_language_preference = data.session_language_preference;
if (data.budget_range !== undefined) updatePayload.budget_range = data.budget_range;
if (data.availability_json !== undefined) updatePayload.availability_json = data.availability_json;
updatePayload.updatedById = currentUser.id;
await learner_profiles.update(updatePayload, {transaction});
if (data.user !== undefined) {
await learner_profiles.setUser(
data.user,
{ transaction }
);
}
return learner_profiles;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const learner_profiles = await db.learner_profiles.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of learner_profiles) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of learner_profiles) {
await record.destroy({transaction});
}
});
return learner_profiles;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const learner_profiles = await db.learner_profiles.findByPk(id, options);
await learner_profiles.update({
deletedBy: currentUser.id
}, {
transaction,
});
await learner_profiles.destroy({
transaction
});
return learner_profiles;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const learner_profiles = await db.learner_profiles.findOne(
{ where },
{ transaction },
);
if (!learner_profiles) {
return learner_profiles;
}
const output = learner_profiles.get({plain: true});
output.user = await learner_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.availability_json) {
where = {
...where,
[Op.and]: Utils.ilike(
'learner_profiles',
'availability_json',
filter.availability_json,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.main_goal) {
where = {
...where,
main_goal: filter.main_goal,
};
}
if (filter.preferred_session_format) {
where = {
...where,
preferred_session_format: filter.preferred_session_format,
};
}
if (filter.session_language_preference) {
where = {
...where,
session_language_preference: filter.session_language_preference,
};
}
if (filter.budget_range) {
where = {
...where,
budget_range: filter.budget_range,
};
}
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.learner_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(
'learner_profiles',
'main_goal',
query,
),
],
};
}
const records = await db.learner_profiles.findAll({
attributes: [ 'id', 'main_goal' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['main_goal', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.main_goal,
}));
}
};