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 Http_requestsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const http_requests = await db.http_requests.create( { id: data.id || undefined, url: data.url || null , method: data.method || null , status_code: data.status_code || null , response_time_ms: data.response_time_ms || null , content_length: data.content_length || null , content_type: data.content_type || null , redirect_location: data.redirect_location || null , requested_at: data.requested_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await http_requests.setScan( data.scan || null, { transaction, }); await http_requests.setOrganizations( data.organizations || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.http_requests.getTableName(), belongsToColumn: 'response_body_file', belongsToId: http_requests.id, }, data.response_body_file, options, ); return http_requests; } 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 http_requestsData = data.map((item, index) => ({ id: item.id || undefined, url: item.url || null , method: item.method || null , status_code: item.status_code || null , response_time_ms: item.response_time_ms || null , content_length: item.content_length || null , content_type: item.content_type || null , redirect_location: item.redirect_location || null , requested_at: item.requested_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const http_requests = await db.http_requests.bulkCreate(http_requestsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < http_requests.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.http_requests.getTableName(), belongsToColumn: 'response_body_file', belongsToId: http_requests[i].id, }, data[i].response_body_file, options, ); } return http_requests; } 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 http_requests = await db.http_requests.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.url !== undefined) updatePayload.url = data.url; if (data.method !== undefined) updatePayload.method = data.method; if (data.status_code !== undefined) updatePayload.status_code = data.status_code; if (data.response_time_ms !== undefined) updatePayload.response_time_ms = data.response_time_ms; if (data.content_length !== undefined) updatePayload.content_length = data.content_length; if (data.content_type !== undefined) updatePayload.content_type = data.content_type; if (data.redirect_location !== undefined) updatePayload.redirect_location = data.redirect_location; if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at; updatePayload.updatedById = currentUser.id; await http_requests.update(updatePayload, {transaction}); if (data.scan !== undefined) { await http_requests.setScan( data.scan, { transaction } ); } if (data.organizations !== undefined) { await http_requests.setOrganizations( data.organizations, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.http_requests.getTableName(), belongsToColumn: 'response_body_file', belongsToId: http_requests.id, }, data.response_body_file, options, ); return http_requests; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const http_requests = await db.http_requests.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of http_requests) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of http_requests) { await record.destroy({transaction}); } }); return http_requests; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const http_requests = await db.http_requests.findByPk(id, options); await http_requests.update({ deletedBy: currentUser.id }, { transaction, }); await http_requests.destroy({ transaction }); return http_requests; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const http_requests = await db.http_requests.findOne( { where }, { transaction }, ); if (!http_requests) { return http_requests; } const output = http_requests.get({plain: true}); output.scan = await http_requests.getScan({ transaction }); output.response_body_file = await http_requests.getResponse_body_file({ transaction }); output.organizations = await http_requests.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.scans, as: 'scan', where: filter.scan ? { [Op.or]: [ { id: { [Op.in]: filter.scan.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.scan.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.organizations, as: 'organizations', }, { model: db.file, as: 'response_body_file', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.url) { where = { ...where, [Op.and]: Utils.ilike( 'http_requests', 'url', filter.url, ), }; } if (filter.method) { where = { ...where, [Op.and]: Utils.ilike( 'http_requests', 'method', filter.method, ), }; } if (filter.content_type) { where = { ...where, [Op.and]: Utils.ilike( 'http_requests', 'content_type', filter.content_type, ), }; } if (filter.redirect_location) { where = { ...where, [Op.and]: Utils.ilike( 'http_requests', 'redirect_location', filter.redirect_location, ), }; } if (filter.status_codeRange) { const [start, end] = filter.status_codeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, status_code: { ...where.status_code, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, status_code: { ...where.status_code, [Op.lte]: end, }, }; } } if (filter.response_time_msRange) { const [start, end] = filter.response_time_msRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, response_time_ms: { ...where.response_time_ms, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, response_time_ms: { ...where.response_time_ms, [Op.lte]: end, }, }; } } if (filter.content_lengthRange) { const [start, end] = filter.content_lengthRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, content_length: { ...where.content_length, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, content_length: { ...where.content_length, [Op.lte]: end, }, }; } } if (filter.requested_atRange) { const [start, end] = filter.requested_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, requested_at: { ...where.requested_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, requested_at: { ...where.requested_at, [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.http_requests.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( 'http_requests', 'url', query, ), ], }; } const records = await db.http_requests.findAll({ attributes: [ 'id', 'url' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['url', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.url, })); } };