38478-vm/backend/src/db/api/reconnect_logs.js
2026-02-16 11:58:59 +00:00

494 lines
12 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 Reconnect_logsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const reconnect_logs = await db.reconnect_logs.create(
{
id: data.id || undefined,
log_name: data.log_name
||
null
,
occurred_at: data.occurred_at
||
null
,
result: data.result
||
null
,
attempt_number: data.attempt_number
||
null
,
details: data.details
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await reconnect_logs.setVoice_session( data.voice_session || null, {
transaction,
});
return reconnect_logs;
}
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 reconnect_logsData = data.map((item, index) => ({
id: item.id || undefined,
log_name: item.log_name
||
null
,
occurred_at: item.occurred_at
||
null
,
result: item.result
||
null
,
attempt_number: item.attempt_number
||
null
,
details: item.details
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const reconnect_logs = await db.reconnect_logs.bulkCreate(reconnect_logsData, { transaction });
// For each item created, replace relation files
return reconnect_logs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const reconnect_logs = await db.reconnect_logs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.log_name !== undefined) updatePayload.log_name = data.log_name;
if (data.occurred_at !== undefined) updatePayload.occurred_at = data.occurred_at;
if (data.result !== undefined) updatePayload.result = data.result;
if (data.attempt_number !== undefined) updatePayload.attempt_number = data.attempt_number;
if (data.details !== undefined) updatePayload.details = data.details;
updatePayload.updatedById = currentUser.id;
await reconnect_logs.update(updatePayload, {transaction});
if (data.voice_session !== undefined) {
await reconnect_logs.setVoice_session(
data.voice_session,
{ transaction }
);
}
return reconnect_logs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const reconnect_logs = await db.reconnect_logs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of reconnect_logs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of reconnect_logs) {
await record.destroy({transaction});
}
});
return reconnect_logs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const reconnect_logs = await db.reconnect_logs.findByPk(id, options);
await reconnect_logs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await reconnect_logs.destroy({
transaction
});
return reconnect_logs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const reconnect_logs = await db.reconnect_logs.findOne(
{ where },
{ transaction },
);
if (!reconnect_logs) {
return reconnect_logs;
}
const output = reconnect_logs.get({plain: true});
output.voice_session = await reconnect_logs.getVoice_session({
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.voice_sessions,
as: 'voice_session',
where: filter.voice_session ? {
[Op.or]: [
{ id: { [Op.in]: filter.voice_session.split('|').map(term => Utils.uuid(term)) } },
{
session_name: {
[Op.or]: filter.voice_session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.log_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'reconnect_logs',
'log_name',
filter.log_name,
),
};
}
if (filter.details) {
where = {
...where,
[Op.and]: Utils.ilike(
'reconnect_logs',
'details',
filter.details,
),
};
}
if (filter.occurred_atRange) {
const [start, end] = filter.occurred_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
occurred_at: {
...where.occurred_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
occurred_at: {
...where.occurred_at,
[Op.lte]: end,
},
};
}
}
if (filter.attempt_numberRange) {
const [start, end] = filter.attempt_numberRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
attempt_number: {
...where.attempt_number,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
attempt_number: {
...where.attempt_number,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.result) {
where = {
...where,
result: filter.result,
};
}
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.reconnect_logs.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(
'reconnect_logs',
'log_name',
query,
),
],
};
}
const records = await db.reconnect_logs.findAll({
attributes: [ 'id', 'log_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['log_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.log_name,
}));
}
};