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 AgenciesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const agencies = await db.agencies.create( { id: data.id || undefined, agency_name: data.agency_name || null, agency_type: data.agency_type || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); return agencies; } 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 agenciesData = data.map((item, index) => ({ id: item.id || undefined, agency_name: item.agency_name || null, agency_type: item.agency_type || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const agencies = await db.agencies.bulkCreate(agenciesData, { transaction, }); // For each item created, replace relation files return agencies; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const agencies = await db.agencies.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.agency_name !== undefined) updatePayload.agency_name = data.agency_name; if (data.agency_type !== undefined) updatePayload.agency_type = data.agency_type; updatePayload.updatedById = currentUser.id; await agencies.update(updatePayload, { transaction }); return agencies; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const agencies = await db.agencies.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of agencies) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of agencies) { await record.destroy({ transaction }); } }); return agencies; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const agencies = await db.agencies.findByPk(id, options); await agencies.update( { deletedBy: currentUser.id, }, { transaction, }, ); await agencies.destroy({ transaction, }); return agencies; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const agencies = await db.agencies.findOne({ where }, { transaction }); if (!agencies) { return agencies; } const output = agencies.get({ plain: true }); output.members_agency = await agencies.getMembers_agency({ 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 = []; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.agency_name) { where = { ...where, [Op.and]: Utils.ilike('agencies', 'agency_name', filter.agency_name), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.agency_type) { where = { ...where, agency_type: filter.agency_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.agencies.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('agencies', 'agency_name', query), ], }; } const records = await db.agencies.findAll({ attributes: ['id', 'agency_name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['agency_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.agency_name, })); } };