32262/backend/src/db/api/symbol_analyses.js
2025-06-15 20:49:43 +00:00

522 lines
13 KiB
JavaScript

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 Symbol_analysesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const symbol_analyses = await db.symbol_analyses.create(
{
id: data.id || undefined,
symbol: data.symbol || null,
volatility: data.volatility || null,
volume: data.volume || null,
spread: data.spread || null,
profit_potential: data.profit_potential || null,
trend: data.trend || null,
risk_level: data.risk_level || null,
rsi: data.rsi || null,
macd_signal: data.macd_signal || null,
support: data.support || null,
resistance: data.resistance || null,
last_update: data.last_update || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return symbol_analyses;
}
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 symbol_analysesData = data.map((item, index) => ({
id: item.id || undefined,
symbol: item.symbol || null,
volatility: item.volatility || null,
volume: item.volume || null,
spread: item.spread || null,
profit_potential: item.profit_potential || null,
trend: item.trend || null,
risk_level: item.risk_level || null,
rsi: item.rsi || null,
macd_signal: item.macd_signal || null,
support: item.support || null,
resistance: item.resistance || null,
last_update: item.last_update || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const symbol_analyses = await db.symbol_analyses.bulkCreate(
symbol_analysesData,
{ transaction },
);
// For each item created, replace relation files
return symbol_analyses;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const symbol_analyses = await db.symbol_analyses.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.symbol !== undefined) updatePayload.symbol = data.symbol;
if (data.volatility !== undefined)
updatePayload.volatility = data.volatility;
if (data.volume !== undefined) updatePayload.volume = data.volume;
if (data.spread !== undefined) updatePayload.spread = data.spread;
if (data.profit_potential !== undefined)
updatePayload.profit_potential = data.profit_potential;
if (data.trend !== undefined) updatePayload.trend = data.trend;
if (data.risk_level !== undefined)
updatePayload.risk_level = data.risk_level;
if (data.rsi !== undefined) updatePayload.rsi = data.rsi;
if (data.macd_signal !== undefined)
updatePayload.macd_signal = data.macd_signal;
if (data.support !== undefined) updatePayload.support = data.support;
if (data.resistance !== undefined)
updatePayload.resistance = data.resistance;
if (data.last_update !== undefined)
updatePayload.last_update = data.last_update;
updatePayload.updatedById = currentUser.id;
await symbol_analyses.update(updatePayload, { transaction });
return symbol_analyses;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const symbol_analyses = await db.symbol_analyses.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of symbol_analyses) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of symbol_analyses) {
await record.destroy({ transaction });
}
});
return symbol_analyses;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const symbol_analyses = await db.symbol_analyses.findByPk(id, options);
await symbol_analyses.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await symbol_analyses.destroy({
transaction,
});
return symbol_analyses;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const symbol_analyses = await db.symbol_analyses.findOne(
{ where },
{ transaction },
);
if (!symbol_analyses) {
return symbol_analyses;
}
const output = symbol_analyses.get({ plain: true });
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 = [];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.symbol) {
where = {
...where,
[Op.and]: Utils.ilike('symbol_analyses', 'symbol', filter.symbol),
};
}
if (filter.volatilityRange) {
const [start, end] = filter.volatilityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
volatility: {
...where.volatility,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
volatility: {
...where.volatility,
[Op.lte]: end,
},
};
}
}
if (filter.volumeRange) {
const [start, end] = filter.volumeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
volume: {
...where.volume,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
volume: {
...where.volume,
[Op.lte]: end,
},
};
}
}
if (filter.spreadRange) {
const [start, end] = filter.spreadRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
spread: {
...where.spread,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
spread: {
...where.spread,
[Op.lte]: end,
},
};
}
}
if (filter.profit_potentialRange) {
const [start, end] = filter.profit_potentialRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
profit_potential: {
...where.profit_potential,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
profit_potential: {
...where.profit_potential,
[Op.lte]: end,
},
};
}
}
if (filter.rsiRange) {
const [start, end] = filter.rsiRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
rsi: {
...where.rsi,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
rsi: {
...where.rsi,
[Op.lte]: end,
},
};
}
}
if (filter.supportRange) {
const [start, end] = filter.supportRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
support: {
...where.support,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
support: {
...where.support,
[Op.lte]: end,
},
};
}
}
if (filter.resistanceRange) {
const [start, end] = filter.resistanceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
resistance: {
...where.resistance,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
resistance: {
...where.resistance,
[Op.lte]: end,
},
};
}
}
if (filter.last_updateRange) {
const [start, end] = filter.last_updateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
last_update: {
...where.last_update,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
last_update: {
...where.last_update,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.trend) {
where = {
...where,
trend: filter.trend,
};
}
if (filter.risk_level) {
where = {
...where,
risk_level: filter.risk_level,
};
}
if (filter.macd_signal) {
where = {
...where,
macd_signal: filter.macd_signal,
};
}
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.symbol_analyses.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('symbol_analyses', 'symbol', query),
],
};
}
const records = await db.symbol_analyses.findAll({
attributes: ['id', 'symbol'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['symbol', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.symbol,
}));
}
};