362 lines
8.9 KiB
JavaScript
362 lines
8.9 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 Trading_signalsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trading_signals = await db.trading_signals.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
signal_type: data.signal_type || null,
|
|
instrument: data.instrument || null,
|
|
target_price: data.target_price || null,
|
|
stop_loss: data.stop_loss || null,
|
|
signal_date: data.signal_date || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
return trading_signals;
|
|
}
|
|
|
|
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 trading_signalsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
signal_type: item.signal_type || null,
|
|
instrument: item.instrument || null,
|
|
target_price: item.target_price || null,
|
|
stop_loss: item.stop_loss || null,
|
|
signal_date: item.signal_date || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const trading_signals = await db.trading_signals.bulkCreate(
|
|
trading_signalsData,
|
|
{ transaction },
|
|
);
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return trading_signals;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trading_signals = await db.trading_signals.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.signal_type !== undefined)
|
|
updatePayload.signal_type = data.signal_type;
|
|
|
|
if (data.instrument !== undefined)
|
|
updatePayload.instrument = data.instrument;
|
|
|
|
if (data.target_price !== undefined)
|
|
updatePayload.target_price = data.target_price;
|
|
|
|
if (data.stop_loss !== undefined) updatePayload.stop_loss = data.stop_loss;
|
|
|
|
if (data.signal_date !== undefined)
|
|
updatePayload.signal_date = data.signal_date;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await trading_signals.update(updatePayload, { transaction });
|
|
|
|
return trading_signals;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trading_signals = await db.trading_signals.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of trading_signals) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of trading_signals) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return trading_signals;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trading_signals = await db.trading_signals.findByPk(id, options);
|
|
|
|
await trading_signals.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await trading_signals.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return trading_signals;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trading_signals = await db.trading_signals.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!trading_signals) {
|
|
return trading_signals;
|
|
}
|
|
|
|
const output = trading_signals.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.instrument) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trading_signals',
|
|
'instrument',
|
|
filter.instrument,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.target_priceRange) {
|
|
const [start, end] = filter.target_priceRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
target_price: {
|
|
...where.target_price,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
target_price: {
|
|
...where.target_price,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.stop_lossRange) {
|
|
const [start, end] = filter.stop_lossRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
stop_loss: {
|
|
...where.stop_loss,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
stop_loss: {
|
|
...where.stop_loss,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.signal_dateRange) {
|
|
const [start, end] = filter.signal_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
signal_date: {
|
|
...where.signal_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
signal_date: {
|
|
...where.signal_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.signal_type) {
|
|
where = {
|
|
...where,
|
|
signal_type: filter.signal_type,
|
|
};
|
|
}
|
|
|
|
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.trading_signals.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('trading_signals', 'instrument', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.trading_signals.findAll({
|
|
attributes: ['id', 'instrument'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['instrument', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.instrument,
|
|
}));
|
|
}
|
|
};
|