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 DatasetsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const datasets = await db.datasets.create( { id: data.id || undefined, name: data.name || null , dataset_type: data.dataset_type || null , license_type: data.license_type || null , source_url: data.source_url || null , description: data.description || null , hash_manifest: data.hash_manifest || null , imported_at: data.imported_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await datasets.setOrganizations( data.organizations || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.datasets.getTableName(), belongsToColumn: 'dataset_archive', belongsToId: datasets.id, }, data.dataset_archive, options, ); return datasets; } 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 datasetsData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null , dataset_type: item.dataset_type || null , license_type: item.license_type || null , source_url: item.source_url || null , description: item.description || null , hash_manifest: item.hash_manifest || null , imported_at: item.imported_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const datasets = await db.datasets.bulkCreate(datasetsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < datasets.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.datasets.getTableName(), belongsToColumn: 'dataset_archive', belongsToId: datasets[i].id, }, data[i].dataset_archive, options, ); } return datasets; } 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 datasets = await db.datasets.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.dataset_type !== undefined) updatePayload.dataset_type = data.dataset_type; if (data.license_type !== undefined) updatePayload.license_type = data.license_type; if (data.source_url !== undefined) updatePayload.source_url = data.source_url; if (data.description !== undefined) updatePayload.description = data.description; if (data.hash_manifest !== undefined) updatePayload.hash_manifest = data.hash_manifest; if (data.imported_at !== undefined) updatePayload.imported_at = data.imported_at; updatePayload.updatedById = currentUser.id; await datasets.update(updatePayload, {transaction}); if (data.organizations !== undefined) { await datasets.setOrganizations( data.organizations, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.datasets.getTableName(), belongsToColumn: 'dataset_archive', belongsToId: datasets.id, }, data.dataset_archive, options, ); return datasets; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const datasets = await db.datasets.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of datasets) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of datasets) { await record.destroy({transaction}); } }); return datasets; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const datasets = await db.datasets.findByPk(id, options); await datasets.update({ deletedBy: currentUser.id }, { transaction, }); await datasets.destroy({ transaction }); return datasets; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const datasets = await db.datasets.findOne( { where }, { transaction }, ); if (!datasets) { return datasets; } const output = datasets.get({plain: true}); output.dataset_archive = await datasets.getDataset_archive({ transaction }); output.organizations = await datasets.getOrganizations({ 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 userOrganizations = (user && user.organizations?.id) || null; if (userOrganizations) { if (options?.currentUser?.organizationsId) { where.organizationsId = options.currentUser.organizationsId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.organizations, as: 'organizations', }, { model: db.file, as: 'dataset_archive', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike( 'datasets', 'name', filter.name, ), }; } if (filter.source_url) { where = { ...where, [Op.and]: Utils.ilike( 'datasets', 'source_url', filter.source_url, ), }; } if (filter.description) { where = { ...where, [Op.and]: Utils.ilike( 'datasets', 'description', filter.description, ), }; } if (filter.hash_manifest) { where = { ...where, [Op.and]: Utils.ilike( 'datasets', 'hash_manifest', filter.hash_manifest, ), }; } if (filter.imported_atRange) { const [start, end] = filter.imported_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, imported_at: { ...where.imported_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, imported_at: { ...where.imported_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.dataset_type) { where = { ...where, dataset_type: filter.dataset_type, }; } if (filter.license_type) { where = { ...where, license_type: filter.license_type, }; } if (filter.organizations) { const listItems = filter.organizations.split('|').map(item => { return Utils.uuid(item) }); where = { ...where, organizationsId: {[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.organizationsId; } 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.datasets.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( 'datasets', 'name', query, ), ], }; } const records = await db.datasets.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, })); } };