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 ChildrenDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const children = await db.children.create( { id: data.id || undefined, first_name: data.first_name || null, last_name: data.last_name || null, birth_number: data.birth_number || null, insurance_number: data.insurance_number || null, zp_code: data.zp_code || null, child_mobile: data.child_mobile || null, insurance_expiry: data.insurance_expiry || null, allergies: data.allergies || null, internal_notes: data.internal_notes || null, birth_date: data.birth_date || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await children.setGuardian(data.guardian || null, { transaction, }); return children; } 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 childrenData = data.map((item, index) => ({ id: item.id || undefined, first_name: item.first_name || null, last_name: item.last_name || null, birth_number: item.birth_number || null, insurance_number: item.insurance_number || null, zp_code: item.zp_code || null, child_mobile: item.child_mobile || null, insurance_expiry: item.insurance_expiry || null, allergies: item.allergies || null, internal_notes: item.internal_notes || null, birth_date: item.birth_date || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const children = await db.children.bulkCreate(childrenData, { transaction, }); // For each item created, replace relation files return children; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const children = await db.children.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.first_name !== undefined) updatePayload.first_name = data.first_name; if (data.last_name !== undefined) updatePayload.last_name = data.last_name; if (data.birth_number !== undefined) updatePayload.birth_number = data.birth_number; if (data.insurance_number !== undefined) updatePayload.insurance_number = data.insurance_number; if (data.zp_code !== undefined) updatePayload.zp_code = data.zp_code; if (data.child_mobile !== undefined) updatePayload.child_mobile = data.child_mobile; if (data.insurance_expiry !== undefined) updatePayload.insurance_expiry = data.insurance_expiry; if (data.allergies !== undefined) updatePayload.allergies = data.allergies; if (data.internal_notes !== undefined) updatePayload.internal_notes = data.internal_notes; if (data.birth_date !== undefined) updatePayload.birth_date = data.birth_date; updatePayload.updatedById = currentUser.id; await children.update(updatePayload, { transaction }); if (data.guardian !== undefined) { await children.setGuardian( data.guardian, { transaction }, ); } return children; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const children = await db.children.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of children) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of children) { await record.destroy({ transaction }); } }); return children; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const children = await db.children.findByPk(id, options); await children.update( { deletedBy: currentUser.id, }, { transaction, }, ); await children.destroy({ transaction, }); return children; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const children = await db.children.findOne({ where }, { transaction }); if (!children) { return children; } const output = children.get({ plain: true }); output.presence_child = await children.getPresence_child({ transaction, }); output.rfid_cards_child = await children.getRfid_cards_child({ transaction, }); output.guardian = await children.getGuardian({ 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.guardians, as: 'guardian', where: filter.guardian ? { [Op.or]: [ { id: { [Op.in]: filter.guardian .split('|') .map((term) => Utils.uuid(term)), }, }, { first_name: { [Op.or]: filter.guardian .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.first_name) { where = { ...where, [Op.and]: Utils.ilike('children', 'first_name', filter.first_name), }; } if (filter.last_name) { where = { ...where, [Op.and]: Utils.ilike('children', 'last_name', filter.last_name), }; } if (filter.birth_number) { where = { ...where, [Op.and]: Utils.ilike( 'children', 'birth_number', filter.birth_number, ), }; } if (filter.insurance_number) { where = { ...where, [Op.and]: Utils.ilike( 'children', 'insurance_number', filter.insurance_number, ), }; } if (filter.zp_code) { where = { ...where, [Op.and]: Utils.ilike('children', 'zp_code', filter.zp_code), }; } if (filter.child_mobile) { where = { ...where, [Op.and]: Utils.ilike( 'children', 'child_mobile', filter.child_mobile, ), }; } if (filter.allergies) { where = { ...where, [Op.and]: Utils.ilike('children', 'allergies', filter.allergies), }; } if (filter.internal_notes) { where = { ...where, [Op.and]: Utils.ilike( 'children', 'internal_notes', filter.internal_notes, ), }; } if (filter.insurance_expiryRange) { const [start, end] = filter.insurance_expiryRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, insurance_expiry: { ...where.insurance_expiry, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, insurance_expiry: { ...where.insurance_expiry, [Op.lte]: end, }, }; } } if (filter.birth_dateRange) { const [start, end] = filter.birth_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, birth_date: { ...where.birth_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, birth_date: { ...where.birth_date, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } 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.children.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('children', 'first_name', query), ], }; } const records = await db.children.findAll({ attributes: ['id', 'first_name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['first_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.first_name, })); } };