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 ChannelsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const channels = await db.channels.create( { id: data.id || undefined, provider: data.provider || null , display_name: data.display_name || null , external_account_reference: data.external_account_reference || null , status: data.status || null , is_enabled: data.is_enabled || false , connected_at: data.connected_at || null , last_sync_at: data.last_sync_at || null , webhook_url: data.webhook_url || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); return channels; } 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 channelsData = data.map((item, index) => ({ id: item.id || undefined, provider: item.provider || null , display_name: item.display_name || null , external_account_reference: item.external_account_reference || null , status: item.status || null , is_enabled: item.is_enabled || false , connected_at: item.connected_at || null , last_sync_at: item.last_sync_at || null , webhook_url: item.webhook_url || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const channels = await db.channels.bulkCreate(channelsData, { transaction }); // For each item created, replace relation files return channels; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const channels = await db.channels.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.provider !== undefined) updatePayload.provider = data.provider; if (data.display_name !== undefined) updatePayload.display_name = data.display_name; if (data.external_account_reference !== undefined) updatePayload.external_account_reference = data.external_account_reference; if (data.status !== undefined) updatePayload.status = data.status; if (data.is_enabled !== undefined) updatePayload.is_enabled = data.is_enabled; if (data.connected_at !== undefined) updatePayload.connected_at = data.connected_at; if (data.last_sync_at !== undefined) updatePayload.last_sync_at = data.last_sync_at; if (data.webhook_url !== undefined) updatePayload.webhook_url = data.webhook_url; updatePayload.updatedById = currentUser.id; await channels.update(updatePayload, {transaction}); return channels; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const channels = await db.channels.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of channels) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of channels) { await record.destroy({transaction}); } }); return channels; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const channels = await db.channels.findByPk(id, options); await channels.update({ deletedBy: currentUser.id }, { transaction, }); await channels.destroy({ transaction }); return channels; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const channels = await db.channels.findOne( { where }, { transaction }, ); if (!channels) { return channels; } const output = channels.get({plain: true}); output.conversations_channel = await channels.getConversations_channel({ transaction }); output.automation_rules_channel = await channels.getAutomation_rules_channel({ 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 = [ ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.display_name) { where = { ...where, [Op.and]: Utils.ilike( 'channels', 'display_name', filter.display_name, ), }; } if (filter.external_account_reference) { where = { ...where, [Op.and]: Utils.ilike( 'channels', 'external_account_reference', filter.external_account_reference, ), }; } if (filter.webhook_url) { where = { ...where, [Op.and]: Utils.ilike( 'channels', 'webhook_url', filter.webhook_url, ), }; } if (filter.connected_atRange) { const [start, end] = filter.connected_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, connected_at: { ...where.connected_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, connected_at: { ...where.connected_at, [Op.lte]: end, }, }; } } if (filter.last_sync_atRange) { const [start, end] = filter.last_sync_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, last_sync_at: { ...where.last_sync_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, last_sync_at: { ...where.last_sync_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.provider) { where = { ...where, provider: filter.provider, }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.is_enabled) { where = { ...where, is_enabled: filter.is_enabled, }; } 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.channels.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( 'channels', 'display_name', query, ), ], }; } const records = await db.channels.findAll({ attributes: [ 'id', 'display_name' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['display_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.display_name, })); } };