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 Contact_submissionsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const contact_submissions = await db.contact_submissions.create( { id: data.id || undefined, name: data.name || null , email: data.email || null , phone: data.phone || null , message: data.message || null , status: data.status || null , submitted_at: data.submitted_at || null , resolved_at: data.resolved_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await contact_submissions.setLanding_page( data.landing_page || null, { transaction, }); return contact_submissions; } 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 contact_submissionsData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null , email: item.email || null , phone: item.phone || null , message: item.message || null , status: item.status || null , submitted_at: item.submitted_at || null , resolved_at: item.resolved_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const contact_submissions = await db.contact_submissions.bulkCreate(contact_submissionsData, { transaction }); // For each item created, replace relation files return contact_submissions; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const contact_submissions = await db.contact_submissions.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.email !== undefined) updatePayload.email = data.email; if (data.phone !== undefined) updatePayload.phone = data.phone; if (data.message !== undefined) updatePayload.message = data.message; if (data.status !== undefined) updatePayload.status = data.status; if (data.submitted_at !== undefined) updatePayload.submitted_at = data.submitted_at; if (data.resolved_at !== undefined) updatePayload.resolved_at = data.resolved_at; updatePayload.updatedById = currentUser.id; await contact_submissions.update(updatePayload, {transaction}); if (data.landing_page !== undefined) { await contact_submissions.setLanding_page( data.landing_page, { transaction } ); } return contact_submissions; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const contact_submissions = await db.contact_submissions.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of contact_submissions) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of contact_submissions) { await record.destroy({transaction}); } }); return contact_submissions; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const contact_submissions = await db.contact_submissions.findByPk(id, options); await contact_submissions.update({ deletedBy: currentUser.id }, { transaction, }); await contact_submissions.destroy({ transaction }); return contact_submissions; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const contact_submissions = await db.contact_submissions.findOne( { where }, { transaction }, ); if (!contact_submissions) { return contact_submissions; } const output = contact_submissions.get({plain: true}); output.landing_page = await contact_submissions.getLanding_page({ 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.landing_pages, as: 'landing_page', where: filter.landing_page ? { [Op.or]: [ { id: { [Op.in]: filter.landing_page.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.landing_page.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike( 'contact_submissions', 'name', filter.name, ), }; } if (filter.email) { where = { ...where, [Op.and]: Utils.ilike( 'contact_submissions', 'email', filter.email, ), }; } if (filter.phone) { where = { ...where, [Op.and]: Utils.ilike( 'contact_submissions', 'phone', filter.phone, ), }; } if (filter.message) { where = { ...where, [Op.and]: Utils.ilike( 'contact_submissions', 'message', filter.message, ), }; } if (filter.calendarStart && filter.calendarEnd) { where = { ...where, [Op.or]: [ { submitted_at: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, { resolved_at: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, ], }; } if (filter.submitted_atRange) { const [start, end] = filter.submitted_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, submitted_at: { ...where.submitted_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, submitted_at: { ...where.submitted_at, [Op.lte]: end, }, }; } } if (filter.resolved_atRange) { const [start, end] = filter.resolved_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, resolved_at: { ...where.resolved_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, resolved_at: { ...where.resolved_at, [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.contact_submissions.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( 'contact_submissions', 'email', query, ), ], }; } const records = await db.contact_submissions.findAll({ attributes: [ 'id', 'email' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['email', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.email, })); } };