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 MatchesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const matches = await db.matches.create( { id: data.id || undefined, matched_at: data.matched_at || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await matches.setStudent_one(data.student_one || null, { transaction, }); await matches.setStudent_two(data.student_two || null, { transaction, }); await matches.setUniversities(data.universities || null, { transaction, }); return matches; } 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 matchesData = data.map((item, index) => ({ id: item.id || undefined, matched_at: item.matched_at || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const matches = await db.matches.bulkCreate(matchesData, { transaction }); // For each item created, replace relation files return matches; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const globalAccess = currentUser.app_role?.globalAccess; const matches = await db.matches.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.matched_at !== undefined) updatePayload.matched_at = data.matched_at; updatePayload.updatedById = currentUser.id; await matches.update(updatePayload, { transaction }); if (data.student_one !== undefined) { await matches.setStudent_one( data.student_one, { transaction }, ); } if (data.student_two !== undefined) { await matches.setStudent_two( data.student_two, { transaction }, ); } if (data.universities !== undefined) { await matches.setUniversities( data.universities, { transaction }, ); } return matches; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const matches = await db.matches.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of matches) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of matches) { await record.destroy({ transaction }); } }); return matches; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const matches = await db.matches.findByPk(id, options); await matches.update( { deletedBy: currentUser.id, }, { transaction, }, ); await matches.destroy({ transaction, }); return matches; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const matches = await db.matches.findOne({ where }, { transaction }); if (!matches) { return matches; } const output = matches.get({ plain: true }); output.student_one = await matches.getStudent_one({ transaction, }); output.student_two = await matches.getStudent_two({ transaction, }); output.universities = await matches.getUniversities({ transaction, }); return output; } static async findAll(filter, globalAccess, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; const user = (options && options.currentUser) || null; const userUniversities = (user && user.universities?.id) || null; if (userUniversities) { if (options?.currentUser?.universitiesId) { where.universitiesId = options.currentUser.universitiesId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.students, as: 'student_one', where: filter.student_one ? { [Op.or]: [ { id: { [Op.in]: filter.student_one .split('|') .map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: filter.student_one .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.students, as: 'student_two', where: filter.student_two ? { [Op.or]: [ { id: { [Op.in]: filter.student_two .split('|') .map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: filter.student_two .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.universities, as: 'universities', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.matched_atRange) { const [start, end] = filter.matched_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, matched_at: { ...where.matched_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, matched_at: { ...where.matched_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.universities) { const listItems = filter.universities.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, universitiesId: { [Op.or]: listItems }, }; } 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, }, }; } } } if (globalAccess) { delete where.universitiesId; } 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.matches.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, globalAccess, organizationId, ) { let where = {}; if (!globalAccess && organizationId) { where.organizationId = organizationId; } if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike('matches', 'student_one', query), ], }; } const records = await db.matches.findAll({ attributes: ['id', 'student_one'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['student_one', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.student_one, })); } };