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 LeadsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const leads = await db.leads.create( { id: data.id || undefined, full_name: data.full_name || null, email: data.email || null, phone_number: data.phone_number || null, request_date: data.request_date || null, service: data.service || null, surface: data.surface || null, etat_supports: data.etat_supports || null, finition: data.finition || null, estimation_prix: data.estimation_prix || null, statut: data.statut || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); return leads; } 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 leadsData = data.map((item, index) => ({ id: item.id || undefined, full_name: item.full_name || null, email: item.email || null, phone_number: item.phone_number || null, request_date: item.request_date || null, service: item.service || null, surface: item.surface || null, etat_supports: item.etat_supports || null, finition: item.finition || null, estimation_prix: item.estimation_prix || null, statut: item.statut || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const leads = await db.leads.bulkCreate(leadsData, { transaction }); // For each item created, replace relation files return leads; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const leads = await db.leads.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.full_name !== undefined) updatePayload.full_name = data.full_name; if (data.email !== undefined) updatePayload.email = data.email; if (data.phone_number !== undefined) updatePayload.phone_number = data.phone_number; if (data.request_date !== undefined) updatePayload.request_date = data.request_date; if (data.service !== undefined) updatePayload.service = data.service; if (data.surface !== undefined) updatePayload.surface = data.surface; if (data.etat_supports !== undefined) updatePayload.etat_supports = data.etat_supports; if (data.finition !== undefined) updatePayload.finition = data.finition; if (data.estimation_prix !== undefined) updatePayload.estimation_prix = data.estimation_prix; if (data.statut !== undefined) updatePayload.statut = data.statut; updatePayload.updatedById = currentUser.id; await leads.update(updatePayload, { transaction }); return leads; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const leads = await db.leads.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of leads) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of leads) { await record.destroy({ transaction }); } }); return leads; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const leads = await db.leads.findByPk(id, options); await leads.update( { deletedBy: currentUser.id, }, { transaction, }, ); await leads.destroy({ transaction, }); return leads; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const leads = await db.leads.findOne({ where }, { transaction }); if (!leads) { return leads; } const output = leads.get({ plain: true }); 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.full_name) { where = { ...where, [Op.and]: Utils.ilike('leads', 'full_name', filter.full_name), }; } if (filter.email) { where = { ...where, [Op.and]: Utils.ilike('leads', 'email', filter.email), }; } if (filter.phone_number) { where = { ...where, [Op.and]: Utils.ilike('leads', 'phone_number', filter.phone_number), }; } if (filter.request_dateRange) { const [start, end] = filter.request_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, request_date: { ...where.request_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, request_date: { ...where.request_date, [Op.lte]: end, }, }; } } if (filter.surfaceRange) { const [start, end] = filter.surfaceRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, surface: { ...where.surface, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, surface: { ...where.surface, [Op.lte]: end, }, }; } } if (filter.estimation_prixRange) { const [start, end] = filter.estimation_prixRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, estimation_prix: { ...where.estimation_prix, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, estimation_prix: { ...where.estimation_prix, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.service) { where = { ...where, service: filter.service, }; } if (filter.etat_supports) { where = { ...where, etat_supports: filter.etat_supports, }; } if (filter.finition) { where = { ...where, finition: filter.finition, }; } if (filter.statut) { where = { ...where, statut: filter.statut, }; } 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.leads.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('leads', 'full_name', query), ], }; } const records = await db.leads.findAll({ attributes: ['id', 'full_name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['full_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.full_name, })); } };