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 InsurancesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const insurances = await db.insurances.create( { id: data.id || undefined, payer_type: data.payer_type || null, aliases: data.aliases || null, payer_id: data.payer_id || null, notes: data.notes || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await insurances.setPractice(data.practice || null, { transaction, }); return insurances; } 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 insurancesData = data.map((item, index) => ({ id: item.id || undefined, payer_type: item.payer_type || null, aliases: item.aliases || null, payer_id: item.payer_id || null, notes: item.notes || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const insurances = await db.insurances.bulkCreate(insurancesData, { transaction, }); // For each item created, replace relation files return insurances; } 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 insurances = await db.insurances.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.payer_type !== undefined) updatePayload.payer_type = data.payer_type; if (data.aliases !== undefined) updatePayload.aliases = data.aliases; if (data.payer_id !== undefined) updatePayload.payer_id = data.payer_id; if (data.notes !== undefined) updatePayload.notes = data.notes; updatePayload.updatedById = currentUser.id; await insurances.update(updatePayload, { transaction }); if (data.practice !== undefined) { await insurances.setPractice( data.practice, { transaction }, ); } return insurances; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const insurances = await db.insurances.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of insurances) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of insurances) { await record.destroy({ transaction }); } }); return insurances; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const insurances = await db.insurances.findByPk(id, options); await insurances.update( { deletedBy: currentUser.id, }, { transaction, }, ); await insurances.destroy({ transaction, }); return insurances; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const insurances = await db.insurances.findOne({ where }, { transaction }); if (!insurances) { return insurances; } const output = insurances.get({ plain: true }); output.practice = await insurances.getPractice({ 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 userPractice = (user && user.practice?.id) || null; if (userPractice) { if (options?.currentUser?.practiceId) { where.practiceId = options.currentUser.practiceId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.practice, as: 'practice', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.aliases) { where = { ...where, [Op.and]: Utils.ilike('insurances', 'aliases', filter.aliases), }; } if (filter.payer_id) { where = { ...where, [Op.and]: Utils.ilike('insurances', 'payer_id', filter.payer_id), }; } if (filter.notes) { where = { ...where, [Op.and]: Utils.ilike('insurances', 'notes', filter.notes), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.payer_type) { where = { ...where, payer_type: filter.payer_type, }; } if (filter.practice) { const listItems = filter.practice.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, practiceId: { [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.practiceId; } 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.insurances.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('insurances', 'payer_id', query), ], }; } const records = await db.insurances.findAll({ attributes: ['id', 'payer_id'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['payer_id', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.payer_id, })); } };