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 Operational_logsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const operational_logs = await db.operational_logs.create( { id: data.id || undefined, log_time: data.log_time || null , severity: data.severity || null , message: data.message || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await operational_logs.setFlight( data.flight || null, { transaction, }); await operational_logs.setLogged_by_user( data.logged_by_user || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.operational_logs.getTableName(), belongsToColumn: 'attachments', belongsToId: operational_logs.id, }, data.attachments, options, ); return operational_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 operational_logsData = data.map((item, index) => ({ id: item.id || undefined, log_time: item.log_time || null , severity: item.severity || null , message: item.message || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const operational_logs = await db.operational_logs.bulkCreate(operational_logsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < operational_logs.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.operational_logs.getTableName(), belongsToColumn: 'attachments', belongsToId: operational_logs[i].id, }, data[i].attachments, options, ); } return operational_logs; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const operational_logs = await db.operational_logs.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.log_time !== undefined) updatePayload.log_time = data.log_time; if (data.severity !== undefined) updatePayload.severity = data.severity; if (data.message !== undefined) updatePayload.message = data.message; updatePayload.updatedById = currentUser.id; await operational_logs.update(updatePayload, {transaction}); if (data.flight !== undefined) { await operational_logs.setFlight( data.flight, { transaction } ); } if (data.logged_by_user !== undefined) { await operational_logs.setLogged_by_user( data.logged_by_user, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.operational_logs.getTableName(), belongsToColumn: 'attachments', belongsToId: operational_logs.id, }, data.attachments, options, ); return operational_logs; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const operational_logs = await db.operational_logs.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of operational_logs) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of operational_logs) { await record.destroy({transaction}); } }); return operational_logs; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const operational_logs = await db.operational_logs.findByPk(id, options); await operational_logs.update({ deletedBy: currentUser.id }, { transaction, }); await operational_logs.destroy({ transaction }); return operational_logs; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const operational_logs = await db.operational_logs.findOne( { where }, { transaction }, ); if (!operational_logs) { return operational_logs; } const output = operational_logs.get({plain: true}); output.flight = await operational_logs.getFlight({ transaction }); output.logged_by_user = await operational_logs.getLogged_by_user({ transaction }); output.attachments = await operational_logs.getAttachments({ 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.flights, as: 'flight', where: filter.flight ? { [Op.or]: [ { id: { [Op.in]: filter.flight.split('|').map(term => Utils.uuid(term)) } }, { flight_number: { [Op.or]: filter.flight.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.users, as: 'logged_by_user', where: filter.logged_by_user ? { [Op.or]: [ { id: { [Op.in]: filter.logged_by_user.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.logged_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'attachments', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.message) { where = { ...where, [Op.and]: Utils.ilike( 'operational_logs', 'message', filter.message, ), }; } if (filter.log_timeRange) { const [start, end] = filter.log_timeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, log_time: { ...where.log_time, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, log_time: { ...where.log_time, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.severity) { where = { ...where, severity: filter.severity, }; } 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.operational_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( 'operational_logs', 'message', query, ), ], }; } const records = await db.operational_logs.findAll({ attributes: [ 'id', 'message' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['message', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.message, })); } };