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 App_releasesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const app_releases = await db.app_releases.create( { id: data.id || undefined, version: data.version || null , channel: data.channel || null , released_at: data.released_at || null , sha256: data.sha256 || null , release_notes: data.release_notes || null , is_mandatory: data.is_mandatory || false , is_published: data.is_published || false , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await app_releases.setProduct( data.product || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.app_releases.getTableName(), belongsToColumn: 'installer_file', belongsToId: app_releases.id, }, data.installer_file, options, ); return app_releases; } 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 app_releasesData = data.map((item, index) => ({ id: item.id || undefined, version: item.version || null , channel: item.channel || null , released_at: item.released_at || null , sha256: item.sha256 || null , release_notes: item.release_notes || null , is_mandatory: item.is_mandatory || false , is_published: item.is_published || false , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const app_releases = await db.app_releases.bulkCreate(app_releasesData, { transaction }); // For each item created, replace relation files for (let i = 0; i < app_releases.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.app_releases.getTableName(), belongsToColumn: 'installer_file', belongsToId: app_releases[i].id, }, data[i].installer_file, options, ); } return app_releases; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const app_releases = await db.app_releases.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.version !== undefined) updatePayload.version = data.version; if (data.channel !== undefined) updatePayload.channel = data.channel; if (data.released_at !== undefined) updatePayload.released_at = data.released_at; if (data.sha256 !== undefined) updatePayload.sha256 = data.sha256; if (data.release_notes !== undefined) updatePayload.release_notes = data.release_notes; if (data.is_mandatory !== undefined) updatePayload.is_mandatory = data.is_mandatory; if (data.is_published !== undefined) updatePayload.is_published = data.is_published; updatePayload.updatedById = currentUser.id; await app_releases.update(updatePayload, {transaction}); if (data.product !== undefined) { await app_releases.setProduct( data.product, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.app_releases.getTableName(), belongsToColumn: 'installer_file', belongsToId: app_releases.id, }, data.installer_file, options, ); return app_releases; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const app_releases = await db.app_releases.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of app_releases) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of app_releases) { await record.destroy({transaction}); } }); return app_releases; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const app_releases = await db.app_releases.findByPk(id, options); await app_releases.update({ deletedBy: currentUser.id }, { transaction, }); await app_releases.destroy({ transaction }); return app_releases; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const app_releases = await db.app_releases.findOne( { where }, { transaction }, ); if (!app_releases) { return app_releases; } const output = app_releases.get({plain: true}); output.product = await app_releases.getProduct({ transaction }); output.installer_file = await app_releases.getInstaller_file({ 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.products, as: 'product', where: filter.product ? { [Op.or]: [ { id: { [Op.in]: filter.product.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.product.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'installer_file', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.version) { where = { ...where, [Op.and]: Utils.ilike( 'app_releases', 'version', filter.version, ), }; } if (filter.sha256) { where = { ...where, [Op.and]: Utils.ilike( 'app_releases', 'sha256', filter.sha256, ), }; } if (filter.release_notes) { where = { ...where, [Op.and]: Utils.ilike( 'app_releases', 'release_notes', filter.release_notes, ), }; } if (filter.released_atRange) { const [start, end] = filter.released_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, released_at: { ...where.released_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, released_at: { ...where.released_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.channel) { where = { ...where, channel: filter.channel, }; } if (filter.is_mandatory) { where = { ...where, is_mandatory: filter.is_mandatory, }; } if (filter.is_published) { where = { ...where, is_published: filter.is_published, }; } 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.app_releases.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( 'app_releases', 'version', query, ), ], }; } const records = await db.app_releases.findAll({ attributes: [ 'id', 'version' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['version', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.version, })); } };