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 Archival_itemsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const archival_items = await db.archival_items.create( { id: data.id || undefined, label: data.label || null, description: data.description || null, published: data.published || false, period_from: data.period_from || null, period_to: data.period_to || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await archival_items.setTags(data.tags || [], { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.archival_items.getTableName(), belongsToColumn: 'media', belongsToId: archival_items.id, }, data.media, options, ); return archival_items; } 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 archival_itemsData = data.map((item, index) => ({ id: item.id || undefined, label: item.label || null, description: item.description || null, published: item.published || false, period_from: item.period_from || null, period_to: item.period_to || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const archival_items = await db.archival_items.bulkCreate( archival_itemsData, { transaction }, ); // For each item created, replace relation files for (let i = 0; i < archival_items.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.archival_items.getTableName(), belongsToColumn: 'media', belongsToId: archival_items[i].id, }, data[i].media, options, ); } return archival_items; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const archival_items = await db.archival_items.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.label !== undefined) updatePayload.label = data.label; if (data.description !== undefined) updatePayload.description = data.description; if (data.published !== undefined) updatePayload.published = data.published; if (data.period_from !== undefined) updatePayload.period_from = data.period_from; if (data.period_to !== undefined) updatePayload.period_to = data.period_to; updatePayload.updatedById = currentUser.id; await archival_items.update(updatePayload, { transaction }); if (data.tags !== undefined) { await archival_items.setTags(data.tags, { transaction }); } await FileDBApi.replaceRelationFiles( { belongsTo: db.archival_items.getTableName(), belongsToColumn: 'media', belongsToId: archival_items.id, }, data.media, options, ); return archival_items; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const archival_items = await db.archival_items.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of archival_items) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of archival_items) { await record.destroy({ transaction }); } }); return archival_items; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const archival_items = await db.archival_items.findByPk(id, options); await archival_items.update( { deletedBy: currentUser.id, }, { transaction, }, ); await archival_items.destroy({ transaction, }); return archival_items; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const archival_items = await db.archival_items.findOne( { where }, { transaction }, ); if (!archival_items) { return archival_items; } const output = archival_items.get({ plain: true }); output.media = await archival_items.getMedia({ transaction, }); output.tags = await archival_items.getTags({ 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.tags, as: 'tags', required: false, }, { model: db.file, as: 'media', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.label) { where = { ...where, [Op.and]: Utils.ilike('archival_items', 'label', filter.label), }; } if (filter.description) { where = { ...where, [Op.and]: Utils.ilike( 'archival_items', 'description', filter.description, ), }; } if (filter.calendarStart && filter.calendarEnd) { where = { ...where, [Op.or]: [ { period_from: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, { period_to: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, ], }; } if (filter.period_fromRange) { const [start, end] = filter.period_fromRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, period_from: { ...where.period_from, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, period_from: { ...where.period_from, [Op.lte]: end, }, }; } } if (filter.period_toRange) { const [start, end] = filter.period_toRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, period_to: { ...where.period_to, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, period_to: { ...where.period_to, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.published) { where = { ...where, published: filter.published, }; } if (filter.tags) { const searchTerms = filter.tags.split('|'); include = [ { model: db.tags, as: 'tags_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } 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.archival_items.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('archival_items', 'label', query), ], }; } const records = await db.archival_items.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, })); } };