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 Content_templatesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const content_templates = await db.content_templates.create( { id: data.id || undefined, name: data.name || null , template_type: data.template_type || null , language: data.language || null , subject_line: data.subject_line || null , body: data.body || null , is_default: data.is_default || false , status: data.status || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await content_templates.setAgencies( data.agencies || null, { transaction, }); return content_templates; } 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 content_templatesData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null , template_type: item.template_type || null , language: item.language || null , subject_line: item.subject_line || null , body: item.body || null , is_default: item.is_default || false , status: item.status || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const content_templates = await db.content_templates.bulkCreate(content_templatesData, { transaction }); // For each item created, replace relation files return content_templates; } 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 content_templates = await db.content_templates.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.template_type !== undefined) updatePayload.template_type = data.template_type; if (data.language !== undefined) updatePayload.language = data.language; if (data.subject_line !== undefined) updatePayload.subject_line = data.subject_line; if (data.body !== undefined) updatePayload.body = data.body; if (data.is_default !== undefined) updatePayload.is_default = data.is_default; if (data.status !== undefined) updatePayload.status = data.status; updatePayload.updatedById = currentUser.id; await content_templates.update(updatePayload, {transaction}); if (data.agencies !== undefined) { await content_templates.setAgencies( data.agencies, { transaction } ); } return content_templates; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const content_templates = await db.content_templates.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of content_templates) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of content_templates) { await record.destroy({transaction}); } }); return content_templates; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const content_templates = await db.content_templates.findByPk(id, options); await content_templates.update({ deletedBy: currentUser.id }, { transaction, }); await content_templates.destroy({ transaction }); return content_templates; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const content_templates = await db.content_templates.findOne( { where }, { transaction }, ); if (!content_templates) { return content_templates; } const output = content_templates.get({plain: true}); output.email_campaigns_template = await content_templates.getEmail_campaigns_template({ transaction }); output.pdf_documents_template = await content_templates.getPdf_documents_template({ transaction }); output.agencies = await content_templates.getAgencies({ 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 userAgencies = (user && user.agencies?.id) || null; if (userAgencies) { if (options?.currentUser?.agenciesId) { where.agenciesId = options.currentUser.agenciesId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.agencies, as: 'agencies', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike( 'content_templates', 'name', filter.name, ), }; } if (filter.subject_line) { where = { ...where, [Op.and]: Utils.ilike( 'content_templates', 'subject_line', filter.subject_line, ), }; } if (filter.body) { where = { ...where, [Op.and]: Utils.ilike( 'content_templates', 'body', filter.body, ), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.template_type) { where = { ...where, template_type: filter.template_type, }; } if (filter.language) { where = { ...where, language: filter.language, }; } if (filter.is_default) { where = { ...where, is_default: filter.is_default, }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.agencies) { const listItems = filter.agencies.split('|').map(item => { return Utils.uuid(item) }); where = { ...where, agenciesId: {[Op.or]: listItems} }; } 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.agenciesId; } 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.content_templates.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( 'content_templates', 'name', query, ), ], }; } const records = await db.content_templates.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, })); } };