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 Tiktok_viewersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const tiktok_viewers = await db.tiktok_viewers.create( { id: data.id || undefined, display_name: data.display_name || null , tiktok_user_identifier: data.tiktok_user_identifier || null , profile_url: data.profile_url || null , is_vip: data.is_vip || false , vip_level: data.vip_level || null , first_seen_at: data.first_seen_at || null , last_seen_at: data.last_seen_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await FileDBApi.replaceRelationFiles( { belongsTo: db.tiktok_viewers.getTableName(), belongsToColumn: 'profile_images', belongsToId: tiktok_viewers.id, }, data.profile_images, options, ); return tiktok_viewers; } 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 tiktok_viewersData = data.map((item, index) => ({ id: item.id || undefined, display_name: item.display_name || null , tiktok_user_identifier: item.tiktok_user_identifier || null , profile_url: item.profile_url || null , is_vip: item.is_vip || false , vip_level: item.vip_level || null , first_seen_at: item.first_seen_at || null , last_seen_at: item.last_seen_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const tiktok_viewers = await db.tiktok_viewers.bulkCreate(tiktok_viewersData, { transaction }); // For each item created, replace relation files for (let i = 0; i < tiktok_viewers.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.tiktok_viewers.getTableName(), belongsToColumn: 'profile_images', belongsToId: tiktok_viewers[i].id, }, data[i].profile_images, options, ); } return tiktok_viewers; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const tiktok_viewers = await db.tiktok_viewers.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.display_name !== undefined) updatePayload.display_name = data.display_name; if (data.tiktok_user_identifier !== undefined) updatePayload.tiktok_user_identifier = data.tiktok_user_identifier; if (data.profile_url !== undefined) updatePayload.profile_url = data.profile_url; if (data.is_vip !== undefined) updatePayload.is_vip = data.is_vip; if (data.vip_level !== undefined) updatePayload.vip_level = data.vip_level; if (data.first_seen_at !== undefined) updatePayload.first_seen_at = data.first_seen_at; if (data.last_seen_at !== undefined) updatePayload.last_seen_at = data.last_seen_at; updatePayload.updatedById = currentUser.id; await tiktok_viewers.update(updatePayload, {transaction}); await FileDBApi.replaceRelationFiles( { belongsTo: db.tiktok_viewers.getTableName(), belongsToColumn: 'profile_images', belongsToId: tiktok_viewers.id, }, data.profile_images, options, ); return tiktok_viewers; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const tiktok_viewers = await db.tiktok_viewers.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of tiktok_viewers) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of tiktok_viewers) { await record.destroy({transaction}); } }); return tiktok_viewers; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const tiktok_viewers = await db.tiktok_viewers.findByPk(id, options); await tiktok_viewers.update({ deletedBy: currentUser.id }, { transaction, }); await tiktok_viewers.destroy({ transaction }); return tiktok_viewers; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const tiktok_viewers = await db.tiktok_viewers.findOne( { where }, { transaction }, ); if (!tiktok_viewers) { return tiktok_viewers; } const output = tiktok_viewers.get({plain: true}); output.arena_players_viewer = await tiktok_viewers.getArena_players_viewer({ transaction }); output.tiktok_live_events_viewer = await tiktok_viewers.getTiktok_live_events_viewer({ transaction }); output.leaderboard_entries_viewer = await tiktok_viewers.getLeaderboard_entries_viewer({ transaction }); output.profile_images = await tiktok_viewers.getProfile_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.file, as: 'profile_images', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.display_name) { where = { ...where, [Op.and]: Utils.ilike( 'tiktok_viewers', 'display_name', filter.display_name, ), }; } if (filter.tiktok_user_identifier) { where = { ...where, [Op.and]: Utils.ilike( 'tiktok_viewers', 'tiktok_user_identifier', filter.tiktok_user_identifier, ), }; } if (filter.profile_url) { where = { ...where, [Op.and]: Utils.ilike( 'tiktok_viewers', 'profile_url', filter.profile_url, ), }; } if (filter.vip_levelRange) { const [start, end] = filter.vip_levelRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, vip_level: { ...where.vip_level, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, vip_level: { ...where.vip_level, [Op.lte]: end, }, }; } } if (filter.first_seen_atRange) { const [start, end] = filter.first_seen_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, first_seen_at: { ...where.first_seen_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, first_seen_at: { ...where.first_seen_at, [Op.lte]: end, }, }; } } if (filter.last_seen_atRange) { const [start, end] = filter.last_seen_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, last_seen_at: { ...where.last_seen_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, last_seen_at: { ...where.last_seen_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.is_vip) { where = { ...where, is_vip: filter.is_vip, }; } 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.tiktok_viewers.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( 'tiktok_viewers', 'display_name', query, ), ], }; } const records = await db.tiktok_viewers.findAll({ attributes: [ 'id', 'display_name' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['display_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.display_name, })); } };