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 Timeline_postsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const timeline_posts = await db.timeline_posts.create( { id: data.id || undefined, post_type: data.post_type || null , content: data.content || null , link_url: data.link_url || null , link_title: data.link_title || null , link_description: data.link_description || null , visibility: data.visibility || null , is_pinned: data.is_pinned || false , is_edited: data.is_edited || false , published_at: data.published_at || null , edited_at: data.edited_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await timeline_posts.setAuthor( data.author || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'media_images', belongsToId: timeline_posts.id, }, data.media_images, options, ); await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'media_files', belongsToId: timeline_posts.id, }, data.media_files, options, ); await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'link_preview_images', belongsToId: timeline_posts.id, }, data.link_preview_images, options, ); return timeline_posts; } 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 timeline_postsData = data.map((item, index) => ({ id: item.id || undefined, post_type: item.post_type || null , content: item.content || null , link_url: item.link_url || null , link_title: item.link_title || null , link_description: item.link_description || null , visibility: item.visibility || null , is_pinned: item.is_pinned || false , is_edited: item.is_edited || false , published_at: item.published_at || null , edited_at: item.edited_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const timeline_posts = await db.timeline_posts.bulkCreate(timeline_postsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < timeline_posts.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'media_images', belongsToId: timeline_posts[i].id, }, data[i].media_images, options, ); } for (let i = 0; i < timeline_posts.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'media_files', belongsToId: timeline_posts[i].id, }, data[i].media_files, options, ); } for (let i = 0; i < timeline_posts.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'link_preview_images', belongsToId: timeline_posts[i].id, }, data[i].link_preview_images, options, ); } return timeline_posts; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const timeline_posts = await db.timeline_posts.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.post_type !== undefined) updatePayload.post_type = data.post_type; if (data.content !== undefined) updatePayload.content = data.content; if (data.link_url !== undefined) updatePayload.link_url = data.link_url; if (data.link_title !== undefined) updatePayload.link_title = data.link_title; if (data.link_description !== undefined) updatePayload.link_description = data.link_description; if (data.visibility !== undefined) updatePayload.visibility = data.visibility; if (data.is_pinned !== undefined) updatePayload.is_pinned = data.is_pinned; if (data.is_edited !== undefined) updatePayload.is_edited = data.is_edited; if (data.published_at !== undefined) updatePayload.published_at = data.published_at; if (data.edited_at !== undefined) updatePayload.edited_at = data.edited_at; updatePayload.updatedById = currentUser.id; await timeline_posts.update(updatePayload, {transaction}); if (data.author !== undefined) { await timeline_posts.setAuthor( data.author, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'media_images', belongsToId: timeline_posts.id, }, data.media_images, options, ); await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'media_files', belongsToId: timeline_posts.id, }, data.media_files, options, ); await FileDBApi.replaceRelationFiles( { belongsTo: db.timeline_posts.getTableName(), belongsToColumn: 'link_preview_images', belongsToId: timeline_posts.id, }, data.link_preview_images, options, ); return timeline_posts; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const timeline_posts = await db.timeline_posts.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of timeline_posts) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of timeline_posts) { await record.destroy({transaction}); } }); return timeline_posts; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const timeline_posts = await db.timeline_posts.findByPk(id, options); await timeline_posts.update({ deletedBy: currentUser.id }, { transaction, }); await timeline_posts.destroy({ transaction }); return timeline_posts; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const timeline_posts = await db.timeline_posts.findOne( { where }, { transaction }, ); if (!timeline_posts) { return timeline_posts; } const output = timeline_posts.get({plain: true}); output.post_comments_post = await timeline_posts.getPost_comments_post({ transaction }); output.post_reactions_post = await timeline_posts.getPost_reactions_post({ transaction }); output.author = await timeline_posts.getAuthor({ transaction }); output.media_images = await timeline_posts.getMedia_images({ transaction }); output.media_files = await timeline_posts.getMedia_files({ transaction }); output.link_preview_images = await timeline_posts.getLink_preview_images({ 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: 'author', where: filter.author ? { [Op.or]: [ { id: { [Op.in]: filter.author.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.author.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'media_images', }, { model: db.file, as: 'media_files', }, { model: db.file, as: 'link_preview_images', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.content) { where = { ...where, [Op.and]: Utils.ilike( 'timeline_posts', 'content', filter.content, ), }; } if (filter.link_url) { where = { ...where, [Op.and]: Utils.ilike( 'timeline_posts', 'link_url', filter.link_url, ), }; } if (filter.link_title) { where = { ...where, [Op.and]: Utils.ilike( 'timeline_posts', 'link_title', filter.link_title, ), }; } if (filter.link_description) { where = { ...where, [Op.and]: Utils.ilike( 'timeline_posts', 'link_description', filter.link_description, ), }; } if (filter.published_atRange) { const [start, end] = filter.published_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, published_at: { ...where.published_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, published_at: { ...where.published_at, [Op.lte]: end, }, }; } } if (filter.edited_atRange) { const [start, end] = filter.edited_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, edited_at: { ...where.edited_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, edited_at: { ...where.edited_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.post_type) { where = { ...where, post_type: filter.post_type, }; } if (filter.visibility) { where = { ...where, visibility: filter.visibility, }; } if (filter.is_pinned) { where = { ...where, is_pinned: filter.is_pinned, }; } if (filter.is_edited) { where = { ...where, is_edited: filter.is_edited, }; } 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.timeline_posts.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( 'timeline_posts', 'link_title', query, ), ], }; } const records = await db.timeline_posts.findAll({ attributes: [ 'id', 'link_title' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['link_title', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.link_title, })); } };