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 Tourist_spotsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const tourist_spots = await db.tourist_spots.create( { id: data.id || undefined, name: data.name || null, location: data.location || null, crowd_density: data.crowd_density || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await tourist_spots.setSmarttourismhub(data.smarttourismhub || null, { transaction, }); await tourist_spots.setNearby_accommodations( data.nearby_accommodations || [], { transaction, }, ); await tourist_spots.setNearby_restaurants(data.nearby_restaurants || [], { transaction, }); await tourist_spots.setNearby_health_facilities( data.nearby_health_facilities || [], { transaction, }, ); return tourist_spots; } 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 tourist_spotsData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null, location: item.location || null, crowd_density: item.crowd_density || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const tourist_spots = await db.tourist_spots.bulkCreate(tourist_spotsData, { transaction, }); // For each item created, replace relation files return tourist_spots; } 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 tourist_spots = await db.tourist_spots.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.location !== undefined) updatePayload.location = data.location; if (data.crowd_density !== undefined) updatePayload.crowd_density = data.crowd_density; updatePayload.updatedById = currentUser.id; await tourist_spots.update(updatePayload, { transaction }); if (data.smarttourismhub !== undefined) { await tourist_spots.setSmarttourismhub( data.smarttourismhub, { transaction }, ); } if (data.nearby_accommodations !== undefined) { await tourist_spots.setNearby_accommodations(data.nearby_accommodations, { transaction, }); } if (data.nearby_restaurants !== undefined) { await tourist_spots.setNearby_restaurants(data.nearby_restaurants, { transaction, }); } if (data.nearby_health_facilities !== undefined) { await tourist_spots.setNearby_health_facilities( data.nearby_health_facilities, { transaction }, ); } return tourist_spots; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const tourist_spots = await db.tourist_spots.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of tourist_spots) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of tourist_spots) { await record.destroy({ transaction }); } }); return tourist_spots; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const tourist_spots = await db.tourist_spots.findByPk(id, options); await tourist_spots.update( { deletedBy: currentUser.id, }, { transaction, }, ); await tourist_spots.destroy({ transaction, }); return tourist_spots; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const tourist_spots = await db.tourist_spots.findOne( { where }, { transaction }, ); if (!tourist_spots) { return tourist_spots; } const output = tourist_spots.get({ plain: true }); output.nearby_accommodations = await tourist_spots.getNearby_accommodations( { transaction, }, ); output.nearby_restaurants = await tourist_spots.getNearby_restaurants({ transaction, }); output.nearby_health_facilities = await tourist_spots.getNearby_health_facilities({ transaction, }); output.smarttourismhub = await tourist_spots.getSmarttourismhub({ 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 userSmarttourismhub = (user && user.SmartTourismHub?.id) || null; if (userSmarttourismhub) { if (options?.currentUser?.SmartTourismHubId) { where.SmartTourismHubId = options.currentUser.SmartTourismHubId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.smarttourismhub, as: 'smarttourismhub', where: filter.smarttourismhub ? { [Op.or]: [ { id: { [Op.in]: filter.smarttourismhub .split('|') .map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: filter.smarttourismhub .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.accommodations, as: 'nearby_accommodations', required: false, }, { model: db.accommodations, as: 'nearby_restaurants', required: false, }, { model: db.health_facilities, as: 'nearby_health_facilities', required: false, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike('tourist_spots', 'name', filter.name), }; } if (filter.location) { where = { ...where, [Op.and]: Utils.ilike('tourist_spots', 'location', filter.location), }; } if (filter.crowd_densityRange) { const [start, end] = filter.crowd_densityRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, crowd_density: { ...where.crowd_density, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, crowd_density: { ...where.crowd_density, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.nearby_accommodations) { const searchTerms = filter.nearby_accommodations.split('|'); include = [ { model: db.accommodations, as: 'nearby_accommodations_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } if (filter.nearby_restaurants) { const searchTerms = filter.nearby_restaurants.split('|'); include = [ { model: db.accommodations, as: 'nearby_restaurants_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } if (filter.nearby_health_facilities) { const searchTerms = filter.nearby_health_facilities.split('|'); include = [ { model: db.health_facilities, as: 'nearby_health_facilities_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } 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.SmartTourismHubId; } 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.tourist_spots.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('tourist_spots', 'name', query), ], }; } const records = await db.tourist_spots.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, })); } };