38882-vm/backend/src/db/api/market_data_snapshots.js
2026-02-28 18:50:04 +00:00

757 lines
19 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 Market_data_snapshotsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const market_data_snapshots = await db.market_data_snapshots.create(
{
id: data.id || undefined,
as_of_at: data.as_of_at
||
null
,
open: data.open
||
null
,
high: data.high
||
null
,
low: data.low
||
null
,
close: data.close
||
null
,
adj_close: data.adj_close
||
null
,
volume: data.volume
||
null
,
interval: data.interval
||
null
,
source: data.source
||
null
,
is_validated: data.is_validated
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await market_data_snapshots.setAsset( data.asset || null, {
transaction,
});
await market_data_snapshots.setOrganizations( data.organizations || null, {
transaction,
});
return market_data_snapshots;
}
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 market_data_snapshotsData = data.map((item, index) => ({
id: item.id || undefined,
as_of_at: item.as_of_at
||
null
,
open: item.open
||
null
,
high: item.high
||
null
,
low: item.low
||
null
,
close: item.close
||
null
,
adj_close: item.adj_close
||
null
,
volume: item.volume
||
null
,
interval: item.interval
||
null
,
source: item.source
||
null
,
is_validated: item.is_validated
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const market_data_snapshots = await db.market_data_snapshots.bulkCreate(market_data_snapshotsData, { transaction });
// For each item created, replace relation files
return market_data_snapshots;
}
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 market_data_snapshots = await db.market_data_snapshots.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.as_of_at !== undefined) updatePayload.as_of_at = data.as_of_at;
if (data.open !== undefined) updatePayload.open = data.open;
if (data.high !== undefined) updatePayload.high = data.high;
if (data.low !== undefined) updatePayload.low = data.low;
if (data.close !== undefined) updatePayload.close = data.close;
if (data.adj_close !== undefined) updatePayload.adj_close = data.adj_close;
if (data.volume !== undefined) updatePayload.volume = data.volume;
if (data.interval !== undefined) updatePayload.interval = data.interval;
if (data.source !== undefined) updatePayload.source = data.source;
if (data.is_validated !== undefined) updatePayload.is_validated = data.is_validated;
updatePayload.updatedById = currentUser.id;
await market_data_snapshots.update(updatePayload, {transaction});
if (data.asset !== undefined) {
await market_data_snapshots.setAsset(
data.asset,
{ transaction }
);
}
if (data.organizations !== undefined) {
await market_data_snapshots.setOrganizations(
data.organizations,
{ transaction }
);
}
return market_data_snapshots;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const market_data_snapshots = await db.market_data_snapshots.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of market_data_snapshots) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of market_data_snapshots) {
await record.destroy({transaction});
}
});
return market_data_snapshots;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const market_data_snapshots = await db.market_data_snapshots.findByPk(id, options);
await market_data_snapshots.update({
deletedBy: currentUser.id
}, {
transaction,
});
await market_data_snapshots.destroy({
transaction
});
return market_data_snapshots;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const market_data_snapshots = await db.market_data_snapshots.findOne(
{ where },
{ transaction },
);
if (!market_data_snapshots) {
return market_data_snapshots;
}
const output = market_data_snapshots.get({plain: true});
output.asset = await market_data_snapshots.getAsset({
transaction
});
output.organizations = await market_data_snapshots.getOrganizations({
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 userOrganizations = (user && user.organizations?.id) || null;
if (userOrganizations) {
if (options?.currentUser?.organizationsId) {
where.organizationsId = options.currentUser.organizationsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.assets,
as: 'asset',
where: filter.asset ? {
[Op.or]: [
{ id: { [Op.in]: filter.asset.split('|').map(term => Utils.uuid(term)) } },
{
symbol: {
[Op.or]: filter.asset.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.organizations,
as: 'organizations',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.source) {
where = {
...where,
[Op.and]: Utils.ilike(
'market_data_snapshots',
'source',
filter.source,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
as_of_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
as_of_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.as_of_atRange) {
const [start, end] = filter.as_of_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
as_of_at: {
...where.as_of_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
as_of_at: {
...where.as_of_at,
[Op.lte]: end,
},
};
}
}
if (filter.openRange) {
const [start, end] = filter.openRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
open: {
...where.open,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
open: {
...where.open,
[Op.lte]: end,
},
};
}
}
if (filter.highRange) {
const [start, end] = filter.highRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
high: {
...where.high,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
high: {
...where.high,
[Op.lte]: end,
},
};
}
}
if (filter.lowRange) {
const [start, end] = filter.lowRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
low: {
...where.low,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
low: {
...where.low,
[Op.lte]: end,
},
};
}
}
if (filter.closeRange) {
const [start, end] = filter.closeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
close: {
...where.close,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
close: {
...where.close,
[Op.lte]: end,
},
};
}
}
if (filter.adj_closeRange) {
const [start, end] = filter.adj_closeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
adj_close: {
...where.adj_close,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
adj_close: {
...where.adj_close,
[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.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.interval) {
where = {
...where,
interval: filter.interval,
};
}
if (filter.is_validated) {
where = {
...where,
is_validated: filter.is_validated,
};
}
if (filter.organizations) {
const listItems = filter.organizations.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationsId: {[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.organizationsId;
}
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.market_data_snapshots.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(
'market_data_snapshots',
'source',
query,
),
],
};
}
const records = await db.market_data_snapshots.findAll({
attributes: [ 'id', 'source' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['source', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.source,
}));
}
};