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 Email_trackingsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const email_trackings = await db.email_trackings.create( { id: data.id || undefined, opened: data.opened || false, clicked: data.clicked || false, replied: data.replied || false, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await email_trackings.setEmail_campaign(data.email_campaign || null, { transaction, }); await email_trackings.setContact(data.contact || null, { transaction, }); return email_trackings; } 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 email_trackingsData = data.map((item, index) => ({ id: item.id || undefined, opened: item.opened || false, clicked: item.clicked || false, replied: item.replied || false, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const email_trackings = await db.email_trackings.bulkCreate( email_trackingsData, { transaction }, ); // For each item created, replace relation files return email_trackings; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const email_trackings = await db.email_trackings.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.opened !== undefined) updatePayload.opened = data.opened; if (data.clicked !== undefined) updatePayload.clicked = data.clicked; if (data.replied !== undefined) updatePayload.replied = data.replied; updatePayload.updatedById = currentUser.id; await email_trackings.update(updatePayload, { transaction }); if (data.email_campaign !== undefined) { await email_trackings.setEmail_campaign( data.email_campaign, { transaction }, ); } if (data.contact !== undefined) { await email_trackings.setContact( data.contact, { transaction }, ); } return email_trackings; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const email_trackings = await db.email_trackings.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of email_trackings) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of email_trackings) { await record.destroy({ transaction }); } }); return email_trackings; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const email_trackings = await db.email_trackings.findByPk(id, options); await email_trackings.update( { deletedBy: currentUser.id, }, { transaction, }, ); await email_trackings.destroy({ transaction, }); return email_trackings; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const email_trackings = await db.email_trackings.findOne( { where }, { transaction }, ); if (!email_trackings) { return email_trackings; } const output = email_trackings.get({ plain: true }); output.email_campaign = await email_trackings.getEmail_campaign({ transaction, }); output.contact = await email_trackings.getContact({ 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.email_campaigns, as: 'email_campaign', where: filter.email_campaign ? { [Op.or]: [ { id: { [Op.in]: filter.email_campaign .split('|') .map((term) => Utils.uuid(term)), }, }, { subject: { [Op.or]: filter.email_campaign .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.contacts, as: 'contact', where: filter.contact ? { [Op.or]: [ { id: { [Op.in]: filter.contact .split('|') .map((term) => Utils.uuid(term)), }, }, { email: { [Op.or]: filter.contact .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.opened) { where = { ...where, opened: filter.opened, }; } if (filter.clicked) { where = { ...where, clicked: filter.clicked, }; } if (filter.replied) { where = { ...where, replied: filter.replied, }; } 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.email_trackings.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('email_trackings', 'email_campaign', query), ], }; } const records = await db.email_trackings.findAll({ attributes: ['id', 'email_campaign'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['email_campaign', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.email_campaign, })); } };