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 CharactersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const characters = await db.characters.create( { id: data.id || undefined, name: data.name || null , tagline: data.tagline || null , bio: data.bio || null , personality_traits: data.personality_traits || null , speaking_style: data.speaking_style || null , rules: data.rules || null , scenario: data.scenario || null , greeting_message: data.greeting_message || null , visibility: data.visibility || null , is_nsfw: data.is_nsfw || false , status: data.status || null , likes_count: data.likes_count || null , chats_count: data.chats_count || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await characters.setOwner( data.owner || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.characters.getTableName(), belongsToColumn: 'avatar', belongsToId: characters.id, }, data.avatar, options, ); return characters; } 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 charactersData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null , tagline: item.tagline || null , bio: item.bio || null , personality_traits: item.personality_traits || null , speaking_style: item.speaking_style || null , rules: item.rules || null , scenario: item.scenario || null , greeting_message: item.greeting_message || null , visibility: item.visibility || null , is_nsfw: item.is_nsfw || false , status: item.status || null , likes_count: item.likes_count || null , chats_count: item.chats_count || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const characters = await db.characters.bulkCreate(charactersData, { transaction }); // For each item created, replace relation files for (let i = 0; i < characters.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.characters.getTableName(), belongsToColumn: 'avatar', belongsToId: characters[i].id, }, data[i].avatar, options, ); } return characters; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const characters = await db.characters.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.tagline !== undefined) updatePayload.tagline = data.tagline; if (data.bio !== undefined) updatePayload.bio = data.bio; if (data.personality_traits !== undefined) updatePayload.personality_traits = data.personality_traits; if (data.speaking_style !== undefined) updatePayload.speaking_style = data.speaking_style; if (data.rules !== undefined) updatePayload.rules = data.rules; if (data.scenario !== undefined) updatePayload.scenario = data.scenario; if (data.greeting_message !== undefined) updatePayload.greeting_message = data.greeting_message; if (data.visibility !== undefined) updatePayload.visibility = data.visibility; if (data.is_nsfw !== undefined) updatePayload.is_nsfw = data.is_nsfw; if (data.status !== undefined) updatePayload.status = data.status; if (data.likes_count !== undefined) updatePayload.likes_count = data.likes_count; if (data.chats_count !== undefined) updatePayload.chats_count = data.chats_count; updatePayload.updatedById = currentUser.id; await characters.update(updatePayload, {transaction}); if (data.owner !== undefined) { await characters.setOwner( data.owner, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.characters.getTableName(), belongsToColumn: 'avatar', belongsToId: characters.id, }, data.avatar, options, ); return characters; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const characters = await db.characters.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of characters) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of characters) { await record.destroy({transaction}); } }); return characters; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const characters = await db.characters.findByPk(id, options); await characters.update({ deletedBy: currentUser.id }, { transaction, }); await characters.destroy({ transaction }); return characters; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const characters = await db.characters.findOne( { where }, { transaction }, ); if (!characters) { return characters; } const output = characters.get({plain: true}); output.conversations_character = await characters.getConversations_character({ transaction }); output.character_ratings_character = await characters.getCharacter_ratings_character({ transaction }); output.character_tag_links_character = await characters.getCharacter_tag_links_character({ transaction }); output.owner = await characters.getOwner({ transaction }); output.avatar = await characters.getAvatar({ 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: 'owner', where: filter.owner ? { [Op.or]: [ { id: { [Op.in]: filter.owner.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.owner.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'avatar', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'name', filter.name, ), }; } if (filter.tagline) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'tagline', filter.tagline, ), }; } if (filter.bio) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'bio', filter.bio, ), }; } if (filter.personality_traits) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'personality_traits', filter.personality_traits, ), }; } if (filter.speaking_style) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'speaking_style', filter.speaking_style, ), }; } if (filter.rules) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'rules', filter.rules, ), }; } if (filter.scenario) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'scenario', filter.scenario, ), }; } if (filter.greeting_message) { where = { ...where, [Op.and]: Utils.ilike( 'characters', 'greeting_message', filter.greeting_message, ), }; } if (filter.likes_countRange) { const [start, end] = filter.likes_countRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, likes_count: { ...where.likes_count, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, likes_count: { ...where.likes_count, [Op.lte]: end, }, }; } } if (filter.chats_countRange) { const [start, end] = filter.chats_countRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, chats_count: { ...where.chats_count, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, chats_count: { ...where.chats_count, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.visibility) { where = { ...where, visibility: filter.visibility, }; } if (filter.is_nsfw) { where = { ...where, is_nsfw: filter.is_nsfw, }; } 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.characters.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( 'characters', 'name', query, ), ], }; } const records = await db.characters.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, })); } };