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 Generation_outputsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const generation_outputs = await db.generation_outputs.create( { id: data.id || undefined, output_type: data.output_type || null , label: data.label || null , download_url: data.download_url || null , file_size_mb: data.file_size_mb || null , download_status: data.download_status || null , download_attempts: data.download_attempts || null , last_download_attempt_at: data.last_download_attempt_at || null , last_download_error: data.last_download_error || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await generation_outputs.setGeneration( data.generation || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.generation_outputs.getTableName(), belongsToColumn: 'file', belongsToId: generation_outputs.id, }, data.file, options, ); return generation_outputs; } 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 generation_outputsData = data.map((item, index) => ({ id: item.id || undefined, output_type: item.output_type || null , label: item.label || null , download_url: item.download_url || null , file_size_mb: item.file_size_mb || null , download_status: item.download_status || null , download_attempts: item.download_attempts || null , last_download_attempt_at: item.last_download_attempt_at || null , last_download_error: item.last_download_error || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const generation_outputs = await db.generation_outputs.bulkCreate(generation_outputsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < generation_outputs.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.generation_outputs.getTableName(), belongsToColumn: 'file', belongsToId: generation_outputs[i].id, }, data[i].file, options, ); } return generation_outputs; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const generation_outputs = await db.generation_outputs.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.output_type !== undefined) updatePayload.output_type = data.output_type; if (data.label !== undefined) updatePayload.label = data.label; if (data.download_url !== undefined) updatePayload.download_url = data.download_url; if (data.file_size_mb !== undefined) updatePayload.file_size_mb = data.file_size_mb; if (data.download_status !== undefined) updatePayload.download_status = data.download_status; if (data.download_attempts !== undefined) updatePayload.download_attempts = data.download_attempts; if (data.last_download_attempt_at !== undefined) updatePayload.last_download_attempt_at = data.last_download_attempt_at; if (data.last_download_error !== undefined) updatePayload.last_download_error = data.last_download_error; updatePayload.updatedById = currentUser.id; await generation_outputs.update(updatePayload, {transaction}); if (data.generation !== undefined) { await generation_outputs.setGeneration( data.generation, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.generation_outputs.getTableName(), belongsToColumn: 'file', belongsToId: generation_outputs.id, }, data.file, options, ); return generation_outputs; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const generation_outputs = await db.generation_outputs.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of generation_outputs) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of generation_outputs) { await record.destroy({transaction}); } }); return generation_outputs; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const generation_outputs = await db.generation_outputs.findByPk(id, options); await generation_outputs.update({ deletedBy: currentUser.id }, { transaction, }); await generation_outputs.destroy({ transaction }); return generation_outputs; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const generation_outputs = await db.generation_outputs.findOne( { where }, { transaction }, ); if (!generation_outputs) { return generation_outputs; } const output = generation_outputs.get({plain: true}); output.generation = await generation_outputs.getGeneration({ transaction }); output.file = await generation_outputs.getFile({ 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.video_generations, as: 'generation', where: filter.generation ? { [Op.or]: [ { id: { [Op.in]: filter.generation.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.generation.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'file', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.label) { where = { ...where, [Op.and]: Utils.ilike( 'generation_outputs', 'label', filter.label, ), }; } if (filter.download_url) { where = { ...where, [Op.and]: Utils.ilike( 'generation_outputs', 'download_url', filter.download_url, ), }; } if (filter.last_download_error) { where = { ...where, [Op.and]: Utils.ilike( 'generation_outputs', 'last_download_error', filter.last_download_error, ), }; } if (filter.file_size_mbRange) { const [start, end] = filter.file_size_mbRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, file_size_mb: { ...where.file_size_mb, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, file_size_mb: { ...where.file_size_mb, [Op.lte]: end, }, }; } } if (filter.download_attemptsRange) { const [start, end] = filter.download_attemptsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, download_attempts: { ...where.download_attempts, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, download_attempts: { ...where.download_attempts, [Op.lte]: end, }, }; } } if (filter.last_download_attempt_atRange) { const [start, end] = filter.last_download_attempt_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, last_download_attempt_at: { ...where.last_download_attempt_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, last_download_attempt_at: { ...where.last_download_attempt_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.output_type) { where = { ...where, output_type: filter.output_type, }; } if (filter.download_status) { where = { ...where, download_status: filter.download_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.generation_outputs.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( 'generation_outputs', 'label', query, ), ], }; } const records = await db.generation_outputs.findAll({ attributes: [ 'id', 'label' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['label', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.label, })); } };