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 Scenario_resultsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const scenario_results = await db.scenario_results.create( { id: data.id || undefined, delta_point_estimate: data.delta_point_estimate || null , delta_p50: data.delta_p50 || null , delta_volatility: data.delta_volatility || null , pnl_proxy: data.pnl_proxy || null , narrative: data.narrative || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await scenario_results.setScenario( data.scenario || null, { transaction, }); await scenario_results.setForecast( data.forecast || null, { transaction, }); return scenario_results; } 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 scenario_resultsData = data.map((item, index) => ({ id: item.id || undefined, delta_point_estimate: item.delta_point_estimate || null , delta_p50: item.delta_p50 || null , delta_volatility: item.delta_volatility || null , pnl_proxy: item.pnl_proxy || null , narrative: item.narrative || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const scenario_results = await db.scenario_results.bulkCreate(scenario_resultsData, { transaction }); // For each item created, replace relation files return scenario_results; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const scenario_results = await db.scenario_results.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.delta_point_estimate !== undefined) updatePayload.delta_point_estimate = data.delta_point_estimate; if (data.delta_p50 !== undefined) updatePayload.delta_p50 = data.delta_p50; if (data.delta_volatility !== undefined) updatePayload.delta_volatility = data.delta_volatility; if (data.pnl_proxy !== undefined) updatePayload.pnl_proxy = data.pnl_proxy; if (data.narrative !== undefined) updatePayload.narrative = data.narrative; updatePayload.updatedById = currentUser.id; await scenario_results.update(updatePayload, {transaction}); if (data.scenario !== undefined) { await scenario_results.setScenario( data.scenario, { transaction } ); } if (data.forecast !== undefined) { await scenario_results.setForecast( data.forecast, { transaction } ); } return scenario_results; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const scenario_results = await db.scenario_results.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of scenario_results) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of scenario_results) { await record.destroy({transaction}); } }); return scenario_results; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const scenario_results = await db.scenario_results.findByPk(id, options); await scenario_results.update({ deletedBy: currentUser.id }, { transaction, }); await scenario_results.destroy({ transaction }); return scenario_results; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const scenario_results = await db.scenario_results.findOne( { where }, { transaction }, ); if (!scenario_results) { return scenario_results; } const output = scenario_results.get({plain: true}); output.scenario = await scenario_results.getScenario({ transaction }); output.forecast = await scenario_results.getForecast({ 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.scenarios, as: 'scenario', where: filter.scenario ? { [Op.or]: [ { id: { [Op.in]: filter.scenario.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.scenario.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.forecasts, as: 'forecast', where: filter.forecast ? { [Op.or]: [ { id: { [Op.in]: filter.forecast.split('|').map(term => Utils.uuid(term)) } }, { horizon: { [Op.or]: filter.forecast.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.narrative) { where = { ...where, [Op.and]: Utils.ilike( 'scenario_results', 'narrative', filter.narrative, ), }; } if (filter.delta_point_estimateRange) { const [start, end] = filter.delta_point_estimateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, delta_point_estimate: { ...where.delta_point_estimate, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, delta_point_estimate: { ...where.delta_point_estimate, [Op.lte]: end, }, }; } } if (filter.delta_p50Range) { const [start, end] = filter.delta_p50Range; if (start !== undefined && start !== null && start !== '') { where = { ...where, delta_p50: { ...where.delta_p50, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, delta_p50: { ...where.delta_p50, [Op.lte]: end, }, }; } } if (filter.delta_volatilityRange) { const [start, end] = filter.delta_volatilityRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, delta_volatility: { ...where.delta_volatility, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, delta_volatility: { ...where.delta_volatility, [Op.lte]: end, }, }; } } if (filter.pnl_proxyRange) { const [start, end] = filter.pnl_proxyRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, pnl_proxy: { ...where.pnl_proxy, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, pnl_proxy: { ...where.pnl_proxy, [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.scenario_results.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( 'scenario_results', 'pnl_proxy', query, ), ], }; } const records = await db.scenario_results.findAll({ attributes: [ 'id', 'pnl_proxy' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['pnl_proxy', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.pnl_proxy, })); } };