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 Slash_commandsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const slash_commands = await db.slash_commands.create( { id: data.id || undefined, command_name: data.command_name || null , description: data.description || null , command_type: data.command_type || null , is_enabled: data.is_enabled || false , last_registered_at: data.last_registered_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await slash_commands.setBot( data.bot || null, { transaction, }); await slash_commands.setDiscord_guilds( data.discord_guilds || null, { transaction, }); return slash_commands; } 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 slash_commandsData = data.map((item, index) => ({ id: item.id || undefined, command_name: item.command_name || null , description: item.description || null , command_type: item.command_type || null , is_enabled: item.is_enabled || false , last_registered_at: item.last_registered_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const slash_commands = await db.slash_commands.bulkCreate(slash_commandsData, { transaction }); // For each item created, replace relation files return slash_commands; } 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 slash_commands = await db.slash_commands.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.command_name !== undefined) updatePayload.command_name = data.command_name; if (data.description !== undefined) updatePayload.description = data.description; if (data.command_type !== undefined) updatePayload.command_type = data.command_type; if (data.is_enabled !== undefined) updatePayload.is_enabled = data.is_enabled; if (data.last_registered_at !== undefined) updatePayload.last_registered_at = data.last_registered_at; updatePayload.updatedById = currentUser.id; await slash_commands.update(updatePayload, {transaction}); if (data.bot !== undefined) { await slash_commands.setBot( data.bot, { transaction } ); } if (data.discord_guilds !== undefined) { await slash_commands.setDiscord_guilds( data.discord_guilds, { transaction } ); } return slash_commands; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const slash_commands = await db.slash_commands.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of slash_commands) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of slash_commands) { await record.destroy({transaction}); } }); return slash_commands; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const slash_commands = await db.slash_commands.findByPk(id, options); await slash_commands.update({ deletedBy: currentUser.id }, { transaction, }); await slash_commands.destroy({ transaction }); return slash_commands; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const slash_commands = await db.slash_commands.findOne( { where }, { transaction }, ); if (!slash_commands) { return slash_commands; } const output = slash_commands.get({plain: true}); output.command_invocations_command = await slash_commands.getCommand_invocations_command({ transaction }); output.bot = await slash_commands.getBot({ transaction }); output.discord_guilds = await slash_commands.getDiscord_guilds({ 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 userDiscord_guilds = (user && user.discord_guilds?.id) || null; if (userDiscord_guilds) { if (options?.currentUser?.discord_guildsId) { where.discord_guildsId = options.currentUser.discord_guildsId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.discord_bots, as: 'bot', where: filter.bot ? { [Op.or]: [ { id: { [Op.in]: filter.bot.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.bot.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.discord_guilds, as: 'discord_guilds', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.command_name) { where = { ...where, [Op.and]: Utils.ilike( 'slash_commands', 'command_name', filter.command_name, ), }; } if (filter.description) { where = { ...where, [Op.and]: Utils.ilike( 'slash_commands', 'description', filter.description, ), }; } if (filter.last_registered_atRange) { const [start, end] = filter.last_registered_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, last_registered_at: { ...where.last_registered_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, last_registered_at: { ...where.last_registered_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.command_type) { where = { ...where, command_type: filter.command_type, }; } if (filter.is_enabled) { where = { ...where, is_enabled: filter.is_enabled, }; } if (filter.discord_guilds) { const listItems = filter.discord_guilds.split('|').map(item => { return Utils.uuid(item) }); where = { ...where, discord_guildsId: {[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.discord_guildsId; } 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.slash_commands.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( 'slash_commands', 'command_name', query, ), ], }; } const records = await db.slash_commands.findAll({ attributes: [ 'id', 'command_name' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['command_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.command_name, })); } };