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 BooksDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const books = await db.books.create( { id: data.id || undefined, title: data.title || null , genre: data.genre || null , tone: data.tone || null , pace: data.pace || null , language: data.language || null , target_pages: data.target_pages || null , status: data.status || null , generation_started_at: data.generation_started_at || null , generation_finished_at: data.generation_finished_at || null , progress_percent: data.progress_percent || null , failure_reason: data.failure_reason || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await books.setOwner( data.owner || null, { transaction, }); await books.setStorage_location( data.storage_location || null, { transaction, }); return books; } 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 booksData = data.map((item, index) => ({ id: item.id || undefined, title: item.title || null , genre: item.genre || null , tone: item.tone || null , pace: item.pace || null , language: item.language || null , target_pages: item.target_pages || null , status: item.status || null , generation_started_at: item.generation_started_at || null , generation_finished_at: item.generation_finished_at || null , progress_percent: item.progress_percent || null , failure_reason: item.failure_reason || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const books = await db.books.bulkCreate(booksData, { transaction }); // For each item created, replace relation files return books; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const books = await db.books.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.title !== undefined) updatePayload.title = data.title; if (data.genre !== undefined) updatePayload.genre = data.genre; if (data.tone !== undefined) updatePayload.tone = data.tone; if (data.pace !== undefined) updatePayload.pace = data.pace; if (data.language !== undefined) updatePayload.language = data.language; if (data.target_pages !== undefined) updatePayload.target_pages = data.target_pages; if (data.status !== undefined) updatePayload.status = data.status; if (data.generation_started_at !== undefined) updatePayload.generation_started_at = data.generation_started_at; if (data.generation_finished_at !== undefined) updatePayload.generation_finished_at = data.generation_finished_at; if (data.progress_percent !== undefined) updatePayload.progress_percent = data.progress_percent; if (data.failure_reason !== undefined) updatePayload.failure_reason = data.failure_reason; updatePayload.updatedById = currentUser.id; await books.update(updatePayload, {transaction}); if (data.owner !== undefined) { await books.setOwner( data.owner, { transaction } ); } if (data.storage_location !== undefined) { await books.setStorage_location( data.storage_location, { transaction } ); } return books; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const books = await db.books.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of books) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of books) { await record.destroy({transaction}); } }); return books; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const books = await db.books.findByPk(id, options); await books.update({ deletedBy: currentUser.id }, { transaction, }); await books.destroy({ transaction }); return books; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const books = await db.books.findOne( { where }, { transaction }, ); if (!books) { return books; } const output = books.get({plain: true}); output.book_prompts_book = await books.getBook_prompts_book({ transaction }); output.generation_jobs_book = await books.getGeneration_jobs_book({ transaction }); output.pdf_files_book = await books.getPdf_files_book({ transaction }); output.download_events_book = await books.getDownload_events_book({ transaction }); output.owner = await books.getOwner({ transaction }); output.storage_location = await books.getStorage_location({ 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.storage_locations, as: 'storage_location', where: filter.storage_location ? { [Op.or]: [ { id: { [Op.in]: filter.storage_location.split('|').map(term => Utils.uuid(term)) } }, { display_name: { [Op.or]: filter.storage_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.title) { where = { ...where, [Op.and]: Utils.ilike( 'books', 'title', filter.title, ), }; } if (filter.failure_reason) { where = { ...where, [Op.and]: Utils.ilike( 'books', 'failure_reason', filter.failure_reason, ), }; } if (filter.target_pagesRange) { const [start, end] = filter.target_pagesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, target_pages: { ...where.target_pages, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, target_pages: { ...where.target_pages, [Op.lte]: end, }, }; } } if (filter.generation_started_atRange) { const [start, end] = filter.generation_started_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, generation_started_at: { ...where.generation_started_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, generation_started_at: { ...where.generation_started_at, [Op.lte]: end, }, }; } } if (filter.generation_finished_atRange) { const [start, end] = filter.generation_finished_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, generation_finished_at: { ...where.generation_finished_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, generation_finished_at: { ...where.generation_finished_at, [Op.lte]: end, }, }; } } if (filter.progress_percentRange) { const [start, end] = filter.progress_percentRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, progress_percent: { ...where.progress_percent, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, progress_percent: { ...where.progress_percent, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.genre) { where = { ...where, genre: filter.genre, }; } if (filter.tone) { where = { ...where, tone: filter.tone, }; } if (filter.pace) { where = { ...where, pace: filter.pace, }; } if (filter.language) { where = { ...where, language: filter.language, }; } 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.books.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( 'books', 'title', query, ), ], }; } const records = await db.books.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, })); } };