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 Imaging_ordersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const imaging_orders = await db.imaging_orders.create( { id: data.id || undefined, ordered_datetime: data.ordered_datetime || null , modality: data.modality || null , body_part: data.body_part || null , priority: data.priority || null , status: data.status || null , clinical_indication: data.clinical_indication || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await imaging_orders.setPatient( data.patient || null, { transaction, }); await imaging_orders.setTumor_case( data.tumor_case || null, { transaction, }); await imaging_orders.setVisit( data.visit || null, { transaction, }); await imaging_orders.setOrdering_user( data.ordering_user || null, { transaction, }); return imaging_orders; } 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 imaging_ordersData = data.map((item, index) => ({ id: item.id || undefined, ordered_datetime: item.ordered_datetime || null , modality: item.modality || null , body_part: item.body_part || null , priority: item.priority || null , status: item.status || null , clinical_indication: item.clinical_indication || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const imaging_orders = await db.imaging_orders.bulkCreate(imaging_ordersData, { transaction }); // For each item created, replace relation files return imaging_orders; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const imaging_orders = await db.imaging_orders.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.ordered_datetime !== undefined) updatePayload.ordered_datetime = data.ordered_datetime; if (data.modality !== undefined) updatePayload.modality = data.modality; if (data.body_part !== undefined) updatePayload.body_part = data.body_part; if (data.priority !== undefined) updatePayload.priority = data.priority; if (data.status !== undefined) updatePayload.status = data.status; if (data.clinical_indication !== undefined) updatePayload.clinical_indication = data.clinical_indication; updatePayload.updatedById = currentUser.id; await imaging_orders.update(updatePayload, {transaction}); if (data.patient !== undefined) { await imaging_orders.setPatient( data.patient, { transaction } ); } if (data.tumor_case !== undefined) { await imaging_orders.setTumor_case( data.tumor_case, { transaction } ); } if (data.visit !== undefined) { await imaging_orders.setVisit( data.visit, { transaction } ); } if (data.ordering_user !== undefined) { await imaging_orders.setOrdering_user( data.ordering_user, { transaction } ); } return imaging_orders; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const imaging_orders = await db.imaging_orders.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of imaging_orders) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of imaging_orders) { await record.destroy({transaction}); } }); return imaging_orders; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const imaging_orders = await db.imaging_orders.findByPk(id, options); await imaging_orders.update({ deletedBy: currentUser.id }, { transaction, }); await imaging_orders.destroy({ transaction }); return imaging_orders; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const imaging_orders = await db.imaging_orders.findOne( { where }, { transaction }, ); if (!imaging_orders) { return imaging_orders; } const output = imaging_orders.get({plain: true}); output.imaging_reports_imaging_order = await imaging_orders.getImaging_reports_imaging_order({ transaction }); output.patient = await imaging_orders.getPatient({ transaction }); output.tumor_case = await imaging_orders.getTumor_case({ transaction }); output.visit = await imaging_orders.getVisit({ transaction }); output.ordering_user = await imaging_orders.getOrdering_user({ 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.patients, as: 'patient', where: filter.patient ? { [Op.or]: [ { id: { [Op.in]: filter.patient.split('|').map(term => Utils.uuid(term)) } }, { full_name: { [Op.or]: filter.patient.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.tumor_cases, as: 'tumor_case', where: filter.tumor_case ? { [Op.or]: [ { id: { [Op.in]: filter.tumor_case.split('|').map(term => Utils.uuid(term)) } }, { case_number: { [Op.or]: filter.tumor_case.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.clinical_visits, as: 'visit', where: filter.visit ? { [Op.or]: [ { id: { [Op.in]: filter.visit.split('|').map(term => Utils.uuid(term)) } }, { visit_type: { [Op.or]: filter.visit.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.users, as: 'ordering_user', where: filter.ordering_user ? { [Op.or]: [ { id: { [Op.in]: filter.ordering_user.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.ordering_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.body_part) { where = { ...where, [Op.and]: Utils.ilike( 'imaging_orders', 'body_part', filter.body_part, ), }; } if (filter.clinical_indication) { where = { ...where, [Op.and]: Utils.ilike( 'imaging_orders', 'clinical_indication', filter.clinical_indication, ), }; } if (filter.ordered_datetimeRange) { const [start, end] = filter.ordered_datetimeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, ordered_datetime: { ...where.ordered_datetime, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, ordered_datetime: { ...where.ordered_datetime, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.modality) { where = { ...where, modality: filter.modality, }; } if (filter.priority) { where = { ...where, priority: filter.priority, }; } if (filter.status) { where = { ...where, status: filter.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.imaging_orders.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( 'imaging_orders', 'body_part', query, ), ], }; } const records = await db.imaging_orders.findAll({ attributes: [ 'id', 'body_part' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['body_part', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.body_part, })); } };