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 Section_itemsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const section_items = await db.section_items.create( { id: data.id || undefined, title: data.title || null , description: data.description || null , link_label: data.link_label || null , link_url: data.link_url || null , sort_order: data.sort_order || null , is_highlighted: data.is_highlighted || false , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await section_items.setPage_section( data.page_section || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.section_items.getTableName(), belongsToColumn: 'images', belongsToId: section_items.id, }, data.images, options, ); return section_items; } 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 section_itemsData = data.map((item, index) => ({ id: item.id || undefined, title: item.title || null , description: item.description || null , link_label: item.link_label || null , link_url: item.link_url || null , sort_order: item.sort_order || null , is_highlighted: item.is_highlighted || false , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const section_items = await db.section_items.bulkCreate(section_itemsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < section_items.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.section_items.getTableName(), belongsToColumn: 'images', belongsToId: section_items[i].id, }, data[i].images, options, ); } return section_items; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const section_items = await db.section_items.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.title !== undefined) updatePayload.title = data.title; if (data.description !== undefined) updatePayload.description = data.description; if (data.link_label !== undefined) updatePayload.link_label = data.link_label; if (data.link_url !== undefined) updatePayload.link_url = data.link_url; if (data.sort_order !== undefined) updatePayload.sort_order = data.sort_order; if (data.is_highlighted !== undefined) updatePayload.is_highlighted = data.is_highlighted; updatePayload.updatedById = currentUser.id; await section_items.update(updatePayload, {transaction}); if (data.page_section !== undefined) { await section_items.setPage_section( data.page_section, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.section_items.getTableName(), belongsToColumn: 'images', belongsToId: section_items.id, }, data.images, options, ); return section_items; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const section_items = await db.section_items.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of section_items) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of section_items) { await record.destroy({transaction}); } }); return section_items; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const section_items = await db.section_items.findByPk(id, options); await section_items.update({ deletedBy: currentUser.id }, { transaction, }); await section_items.destroy({ transaction }); return section_items; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const section_items = await db.section_items.findOne( { where }, { transaction }, ); if (!section_items) { return section_items; } const output = section_items.get({plain: true}); output.page_section = await section_items.getPage_section({ transaction }); output.images = await section_items.getImages({ 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.page_sections, as: 'page_section', where: filter.page_section ? { [Op.or]: [ { id: { [Op.in]: filter.page_section.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.page_section.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'images', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.title) { where = { ...where, [Op.and]: Utils.ilike( 'section_items', 'title', filter.title, ), }; } if (filter.description) { where = { ...where, [Op.and]: Utils.ilike( 'section_items', 'description', filter.description, ), }; } if (filter.link_label) { where = { ...where, [Op.and]: Utils.ilike( 'section_items', 'link_label', filter.link_label, ), }; } if (filter.link_url) { where = { ...where, [Op.and]: Utils.ilike( 'section_items', 'link_url', filter.link_url, ), }; } if (filter.sort_orderRange) { const [start, end] = filter.sort_orderRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, sort_order: { ...where.sort_order, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, sort_order: { ...where.sort_order, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.is_highlighted) { where = { ...where, is_highlighted: filter.is_highlighted, }; } 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.section_items.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( 'section_items', 'title', query, ), ], }; } const records = await db.section_items.findAll({ attributes: [ 'id', 'title' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['title', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.title, })); } };