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 CalendarsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const calendars = await db.calendars.create( { id: data.id || undefined, event_name: data.event_name || null, event_start: data.event_start || null, event_end: data.event_end || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await calendars.setParticipants(data.participants || [], { transaction, }); return calendars; } 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 calendarsData = data.map((item, index) => ({ id: item.id || undefined, event_name: item.event_name || null, event_start: item.event_start || null, event_end: item.event_end || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const calendars = await db.calendars.bulkCreate(calendarsData, { transaction, }); // For each item created, replace relation files return calendars; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const calendars = await db.calendars.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.event_name !== undefined) updatePayload.event_name = data.event_name; if (data.event_start !== undefined) updatePayload.event_start = data.event_start; if (data.event_end !== undefined) updatePayload.event_end = data.event_end; updatePayload.updatedById = currentUser.id; await calendars.update(updatePayload, { transaction }); if (data.participants !== undefined) { await calendars.setParticipants(data.participants, { transaction }); } return calendars; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const calendars = await db.calendars.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of calendars) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of calendars) { await record.destroy({ transaction }); } }); return calendars; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const calendars = await db.calendars.findByPk(id, options); await calendars.update( { deletedBy: currentUser.id, }, { transaction, }, ); await calendars.destroy({ transaction, }); return calendars; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const calendars = await db.calendars.findOne({ where }, { transaction }); if (!calendars) { return calendars; } const output = calendars.get({ plain: true }); output.participants = await calendars.getParticipants({ 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: 'participants', required: false, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.event_name) { where = { ...where, [Op.and]: Utils.ilike('calendars', 'event_name', filter.event_name), }; } if (filter.calendarStart && filter.calendarEnd) { where = { ...where, [Op.or]: [ { event_start: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, { event_end: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, ], }; } if (filter.event_startRange) { const [start, end] = filter.event_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, event_start: { ...where.event_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, event_start: { ...where.event_start, [Op.lte]: end, }, }; } } if (filter.event_endRange) { const [start, end] = filter.event_endRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, event_end: { ...where.event_end, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, event_end: { ...where.event_end, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.participants) { const searchTerms = filter.participants.split('|'); include = [ { model: db.users, as: 'participants_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { firstName: { [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.calendars.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('calendars', 'event_name', query), ], }; } const records = await db.calendars.findAll({ attributes: ['id', 'event_name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['event_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.event_name, })); } };