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 Ai_predictionsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const ai_predictions = await db.ai_predictions.create( { id: data.id || undefined, predicted_wait_minutes: data.predicted_wait_minutes || null , confidence_score: data.confidence_score || null , source: data.source || null , valid_from: data.valid_from || null , valid_until: data.valid_until || null , explanation: data.explanation || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await ai_predictions.setBranch( data.branch || null, { transaction, }); return ai_predictions; } 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 ai_predictionsData = data.map((item, index) => ({ id: item.id || undefined, predicted_wait_minutes: item.predicted_wait_minutes || null , confidence_score: item.confidence_score || null , source: item.source || null , valid_from: item.valid_from || null , valid_until: item.valid_until || null , explanation: item.explanation || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const ai_predictions = await db.ai_predictions.bulkCreate(ai_predictionsData, { transaction }); // For each item created, replace relation files return ai_predictions; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const ai_predictions = await db.ai_predictions.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.predicted_wait_minutes !== undefined) updatePayload.predicted_wait_minutes = data.predicted_wait_minutes; if (data.confidence_score !== undefined) updatePayload.confidence_score = data.confidence_score; if (data.source !== undefined) updatePayload.source = data.source; if (data.valid_from !== undefined) updatePayload.valid_from = data.valid_from; if (data.valid_until !== undefined) updatePayload.valid_until = data.valid_until; if (data.explanation !== undefined) updatePayload.explanation = data.explanation; updatePayload.updatedById = currentUser.id; await ai_predictions.update(updatePayload, {transaction}); if (data.branch !== undefined) { await ai_predictions.setBranch( data.branch, { transaction } ); } return ai_predictions; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const ai_predictions = await db.ai_predictions.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of ai_predictions) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of ai_predictions) { await record.destroy({transaction}); } }); return ai_predictions; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const ai_predictions = await db.ai_predictions.findByPk(id, options); await ai_predictions.update({ deletedBy: currentUser.id }, { transaction, }); await ai_predictions.destroy({ transaction }); return ai_predictions; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const ai_predictions = await db.ai_predictions.findOne( { where }, { transaction }, ); if (!ai_predictions) { return ai_predictions; } const output = ai_predictions.get({plain: true}); output.branch = await ai_predictions.getBranch({ 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.branches, as: 'branch', where: filter.branch ? { [Op.or]: [ { id: { [Op.in]: filter.branch.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.branch.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.explanation) { where = { ...where, [Op.and]: Utils.ilike( 'ai_predictions', 'explanation', filter.explanation, ), }; } if (filter.calendarStart && filter.calendarEnd) { where = { ...where, [Op.or]: [ { valid_from: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, { valid_until: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, ], }; } if (filter.predicted_wait_minutesRange) { const [start, end] = filter.predicted_wait_minutesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, predicted_wait_minutes: { ...where.predicted_wait_minutes, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, predicted_wait_minutes: { ...where.predicted_wait_minutes, [Op.lte]: end, }, }; } } if (filter.confidence_scoreRange) { const [start, end] = filter.confidence_scoreRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, confidence_score: { ...where.confidence_score, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, confidence_score: { ...where.confidence_score, [Op.lte]: end, }, }; } } if (filter.valid_fromRange) { const [start, end] = filter.valid_fromRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, valid_from: { ...where.valid_from, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, valid_from: { ...where.valid_from, [Op.lte]: end, }, }; } } if (filter.valid_untilRange) { const [start, end] = filter.valid_untilRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, valid_until: { ...where.valid_until, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, valid_until: { ...where.valid_until, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.source) { where = { ...where, source: filter.source, }; } 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.ai_predictions.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( 'ai_predictions', 'explanation', query, ), ], }; } const records = await db.ai_predictions.findAll({ attributes: [ 'id', 'explanation' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['explanation', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.explanation, })); } };