32954/backend/src/db/api/keystroke_data.js
2025-07-21 14:38:06 +00:00

462 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 Keystroke_dataDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const keystroke_data = await db.keystroke_data.create(
{
id: data.id || undefined,
dwell_time: data.dwell_time || null,
flight_time: data.flight_time || null,
typing_speed: data.typing_speed || null,
error_rate: data.error_rate || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await keystroke_data.setSession(data.session || null, {
transaction,
});
await keystroke_data.setOrganizations(data.organizations || null, {
transaction,
});
return keystroke_data;
}
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 keystroke_dataData = data.map((item, index) => ({
id: item.id || undefined,
dwell_time: item.dwell_time || null,
flight_time: item.flight_time || null,
typing_speed: item.typing_speed || null,
error_rate: item.error_rate || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const keystroke_data = await db.keystroke_data.bulkCreate(
keystroke_dataData,
{ transaction },
);
// For each item created, replace relation files
return keystroke_data;
}
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 keystroke_data = await db.keystroke_data.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.dwell_time !== undefined)
updatePayload.dwell_time = data.dwell_time;
if (data.flight_time !== undefined)
updatePayload.flight_time = data.flight_time;
if (data.typing_speed !== undefined)
updatePayload.typing_speed = data.typing_speed;
if (data.error_rate !== undefined)
updatePayload.error_rate = data.error_rate;
updatePayload.updatedById = currentUser.id;
await keystroke_data.update(updatePayload, { transaction });
if (data.session !== undefined) {
await keystroke_data.setSession(
data.session,
{ transaction },
);
}
if (data.organizations !== undefined) {
await keystroke_data.setOrganizations(
data.organizations,
{ transaction },
);
}
return keystroke_data;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const keystroke_data = await db.keystroke_data.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of keystroke_data) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of keystroke_data) {
await record.destroy({ transaction });
}
});
return keystroke_data;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const keystroke_data = await db.keystroke_data.findByPk(id, options);
await keystroke_data.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await keystroke_data.destroy({
transaction,
});
return keystroke_data;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const keystroke_data = await db.keystroke_data.findOne(
{ where },
{ transaction },
);
if (!keystroke_data) {
return keystroke_data;
}
const output = keystroke_data.get({ plain: true });
output.session = await keystroke_data.getSession({
transaction,
});
output.organizations = await keystroke_data.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.sessions,
as: 'session',
where: filter.session
? {
[Op.or]: [
{
id: {
[Op.in]: filter.session
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
start_time: {
[Op.or]: filter.session
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
{
model: db.organizations,
as: 'organizations',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.dwell_timeRange) {
const [start, end] = filter.dwell_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
dwell_time: {
...where.dwell_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
dwell_time: {
...where.dwell_time,
[Op.lte]: end,
},
};
}
}
if (filter.flight_timeRange) {
const [start, end] = filter.flight_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
flight_time: {
...where.flight_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
flight_time: {
...where.flight_time,
[Op.lte]: end,
},
};
}
}
if (filter.typing_speedRange) {
const [start, end] = filter.typing_speedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
typing_speed: {
...where.typing_speed,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
typing_speed: {
...where.typing_speed,
[Op.lte]: end,
},
};
}
}
if (filter.error_rateRange) {
const [start, end] = filter.error_rateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
error_rate: {
...where.error_rate,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
error_rate: {
...where.error_rate,
[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.keystroke_data.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('keystroke_data', 'dwell_time', query),
],
};
}
const records = await db.keystroke_data.findAll({
attributes: ['id', 'dwell_time'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['dwell_time', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.dwell_time,
}));
}
};