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 Lead_matchesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const lead_matches = await db.lead_matches.create( { id: data.id || undefined, match_score: data.match_score || null , status: data.status || null , sent_at: data.sent_at || null , viewed_at: data.viewed_at || null , responded_at: data.responded_at || null , scheduled_at: data.scheduled_at || null , completed_at: data.completed_at || null , declined_at: data.declined_at || null , created_at_ts: data.created_at_ts || null , updated_at_ts: data.updated_at_ts || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await lead_matches.setLead( data.lead || null, { transaction, }); await lead_matches.setBusiness( data.business || null, { transaction, }); return lead_matches; } 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 lead_matchesData = data.map((item, index) => ({ id: item.id || undefined, match_score: item.match_score || null , status: item.status || null , sent_at: item.sent_at || null , viewed_at: item.viewed_at || null , responded_at: item.responded_at || null , scheduled_at: item.scheduled_at || null , completed_at: item.completed_at || null , declined_at: item.declined_at || null , created_at_ts: item.created_at_ts || null , updated_at_ts: item.updated_at_ts || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const lead_matches = await db.lead_matches.bulkCreate(lead_matchesData, { transaction }); // For each item created, replace relation files return lead_matches; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const lead_matches = await db.lead_matches.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.match_score !== undefined) updatePayload.match_score = data.match_score; if (data.status !== undefined) updatePayload.status = data.status; if (data.sent_at !== undefined) updatePayload.sent_at = data.sent_at; if (data.viewed_at !== undefined) updatePayload.viewed_at = data.viewed_at; if (data.responded_at !== undefined) updatePayload.responded_at = data.responded_at; if (data.scheduled_at !== undefined) updatePayload.scheduled_at = data.scheduled_at; if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at; if (data.declined_at !== undefined) updatePayload.declined_at = data.declined_at; if (data.created_at_ts !== undefined) updatePayload.created_at_ts = data.created_at_ts; if (data.updated_at_ts !== undefined) updatePayload.updated_at_ts = data.updated_at_ts; updatePayload.updatedById = currentUser.id; await lead_matches.update(updatePayload, {transaction}); if (data.lead !== undefined) { await lead_matches.setLead( data.lead, { transaction } ); } if (data.business !== undefined) { await lead_matches.setBusiness( data.business, { transaction } ); } return lead_matches; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const lead_matches = await db.lead_matches.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of lead_matches) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of lead_matches) { await record.destroy({transaction}); } }); return lead_matches; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const lead_matches = await db.lead_matches.findByPk(id, options); await lead_matches.update({ deletedBy: currentUser.id }, { transaction, }); await lead_matches.destroy({ transaction }); return lead_matches; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const lead_matches = await db.lead_matches.findOne( { where }, { transaction }, ); if (!lead_matches) { return lead_matches; } const output = lead_matches.get({plain: true}); output.lead = await lead_matches.getLead({ transaction }); output.business = await lead_matches.getBusiness({ 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.leads, as: 'lead', where: filter.lead ? { [Op.or]: [ { id: { [Op.in]: filter.lead.split('|').map(term => Utils.uuid(term)) } }, { keyword: { [Op.or]: filter.lead.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.businesses, as: 'business', where: filter.business ? { [Op.or]: [ { id: { [Op.in]: filter.business.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.business.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.match_scoreRange) { const [start, end] = filter.match_scoreRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, match_score: { ...where.match_score, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, match_score: { ...where.match_score, [Op.lte]: end, }, }; } } if (filter.sent_atRange) { const [start, end] = filter.sent_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, sent_at: { ...where.sent_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, sent_at: { ...where.sent_at, [Op.lte]: end, }, }; } } if (filter.viewed_atRange) { const [start, end] = filter.viewed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, viewed_at: { ...where.viewed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, viewed_at: { ...where.viewed_at, [Op.lte]: end, }, }; } } if (filter.responded_atRange) { const [start, end] = filter.responded_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, responded_at: { ...where.responded_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, responded_at: { ...where.responded_at, [Op.lte]: end, }, }; } } if (filter.scheduled_atRange) { const [start, end] = filter.scheduled_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, scheduled_at: { ...where.scheduled_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, scheduled_at: { ...where.scheduled_at, [Op.lte]: end, }, }; } } if (filter.completed_atRange) { const [start, end] = filter.completed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, completed_at: { ...where.completed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, completed_at: { ...where.completed_at, [Op.lte]: end, }, }; } } if (filter.declined_atRange) { const [start, end] = filter.declined_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, declined_at: { ...where.declined_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, declined_at: { ...where.declined_at, [Op.lte]: end, }, }; } } if (filter.created_at_tsRange) { const [start, end] = filter.created_at_tsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, created_at_ts: { ...where.created_at_ts, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, created_at_ts: { ...where.created_at_ts, [Op.lte]: end, }, }; } } if (filter.updated_at_tsRange) { const [start, end] = filter.updated_at_tsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, updated_at_ts: { ...where.updated_at_ts, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, updated_at_ts: { ...where.updated_at_ts, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } 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.lead_matches.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( 'lead_matches', 'status', query, ), ], }; } const records = await db.lead_matches.findAll({ attributes: [ 'id', 'status' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['status', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.status, })); } };