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 Case_artifactsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const case_artifacts = await db.case_artifacts.create( { id: data.id || undefined, artifact_type: data.artifact_type || null , title: data.title || null , content: data.content || null , source_citation: data.source_citation || null , is_public: data.is_public || false , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await case_artifacts.setCase_file( data.case_file || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.case_artifacts.getTableName(), belongsToColumn: 'attachments', belongsToId: case_artifacts.id, }, data.attachments, options, ); return case_artifacts; } 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 case_artifactsData = data.map((item, index) => ({ id: item.id || undefined, artifact_type: item.artifact_type || null , title: item.title || null , content: item.content || null , source_citation: item.source_citation || null , is_public: item.is_public || false , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const case_artifacts = await db.case_artifacts.bulkCreate(case_artifactsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < case_artifacts.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.case_artifacts.getTableName(), belongsToColumn: 'attachments', belongsToId: case_artifacts[i].id, }, data[i].attachments, options, ); } return case_artifacts; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const case_artifacts = await db.case_artifacts.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.artifact_type !== undefined) updatePayload.artifact_type = data.artifact_type; if (data.title !== undefined) updatePayload.title = data.title; if (data.content !== undefined) updatePayload.content = data.content; if (data.source_citation !== undefined) updatePayload.source_citation = data.source_citation; if (data.is_public !== undefined) updatePayload.is_public = data.is_public; updatePayload.updatedById = currentUser.id; await case_artifacts.update(updatePayload, {transaction}); if (data.case_file !== undefined) { await case_artifacts.setCase_file( data.case_file, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.case_artifacts.getTableName(), belongsToColumn: 'attachments', belongsToId: case_artifacts.id, }, data.attachments, options, ); return case_artifacts; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const case_artifacts = await db.case_artifacts.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of case_artifacts) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of case_artifacts) { await record.destroy({transaction}); } }); return case_artifacts; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const case_artifacts = await db.case_artifacts.findByPk(id, options); await case_artifacts.update({ deletedBy: currentUser.id }, { transaction, }); await case_artifacts.destroy({ transaction }); return case_artifacts; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const case_artifacts = await db.case_artifacts.findOne( { where }, { transaction }, ); if (!case_artifacts) { return case_artifacts; } const output = case_artifacts.get({plain: true}); output.case_file = await case_artifacts.getCase_file({ transaction }); output.attachments = await case_artifacts.getAttachments({ 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.case_files, as: 'case_file', where: filter.case_file ? { [Op.or]: [ { id: { [Op.in]: filter.case_file.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.case_file.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'attachments', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.title) { where = { ...where, [Op.and]: Utils.ilike( 'case_artifacts', 'title', filter.title, ), }; } if (filter.content) { where = { ...where, [Op.and]: Utils.ilike( 'case_artifacts', 'content', filter.content, ), }; } if (filter.source_citation) { where = { ...where, [Op.and]: Utils.ilike( 'case_artifacts', 'source_citation', filter.source_citation, ), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.artifact_type) { where = { ...where, artifact_type: filter.artifact_type, }; } if (filter.is_public) { where = { ...where, is_public: filter.is_public, }; } 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.case_artifacts.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( 'case_artifacts', 'title', query, ), ], }; } const records = await db.case_artifacts.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, })); } };