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 Visit_coveragesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const visit_coverages = await db.visit_coverages.create( { id: data.id || undefined, coverage_type: data.coverage_type || null , insurance_plan_note: data.insurance_plan_note || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await visit_coverages.setVisit( data.visit || null, { transaction, }); await visit_coverages.setInsurance_provider( data.insurance_provider || null, { transaction, }); return visit_coverages; } 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 visit_coveragesData = data.map((item, index) => ({ id: item.id || undefined, coverage_type: item.coverage_type || null , insurance_plan_note: item.insurance_plan_note || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const visit_coverages = await db.visit_coverages.bulkCreate(visit_coveragesData, { transaction }); // For each item created, replace relation files return visit_coverages; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const visit_coverages = await db.visit_coverages.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.coverage_type !== undefined) updatePayload.coverage_type = data.coverage_type; if (data.insurance_plan_note !== undefined) updatePayload.insurance_plan_note = data.insurance_plan_note; updatePayload.updatedById = currentUser.id; await visit_coverages.update(updatePayload, {transaction}); if (data.visit !== undefined) { await visit_coverages.setVisit( data.visit, { transaction } ); } if (data.insurance_provider !== undefined) { await visit_coverages.setInsurance_provider( data.insurance_provider, { transaction } ); } return visit_coverages; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const visit_coverages = await db.visit_coverages.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of visit_coverages) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of visit_coverages) { await record.destroy({transaction}); } }); return visit_coverages; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const visit_coverages = await db.visit_coverages.findByPk(id, options); await visit_coverages.update({ deletedBy: currentUser.id }, { transaction, }); await visit_coverages.destroy({ transaction }); return visit_coverages; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const visit_coverages = await db.visit_coverages.findOne( { where }, { transaction }, ); if (!visit_coverages) { return visit_coverages; } const output = visit_coverages.get({plain: true}); output.visit = await visit_coverages.getVisit({ transaction }); output.insurance_provider = await visit_coverages.getInsurance_provider({ 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.visits, as: 'visit', where: filter.visit ? { [Op.or]: [ { id: { [Op.in]: filter.visit.split('|').map(term => Utils.uuid(term)) } }, { visit_number: { [Op.or]: filter.visit.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.insurance_providers, as: 'insurance_provider', where: filter.insurance_provider ? { [Op.or]: [ { id: { [Op.in]: filter.insurance_provider.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.insurance_provider.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.insurance_plan_note) { where = { ...where, [Op.and]: Utils.ilike( 'visit_coverages', 'insurance_plan_note', filter.insurance_plan_note, ), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.coverage_type) { where = { ...where, coverage_type: filter.coverage_type, }; } 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.visit_coverages.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( 'visit_coverages', 'insurance_plan_note', query, ), ], }; } const records = await db.visit_coverages.findAll({ attributes: [ 'id', 'insurance_plan_note' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['insurance_plan_note', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.insurance_plan_note, })); } };