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 Prayer_requestsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const prayer_requests = await db.prayer_requests.create( { id: data.id || undefined, requester_name: data.requester_name || null , requester_email: data.requester_email || null , requester_phone: data.requester_phone || null , subject: data.subject || null , message: data.message || null , status: data.status || null , is_public: data.is_public || false , requested_at: data.requested_at || null , answered_at: data.answered_at || null , admin_notes: data.admin_notes || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await prayer_requests.setHandled_by_user( data.handled_by_user || null, { transaction, }); return prayer_requests; } 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 prayer_requestsData = data.map((item, index) => ({ id: item.id || undefined, requester_name: item.requester_name || null , requester_email: item.requester_email || null , requester_phone: item.requester_phone || null , subject: item.subject || null , message: item.message || null , status: item.status || null , is_public: item.is_public || false , requested_at: item.requested_at || null , answered_at: item.answered_at || null , admin_notes: item.admin_notes || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const prayer_requests = await db.prayer_requests.bulkCreate(prayer_requestsData, { transaction }); // For each item created, replace relation files return prayer_requests; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const prayer_requests = await db.prayer_requests.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.requester_name !== undefined) updatePayload.requester_name = data.requester_name; if (data.requester_email !== undefined) updatePayload.requester_email = data.requester_email; if (data.requester_phone !== undefined) updatePayload.requester_phone = data.requester_phone; if (data.subject !== undefined) updatePayload.subject = data.subject; if (data.message !== undefined) updatePayload.message = data.message; if (data.status !== undefined) updatePayload.status = data.status; if (data.is_public !== undefined) updatePayload.is_public = data.is_public; if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at; if (data.answered_at !== undefined) updatePayload.answered_at = data.answered_at; if (data.admin_notes !== undefined) updatePayload.admin_notes = data.admin_notes; updatePayload.updatedById = currentUser.id; await prayer_requests.update(updatePayload, {transaction}); if (data.handled_by_user !== undefined) { await prayer_requests.setHandled_by_user( data.handled_by_user, { transaction } ); } return prayer_requests; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const prayer_requests = await db.prayer_requests.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of prayer_requests) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of prayer_requests) { await record.destroy({transaction}); } }); return prayer_requests; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const prayer_requests = await db.prayer_requests.findByPk(id, options); await prayer_requests.update({ deletedBy: currentUser.id }, { transaction, }); await prayer_requests.destroy({ transaction }); return prayer_requests; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const prayer_requests = await db.prayer_requests.findOne( { where }, { transaction }, ); if (!prayer_requests) { return prayer_requests; } const output = prayer_requests.get({plain: true}); output.handled_by_user = await prayer_requests.getHandled_by_user({ 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: 'handled_by_user', where: filter.handled_by_user ? { [Op.or]: [ { id: { [Op.in]: filter.handled_by_user.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.handled_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.requester_name) { where = { ...where, [Op.and]: Utils.ilike( 'prayer_requests', 'requester_name', filter.requester_name, ), }; } if (filter.requester_email) { where = { ...where, [Op.and]: Utils.ilike( 'prayer_requests', 'requester_email', filter.requester_email, ), }; } if (filter.requester_phone) { where = { ...where, [Op.and]: Utils.ilike( 'prayer_requests', 'requester_phone', filter.requester_phone, ), }; } if (filter.subject) { where = { ...where, [Op.and]: Utils.ilike( 'prayer_requests', 'subject', filter.subject, ), }; } if (filter.message) { where = { ...where, [Op.and]: Utils.ilike( 'prayer_requests', 'message', filter.message, ), }; } if (filter.admin_notes) { where = { ...where, [Op.and]: Utils.ilike( 'prayer_requests', 'admin_notes', filter.admin_notes, ), }; } if (filter.requested_atRange) { const [start, end] = filter.requested_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, requested_at: { ...where.requested_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, requested_at: { ...where.requested_at, [Op.lte]: end, }, }; } } if (filter.answered_atRange) { const [start, end] = filter.answered_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, answered_at: { ...where.answered_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, answered_at: { ...where.answered_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.is_public) { where = { ...where, is_public: filter.is_public, }; } 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.prayer_requests.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( 'prayer_requests', 'subject', query, ), ], }; } const records = await db.prayer_requests.findAll({ attributes: [ 'id', 'subject' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['subject', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.subject, })); } };