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 Business_ideasDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const business_ideas = await db.business_ideas.create( { id: data.id || undefined, title: data.title || null, description: data.description || null, status: data.status || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await business_ideas.setCreator(data.creator || null, { transaction, }); await business_ideas.setCreators(data.creators || null, { transaction, }); return business_ideas; } 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 business_ideasData = data.map((item, index) => ({ id: item.id || undefined, title: item.title || null, description: item.description || null, status: item.status || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const business_ideas = await db.business_ideas.bulkCreate( business_ideasData, { transaction }, ); // For each item created, replace relation files return business_ideas; } 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 business_ideas = await db.business_ideas.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.title !== undefined) updatePayload.title = data.title; if (data.description !== undefined) updatePayload.description = data.description; if (data.status !== undefined) updatePayload.status = data.status; updatePayload.updatedById = currentUser.id; await business_ideas.update(updatePayload, { transaction }); if (data.creator !== undefined) { await business_ideas.setCreator( data.creator, { transaction }, ); } if (data.creators !== undefined) { await business_ideas.setCreators( data.creators, { transaction }, ); } return business_ideas; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const business_ideas = await db.business_ideas.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of business_ideas) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of business_ideas) { await record.destroy({ transaction }); } }); return business_ideas; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const business_ideas = await db.business_ideas.findByPk(id, options); await business_ideas.update( { deletedBy: currentUser.id, }, { transaction, }, ); await business_ideas.destroy({ transaction, }); return business_ideas; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const business_ideas = await db.business_ideas.findOne( { where }, { transaction }, ); if (!business_ideas) { return business_ideas; } const output = business_ideas.get({ plain: true }); output.executions_business_idea = await business_ideas.getExecutions_business_idea({ transaction, }); output.optimizations_business_idea = await business_ideas.getOptimizations_business_idea({ transaction, }); output.validations_business_idea = await business_ideas.getValidations_business_idea({ transaction, }); output.creator = await business_ideas.getCreator({ transaction, }); output.creators = await business_ideas.getCreators({ 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 userCreators = (user && user.creators?.id) || null; if (userCreators) { if (options?.currentUser?.creatorsId) { where.creatorsId = options.currentUser.creatorsId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.creators, as: 'creator', }, { model: db.creators, as: 'creators', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.title) { where = { ...where, [Op.and]: Utils.ilike('business_ideas', 'title', filter.title), }; } if (filter.description) { where = { ...where, [Op.and]: Utils.ilike( 'business_ideas', 'description', filter.description, ), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.creator) { const listItems = filter.creator.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, creatorId: { [Op.or]: listItems }, }; } if (filter.creators) { const listItems = filter.creators.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, creatorsId: { [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.creatorsId; } 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.business_ideas.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('business_ideas', 'title', query), ], }; } const records = await db.business_ideas.findAll({ attributes: ['id', 'title'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['title', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.title, })); } };