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 User_feedbackDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const user_feedback = await db.user_feedback.create( { id: data.id || undefined, status: data.status || null , message: data.message || null , admin_note: data.admin_note || null , submitted_at: data.submitted_at || null , reviewed_at: data.reviewed_at || null , is_visible_to_user: data.is_visible_to_user || false , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await user_feedback.setUser( data.user || null, { transaction, }); await user_feedback.setPage_access_rule( data.page_access_rule || null, { transaction, }); return user_feedback; } 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 user_feedbackData = data.map((item, index) => ({ id: item.id || undefined, status: item.status || null , message: item.message || null , admin_note: item.admin_note || null , submitted_at: item.submitted_at || null , reviewed_at: item.reviewed_at || null , is_visible_to_user: item.is_visible_to_user || false , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const user_feedback = await db.user_feedback.bulkCreate(user_feedbackData, { transaction }); // For each item created, replace relation files return user_feedback; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const user_feedback = await db.user_feedback.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.status !== undefined) updatePayload.status = data.status; if (data.message !== undefined) updatePayload.message = data.message; if (data.admin_note !== undefined) updatePayload.admin_note = data.admin_note; if (data.submitted_at !== undefined) updatePayload.submitted_at = data.submitted_at; if (data.reviewed_at !== undefined) updatePayload.reviewed_at = data.reviewed_at; if (data.is_visible_to_user !== undefined) updatePayload.is_visible_to_user = data.is_visible_to_user; updatePayload.updatedById = currentUser.id; await user_feedback.update(updatePayload, {transaction}); if (data.user !== undefined) { await user_feedback.setUser( data.user, { transaction } ); } if (data.page_access_rule !== undefined) { await user_feedback.setPage_access_rule( data.page_access_rule, { transaction } ); } return user_feedback; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const user_feedback = await db.user_feedback.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of user_feedback) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of user_feedback) { await record.destroy({transaction}); } }); return user_feedback; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const user_feedback = await db.user_feedback.findByPk(id, options); await user_feedback.update({ deletedBy: currentUser.id }, { transaction, }); await user_feedback.destroy({ transaction }); return user_feedback; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const user_feedback = await db.user_feedback.findOne( { where }, { transaction }, ); if (!user_feedback) { return user_feedback; } const output = user_feedback.get({plain: true}); output.user = await user_feedback.getUser({ transaction }); output.page_access_rule = await user_feedback.getPage_access_rule({ 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.page_access_rules, as: 'page_access_rule', where: filter.page_access_rule ? { [Op.or]: [ { id: { [Op.in]: filter.page_access_rule.split('|').map(term => Utils.uuid(term)) } }, { rule_name: { [Op.or]: filter.page_access_rule.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.message) { where = { ...where, [Op.and]: Utils.ilike( 'user_feedback', 'message', filter.message, ), }; } if (filter.admin_note) { where = { ...where, [Op.and]: Utils.ilike( 'user_feedback', 'admin_note', filter.admin_note, ), }; } if (filter.submitted_atRange) { const [start, end] = filter.submitted_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, submitted_at: { ...where.submitted_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, submitted_at: { ...where.submitted_at, [Op.lte]: end, }, }; } } if (filter.reviewed_atRange) { const [start, end] = filter.reviewed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, reviewed_at: { ...where.reviewed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, reviewed_at: { ...where.reviewed_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_visible_to_user) { where = { ...where, is_visible_to_user: filter.is_visible_to_user, }; } 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.user_feedback.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( 'user_feedback', 'message', query, ), ], }; } const records = await db.user_feedback.findAll({ attributes: [ 'id', 'message' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['message', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.message, })); } };