33618/backend/src/db/api/stock_history.js
2025-08-25 14:31:25 +00:00

426 lines
10 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 Stock_historyDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const stock_history = await db.stock_history.create(
{
id: data.id || undefined,
symbol: data.symbol || null,
date: data.date || null,
open: data.open || null,
high: data.high || null,
low: data.low || null,
close: data.close || null,
volume: data.volume || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return stock_history;
}
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 stock_historyData = data.map((item, index) => ({
id: item.id || undefined,
symbol: item.symbol || null,
date: item.date || null,
open: item.open || null,
high: item.high || null,
low: item.low || null,
close: item.close || null,
volume: item.volume || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const stock_history = await db.stock_history.bulkCreate(stock_historyData, {
transaction,
});
// For each item created, replace relation files
return stock_history;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const stock_history = await db.stock_history.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.symbol !== undefined) updatePayload.symbol = data.symbol;
if (data.date !== undefined) updatePayload.date = data.date;
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.volume !== undefined) updatePayload.volume = data.volume;
updatePayload.updatedById = currentUser.id;
await stock_history.update(updatePayload, { transaction });
return stock_history;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const stock_history = await db.stock_history.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of stock_history) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of stock_history) {
await record.destroy({ transaction });
}
});
return stock_history;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const stock_history = await db.stock_history.findByPk(id, options);
await stock_history.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await stock_history.destroy({
transaction,
});
return stock_history;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const stock_history = await db.stock_history.findOne(
{ where },
{ transaction },
);
if (!stock_history) {
return stock_history;
}
const output = stock_history.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('stock_history', 'symbol', filter.symbol),
};
}
if (filter.dateRange) {
const [start, end] = filter.dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
date: {
...where.date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
date: {
...where.date,
[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.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.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.stock_history.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('stock_history', 'symbol', query),
],
};
}
const records = await db.stock_history.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,
}));
}
};