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 PreferencesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const preferences = await db.preferences.create( { id: data.id || undefined, fumeur_preference: data.fumeur_preference || false, ambiance_preference: data.ambiance_preference || null, genre_preference: data.genre_preference || null, age_min: data.age_min || null, age_max: data.age_max || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await preferences.setUtilisateur(data.utilisateur || null, { transaction, }); return preferences; } 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 preferencesData = data.map((item, index) => ({ id: item.id || undefined, fumeur_preference: item.fumeur_preference || false, ambiance_preference: item.ambiance_preference || null, genre_preference: item.genre_preference || null, age_min: item.age_min || null, age_max: item.age_max || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const preferences = await db.preferences.bulkCreate(preferencesData, { transaction, }); // For each item created, replace relation files return preferences; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const preferences = await db.preferences.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.fumeur_preference !== undefined) updatePayload.fumeur_preference = data.fumeur_preference; if (data.ambiance_preference !== undefined) updatePayload.ambiance_preference = data.ambiance_preference; if (data.genre_preference !== undefined) updatePayload.genre_preference = data.genre_preference; if (data.age_min !== undefined) updatePayload.age_min = data.age_min; if (data.age_max !== undefined) updatePayload.age_max = data.age_max; updatePayload.updatedById = currentUser.id; await preferences.update(updatePayload, { transaction }); if (data.utilisateur !== undefined) { await preferences.setUtilisateur( data.utilisateur, { transaction }, ); } return preferences; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const preferences = await db.preferences.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of preferences) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of preferences) { await record.destroy({ transaction }); } }); return preferences; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const preferences = await db.preferences.findByPk(id, options); await preferences.update( { deletedBy: currentUser.id, }, { transaction, }, ); await preferences.destroy({ transaction, }); return preferences; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const preferences = await db.preferences.findOne( { where }, { transaction }, ); if (!preferences) { return preferences; } const output = preferences.get({ plain: true }); output.utilisateur = await preferences.getUtilisateur({ 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.utilisateurs, as: 'utilisateur', where: filter.utilisateur ? { [Op.or]: [ { id: { [Op.in]: filter.utilisateur .split('|') .map((term) => Utils.uuid(term)), }, }, { nom: { [Op.or]: filter.utilisateur .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.age_minRange) { const [start, end] = filter.age_minRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, age_min: { ...where.age_min, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, age_min: { ...where.age_min, [Op.lte]: end, }, }; } } if (filter.age_maxRange) { const [start, end] = filter.age_maxRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, age_max: { ...where.age_max, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, age_max: { ...where.age_max, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.fumeur_preference) { where = { ...where, fumeur_preference: filter.fumeur_preference, }; } if (filter.ambiance_preference) { where = { ...where, ambiance_preference: filter.ambiance_preference, }; } if (filter.genre_preference) { where = { ...where, genre_preference: filter.genre_preference, }; } 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.preferences.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('preferences', 'ambiance_preference', query), ], }; } const records = await db.preferences.findAll({ attributes: ['id', 'ambiance_preference'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['ambiance_preference', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.ambiance_preference, })); } };