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 Game_settingsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const game_settings = await db.game_settings.create( { id: data.id || undefined, difficulty: data.difficulty || null , virtual_keyboard_enabled: data.virtual_keyboard_enabled || false , input_feedback: data.input_feedback || null , theme: data.theme || null , master_volume: data.master_volume || null , sfx_volume: data.sfx_volume || null , music_volume: data.music_volume || null , reduced_motion: data.reduced_motion || false , high_contrast: data.high_contrast || false , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await game_settings.setUser( data.user || null, { transaction, }); return game_settings; } 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 game_settingsData = data.map((item, index) => ({ id: item.id || undefined, difficulty: item.difficulty || null , virtual_keyboard_enabled: item.virtual_keyboard_enabled || false , input_feedback: item.input_feedback || null , theme: item.theme || null , master_volume: item.master_volume || null , sfx_volume: item.sfx_volume || null , music_volume: item.music_volume || null , reduced_motion: item.reduced_motion || false , high_contrast: item.high_contrast || false , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const game_settings = await db.game_settings.bulkCreate(game_settingsData, { transaction }); // For each item created, replace relation files return game_settings; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const game_settings = await db.game_settings.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.difficulty !== undefined) updatePayload.difficulty = data.difficulty; if (data.virtual_keyboard_enabled !== undefined) updatePayload.virtual_keyboard_enabled = data.virtual_keyboard_enabled; if (data.input_feedback !== undefined) updatePayload.input_feedback = data.input_feedback; if (data.theme !== undefined) updatePayload.theme = data.theme; if (data.master_volume !== undefined) updatePayload.master_volume = data.master_volume; if (data.sfx_volume !== undefined) updatePayload.sfx_volume = data.sfx_volume; if (data.music_volume !== undefined) updatePayload.music_volume = data.music_volume; if (data.reduced_motion !== undefined) updatePayload.reduced_motion = data.reduced_motion; if (data.high_contrast !== undefined) updatePayload.high_contrast = data.high_contrast; updatePayload.updatedById = currentUser.id; await game_settings.update(updatePayload, {transaction}); if (data.user !== undefined) { await game_settings.setUser( data.user, { transaction } ); } return game_settings; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const game_settings = await db.game_settings.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of game_settings) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of game_settings) { await record.destroy({transaction}); } }); return game_settings; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const game_settings = await db.game_settings.findByPk(id, options); await game_settings.update({ deletedBy: currentUser.id }, { transaction, }); await game_settings.destroy({ transaction }); return game_settings; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const game_settings = await db.game_settings.findOne( { where }, { transaction }, ); if (!game_settings) { return game_settings; } const output = game_settings.get({plain: true}); output.user = await game_settings.getUser({ 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}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.master_volumeRange) { const [start, end] = filter.master_volumeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, master_volume: { ...where.master_volume, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, master_volume: { ...where.master_volume, [Op.lte]: end, }, }; } } if (filter.sfx_volumeRange) { const [start, end] = filter.sfx_volumeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, sfx_volume: { ...where.sfx_volume, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, sfx_volume: { ...where.sfx_volume, [Op.lte]: end, }, }; } } if (filter.music_volumeRange) { const [start, end] = filter.music_volumeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, music_volume: { ...where.music_volume, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, music_volume: { ...where.music_volume, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.difficulty) { where = { ...where, difficulty: filter.difficulty, }; } if (filter.virtual_keyboard_enabled) { where = { ...where, virtual_keyboard_enabled: filter.virtual_keyboard_enabled, }; } if (filter.input_feedback) { where = { ...where, input_feedback: filter.input_feedback, }; } if (filter.theme) { where = { ...where, theme: filter.theme, }; } if (filter.reduced_motion) { where = { ...where, reduced_motion: filter.reduced_motion, }; } if (filter.high_contrast) { where = { ...where, high_contrast: filter.high_contrast, }; } 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.game_settings.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( 'game_settings', 'theme', query, ), ], }; } const records = await db.game_settings.findAll({ attributes: [ 'id', 'theme' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['theme', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.theme, })); } };