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