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 Points_of_interestDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const points_of_interest = await db.points_of_interest.create( { id: data.id || undefined, name: data.name || null, description: data.description || null, category: data.category || null, address: data.address || null, latitude: data.latitude || null, longitude: data.longitude || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await points_of_interest.setEmpresa(data.empresa || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.points_of_interest.getTableName(), belongsToColumn: 'images', belongsToId: points_of_interest.id, }, data.images, options, ); return points_of_interest; } 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 points_of_interestData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null, description: item.description || null, category: item.category || null, address: item.address || null, latitude: item.latitude || null, longitude: item.longitude || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const points_of_interest = await db.points_of_interest.bulkCreate( points_of_interestData, { transaction }, ); // For each item created, replace relation files for (let i = 0; i < points_of_interest.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.points_of_interest.getTableName(), belongsToColumn: 'images', belongsToId: points_of_interest[i].id, }, data[i].images, options, ); } return points_of_interest; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const globalAccess = currentUser.app_role?.globalAccess; const points_of_interest = await db.points_of_interest.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.description !== undefined) updatePayload.description = data.description; if (data.category !== undefined) updatePayload.category = data.category; if (data.address !== undefined) updatePayload.address = data.address; if (data.latitude !== undefined) updatePayload.latitude = data.latitude; if (data.longitude !== undefined) updatePayload.longitude = data.longitude; updatePayload.updatedById = currentUser.id; await points_of_interest.update(updatePayload, { transaction }); if (data.empresa !== undefined) { await points_of_interest.setEmpresa( data.empresa, { transaction }, ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.points_of_interest.getTableName(), belongsToColumn: 'images', belongsToId: points_of_interest.id, }, data.images, options, ); return points_of_interest; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const points_of_interest = await db.points_of_interest.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of points_of_interest) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of points_of_interest) { await record.destroy({ transaction }); } }); return points_of_interest; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const points_of_interest = await db.points_of_interest.findByPk( id, options, ); await points_of_interest.update( { deletedBy: currentUser.id, }, { transaction, }, ); await points_of_interest.destroy({ transaction, }); return points_of_interest; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const points_of_interest = await db.points_of_interest.findOne( { where }, { transaction }, ); if (!points_of_interest) { return points_of_interest; } const output = points_of_interest.get({ plain: true }); output.favorites_point_of_interest = await points_of_interest.getFavorites_point_of_interest({ transaction, }); output.images = await points_of_interest.getImages({ transaction, }); output.empresa = await points_of_interest.getEmpresa({ transaction, }); return output; } static async findAll(filter, globalAccess, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; const user = (options && options.currentUser) || null; const userEmpresa = (user && user.Empresa?.id) || null; if (userEmpresa) { if (options?.currentUser?.EmpresaId) { where.EmpresaId = options.currentUser.EmpresaId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.empresa, as: 'empresa', where: filter.empresa ? { [Op.or]: [ { id: { [Op.in]: filter.empresa .split('|') .map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: filter.empresa .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.file, as: 'images', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike('points_of_interest', 'name', filter.name), }; } if (filter.description) { where = { ...where, [Op.and]: Utils.ilike( 'points_of_interest', 'description', filter.description, ), }; } if (filter.category) { where = { ...where, [Op.and]: Utils.ilike( 'points_of_interest', 'category', filter.category, ), }; } if (filter.address) { where = { ...where, [Op.and]: Utils.ilike( 'points_of_interest', 'address', filter.address, ), }; } if (filter.latitudeRange) { const [start, end] = filter.latitudeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, latitude: { ...where.latitude, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, latitude: { ...where.latitude, [Op.lte]: end, }, }; } } if (filter.longitudeRange) { const [start, end] = filter.longitudeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, longitude: { ...where.longitude, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, longitude: { ...where.longitude, [Op.lte]: end, }, }; } } 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, }, }; } } } if (globalAccess) { delete where.EmpresaId; } 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.points_of_interest.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, globalAccess, organizationId, ) { let where = {}; if (!globalAccess && organizationId) { where.organizationId = organizationId; } if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike('points_of_interest', 'name', query), ], }; } const records = await db.points_of_interest.findAll({ attributes: ['id', 'name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.name, })); } };