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 BotsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const bots = await db.bots.create( { id: data.id || undefined, name: data.name || null, status: data.status || null, number_of_orders: data.number_of_orders || null, order_block_size: data.order_block_size || null, initial_order_size: data.initial_order_size || null, order_size_increment: data.order_size_increment || null, price_drop_to_buy_next_order: data.price_drop_to_buy_next_order || null, take_profit: data.take_profit || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await bots.setUser(data.user || null, { transaction, }); return bots; } 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 botsData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null, status: item.status || null, number_of_orders: item.number_of_orders || null, order_block_size: item.order_block_size || null, initial_order_size: item.initial_order_size || null, order_size_increment: item.order_size_increment || null, price_drop_to_buy_next_order: item.price_drop_to_buy_next_order || null, take_profit: item.take_profit || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const bots = await db.bots.bulkCreate(botsData, { transaction }); // For each item created, replace relation files return bots; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const bots = await db.bots.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.status !== undefined) updatePayload.status = data.status; if (data.number_of_orders !== undefined) updatePayload.number_of_orders = data.number_of_orders; if (data.order_block_size !== undefined) updatePayload.order_block_size = data.order_block_size; if (data.initial_order_size !== undefined) updatePayload.initial_order_size = data.initial_order_size; if (data.order_size_increment !== undefined) updatePayload.order_size_increment = data.order_size_increment; if (data.price_drop_to_buy_next_order !== undefined) updatePayload.price_drop_to_buy_next_order = data.price_drop_to_buy_next_order; if (data.take_profit !== undefined) updatePayload.take_profit = data.take_profit; updatePayload.updatedById = currentUser.id; await bots.update(updatePayload, { transaction }); if (data.user !== undefined) { await bots.setUser( data.user, { transaction }, ); } return bots; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const bots = await db.bots.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of bots) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of bots) { await record.destroy({ transaction }); } }); return bots; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const bots = await db.bots.findByPk(id, options); await bots.update( { deletedBy: currentUser.id, }, { transaction, }, ); await bots.destroy({ transaction, }); return bots; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const bots = await db.bots.findOne({ where }, { transaction }); if (!bots) { return bots; } const output = bots.get({ plain: true }); output.trades_bot = await bots.getTrades_bot({ transaction, }); output.user = await bots.getUser({ 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.users, as: 'user', where: filter.user ? { [Op.or]: [ { id: { [Op.in]: filter.user .split('|') .map((term) => Utils.uuid(term)), }, }, { firstName: { [Op.or]: filter.user .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike('bots', 'name', filter.name), }; } if (filter.number_of_ordersRange) { const [start, end] = filter.number_of_ordersRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, number_of_orders: { ...where.number_of_orders, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, number_of_orders: { ...where.number_of_orders, [Op.lte]: end, }, }; } } if (filter.order_block_sizeRange) { const [start, end] = filter.order_block_sizeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, order_block_size: { ...where.order_block_size, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, order_block_size: { ...where.order_block_size, [Op.lte]: end, }, }; } } if (filter.initial_order_sizeRange) { const [start, end] = filter.initial_order_sizeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, initial_order_size: { ...where.initial_order_size, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, initial_order_size: { ...where.initial_order_size, [Op.lte]: end, }, }; } } if (filter.order_size_incrementRange) { const [start, end] = filter.order_size_incrementRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, order_size_increment: { ...where.order_size_increment, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, order_size_increment: { ...where.order_size_increment, [Op.lte]: end, }, }; } } if (filter.price_drop_to_buy_next_orderRange) { const [start, end] = filter.price_drop_to_buy_next_orderRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, price_drop_to_buy_next_order: { ...where.price_drop_to_buy_next_order, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, price_drop_to_buy_next_order: { ...where.price_drop_to_buy_next_order, [Op.lte]: end, }, }; } } if (filter.take_profitRange) { const [start, end] = filter.take_profitRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, take_profit: { ...where.take_profit, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, take_profit: { ...where.take_profit, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.status) { where = { ...where, status: filter.status, }; } 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.bots.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('bots', 'name', query), ], }; } const records = await db.bots.findAll({ attributes: ['id', 'name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.name, })); } };