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 Family_membersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const family_members = await db.family_members.create( { id: data.id || undefined, name: data.name || null, date_of_birth: data.date_of_birth || null, age_group: data.age_group || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await family_members.setUser(data.user || null, { transaction, }); await family_members.setDocuments(data.documents || [], { transaction, }); await family_members.setLoyalty_programs(data.loyalty_programs || [], { transaction, }); return family_members; } 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 family_membersData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null, date_of_birth: item.date_of_birth || null, age_group: item.age_group || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const family_members = await db.family_members.bulkCreate( family_membersData, { transaction }, ); // For each item created, replace relation files return family_members; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const family_members = await db.family_members.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.date_of_birth !== undefined) updatePayload.date_of_birth = data.date_of_birth; if (data.age_group !== undefined) updatePayload.age_group = data.age_group; updatePayload.updatedById = currentUser.id; await family_members.update(updatePayload, { transaction }); if (data.user !== undefined) { await family_members.setUser( data.user, { transaction }, ); } if (data.documents !== undefined) { await family_members.setDocuments(data.documents, { transaction }); } if (data.loyalty_programs !== undefined) { await family_members.setLoyalty_programs(data.loyalty_programs, { transaction, }); } return family_members; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const family_members = await db.family_members.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of family_members) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of family_members) { await record.destroy({ transaction }); } }); return family_members; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const family_members = await db.family_members.findByPk(id, options); await family_members.update( { deletedBy: currentUser.id, }, { transaction, }, ); await family_members.destroy({ transaction, }); return family_members; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const family_members = await db.family_members.findOne( { where }, { transaction }, ); if (!family_members) { return family_members; } const output = family_members.get({ plain: true }); output.documents = await family_members.getDocuments({ transaction, }); output.loyalty_programs = await family_members.getLoyalty_programs({ transaction, }); output.user = await family_members.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}%` })), }, }, ], } : {}, }, { model: db.documents, as: 'documents', required: false, }, { model: db.loyalty_programs, as: 'loyalty_programs', required: false, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike('family_members', 'name', filter.name), }; } if (filter.date_of_birthRange) { const [start, end] = filter.date_of_birthRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, date_of_birth: { ...where.date_of_birth, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, date_of_birth: { ...where.date_of_birth, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.age_group) { where = { ...where, age_group: filter.age_group, }; } if (filter.documents) { const searchTerms = filter.documents.split('|'); include = [ { model: db.documents, as: 'documents_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { document_type: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } if (filter.loyalty_programs) { const searchTerms = filter.loyalty_programs.split('|'); include = [ { model: db.loyalty_programs, as: 'loyalty_programs_filter', required: searchTerms.length > 0, where: searchTerms.length > 0 ? { [Op.or]: [ { id: { [Op.in]: searchTerms.map((term) => Utils.uuid(term)), }, }, { program_name: { [Op.or]: searchTerms.map((term) => ({ [Op.iLike]: `%${term}%`, })), }, }, ], } : undefined, }, ...include, ]; } 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.family_members.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('family_members', 'name', query), ], }; } const records = await db.family_members.findAll({ attributes: ['id', 'name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.name, })); } };