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 GuidesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const guides = await db.guides.create( { id: data.id || undefined, bio: data.bio || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await guides.setUser(data.user || null, { transaction, }); await guides.setTour_packages(data.tour_packages || [], { transaction, }); await guides.setReviews(data.reviews || [], { transaction, }); await guides.setAvailabilities(data.availabilities || [], { transaction, }); return guides; } 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 guidesData = data.map((item, index) => ({ id: item.id || undefined, bio: item.bio || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const guides = await db.guides.bulkCreate(guidesData, { transaction }); // For each item created, replace relation files return guides; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const guides = await db.guides.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.bio !== undefined) updatePayload.bio = data.bio; updatePayload.updatedById = currentUser.id; await guides.update(updatePayload, { transaction }); if (data.user !== undefined) { await guides.setUser( data.user, { transaction }, ); } if (data.tour_packages !== undefined) { await guides.setTour_packages(data.tour_packages, { transaction }); } if (data.reviews !== undefined) { await guides.setReviews(data.reviews, { transaction }); } if (data.availabilities !== undefined) { await guides.setAvailabilities(data.availabilities, { transaction }); } return guides; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const guides = await db.guides.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of guides) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of guides) { await record.destroy({ transaction }); } }); return guides; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const guides = await db.guides.findByPk(id, options); await guides.update( { deletedBy: currentUser.id, }, { transaction, }, ); await guides.destroy({ transaction, }); return guides; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const guides = await db.guides.findOne({ where }, { transaction }); if (!guides) { return guides; } const output = guides.get({ plain: true }); output.availabilities_guide = await guides.getAvailabilities_guide({ transaction, }); output.bookings_guide = await guides.getBookings_guide({ transaction, }); output.reviews_guide = await guides.getReviews_guide({ transaction, }); output.tour_packages_guide = await guides.getTour_packages_guide({ transaction, }); output.user = await guides.getUser({ transaction, }); output.tour_packages = await guides.getTour_packages({ transaction, }); output.reviews = await guides.getReviews({ transaction, }); output.availabilities = await guides.getAvailabilities({ 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.users, as: 'user', where: filter.user ? { [Op.or]: [ { id: { [Op.in]: filter.user .split('|') .map((term) => Utils.uuid(term)), }, }, { firstName: { [Op.or]: filter.user .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.tour_packages, as: 'tour_packages', required: false, }, { model: db.reviews, as: 'reviews', required: false, }, { model: db.availabilities, as: 'availabilities', required: false, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.bio) { where = { ...where, [Op.and]: Utils.ilike('guides', 'bio', filter.bio), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.tour_packages) { const searchTerms = filter.tour_packages.split('|'); include = [ { model: db.tour_packages, as: 'tour_packages_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { title: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } if (filter.reviews) { const searchTerms = filter.reviews.split('|'); include = [ { model: db.reviews, as: 'reviews_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { comment: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } if (filter.availabilities) { const searchTerms = filter.availabilities.split('|'); include = [ { model: db.availabilities, as: 'availabilities_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { available_from: { [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, }, }; } } } 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.guides.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('guides', 'bio', query), ], }; } const records = await db.guides.findAll({ attributes: ['id', 'bio'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['bio', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.bio, })); } };