39841-vm/backend/src/db/api/terminal_files.js
2026-04-29 19:06:40 +00:00

535 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 Terminal_filesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const terminal_files = await db.terminal_files.create(
{
id: data.id || undefined,
kind: data.kind
||
null
,
filename: data.filename
||
null
,
content_type: data.content_type
||
null
,
size_kb: data.size_kb
||
null
,
captured_at: data.captured_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await terminal_files.setSession( data.session || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.terminal_files.getTableName(),
belongsToColumn: 'file_blob',
belongsToId: terminal_files.id,
},
data.file_blob,
options,
);
return terminal_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 terminal_filesData = data.map((item, index) => ({
id: item.id || undefined,
kind: item.kind
||
null
,
filename: item.filename
||
null
,
content_type: item.content_type
||
null
,
size_kb: item.size_kb
||
null
,
captured_at: item.captured_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const terminal_files = await db.terminal_files.bulkCreate(terminal_filesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < terminal_files.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.terminal_files.getTableName(),
belongsToColumn: 'file_blob',
belongsToId: terminal_files[i].id,
},
data[i].file_blob,
options,
);
}
return terminal_files;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const terminal_files = await db.terminal_files.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.kind !== undefined) updatePayload.kind = data.kind;
if (data.filename !== undefined) updatePayload.filename = data.filename;
if (data.content_type !== undefined) updatePayload.content_type = data.content_type;
if (data.size_kb !== undefined) updatePayload.size_kb = data.size_kb;
if (data.captured_at !== undefined) updatePayload.captured_at = data.captured_at;
updatePayload.updatedById = currentUser.id;
await terminal_files.update(updatePayload, {transaction});
if (data.session !== undefined) {
await terminal_files.setSession(
data.session,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.terminal_files.getTableName(),
belongsToColumn: 'file_blob',
belongsToId: terminal_files.id,
},
data.file_blob,
options,
);
return terminal_files;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const terminal_files = await db.terminal_files.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of terminal_files) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of terminal_files) {
await record.destroy({transaction});
}
});
return terminal_files;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const terminal_files = await db.terminal_files.findByPk(id, options);
await terminal_files.update({
deletedBy: currentUser.id
}, {
transaction,
});
await terminal_files.destroy({
transaction
});
return terminal_files;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const terminal_files = await db.terminal_files.findOne(
{ where },
{ transaction },
);
if (!terminal_files) {
return terminal_files;
}
const output = terminal_files.get({plain: true});
output.session = await terminal_files.getSession({
transaction
});
output.file_blob = await terminal_files.getFile_blob({
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.terminal_sessions,
as: 'session',
where: filter.session ? {
[Op.or]: [
{ id: { [Op.in]: filter.session.split('|').map(term => Utils.uuid(term)) } },
{
session_key: {
[Op.or]: filter.session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'file_blob',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.filename) {
where = {
...where,
[Op.and]: Utils.ilike(
'terminal_files',
'filename',
filter.filename,
),
};
}
if (filter.content_type) {
where = {
...where,
[Op.and]: Utils.ilike(
'terminal_files',
'content_type',
filter.content_type,
),
};
}
if (filter.size_kbRange) {
const [start, end] = filter.size_kbRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
size_kb: {
...where.size_kb,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
size_kb: {
...where.size_kb,
[Op.lte]: end,
},
};
}
}
if (filter.captured_atRange) {
const [start, end] = filter.captured_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
captured_at: {
...where.captured_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
captured_at: {
...where.captured_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.kind) {
where = {
...where,
kind: filter.kind,
};
}
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.terminal_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(
'terminal_files',
'filename',
query,
),
],
};
}
const records = await db.terminal_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,
}));
}
};