1001 lines
26 KiB
JavaScript
1001 lines
26 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 TradesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trades = await db.trades.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title
|
|
||
|
|
null
|
|
,
|
|
|
|
entry_date: data.entry_date
|
|
||
|
|
null
|
|
,
|
|
|
|
exit_date: data.exit_date
|
|
||
|
|
null
|
|
,
|
|
|
|
instrument_name: data.instrument_name
|
|
||
|
|
null
|
|
,
|
|
|
|
direction: data.direction
|
|
||
|
|
null
|
|
,
|
|
|
|
entry_price: data.entry_price
|
|
||
|
|
null
|
|
,
|
|
|
|
stop_loss_pips: data.stop_loss_pips
|
|
||
|
|
null
|
|
,
|
|
|
|
take_profit_pips: data.take_profit_pips
|
|
||
|
|
null
|
|
,
|
|
|
|
risk_percent: data.risk_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
result: data.result
|
|
||
|
|
null
|
|
,
|
|
|
|
profit_r: data.profit_r
|
|
||
|
|
null
|
|
,
|
|
|
|
profit_percent: data.profit_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
profit_amount: data.profit_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
setup_type: data.setup_type
|
|
||
|
|
null
|
|
,
|
|
|
|
source_link: data.source_link
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await trades.setInstrument( data.instrument || null, {
|
|
transaction,
|
|
});
|
|
|
|
await trades.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await trades.setConfluence_tags(data.confluence_tags || [], {
|
|
transaction,
|
|
});
|
|
|
|
await trades.setImages(data.images || [], {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
return trades;
|
|
}
|
|
|
|
|
|
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 tradesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
title: item.title
|
|
||
|
|
null
|
|
,
|
|
|
|
entry_date: item.entry_date
|
|
||
|
|
null
|
|
,
|
|
|
|
exit_date: item.exit_date
|
|
||
|
|
null
|
|
,
|
|
|
|
instrument_name: item.instrument_name
|
|
||
|
|
null
|
|
,
|
|
|
|
direction: item.direction
|
|
||
|
|
null
|
|
,
|
|
|
|
entry_price: item.entry_price
|
|
||
|
|
null
|
|
,
|
|
|
|
stop_loss_pips: item.stop_loss_pips
|
|
||
|
|
null
|
|
,
|
|
|
|
take_profit_pips: item.take_profit_pips
|
|
||
|
|
null
|
|
,
|
|
|
|
risk_percent: item.risk_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
result: item.result
|
|
||
|
|
null
|
|
,
|
|
|
|
profit_r: item.profit_r
|
|
||
|
|
null
|
|
,
|
|
|
|
profit_percent: item.profit_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
profit_amount: item.profit_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
setup_type: item.setup_type
|
|
||
|
|
null
|
|
,
|
|
|
|
source_link: item.source_link
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const trades = await db.trades.bulkCreate(tradesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return trades;
|
|
}
|
|
|
|
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 trades = await db.trades.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
|
|
if (data.entry_date !== undefined) updatePayload.entry_date = data.entry_date;
|
|
|
|
|
|
if (data.exit_date !== undefined) updatePayload.exit_date = data.exit_date;
|
|
|
|
|
|
if (data.instrument_name !== undefined) updatePayload.instrument_name = data.instrument_name;
|
|
|
|
|
|
if (data.direction !== undefined) updatePayload.direction = data.direction;
|
|
|
|
|
|
if (data.entry_price !== undefined) updatePayload.entry_price = data.entry_price;
|
|
|
|
|
|
if (data.stop_loss_pips !== undefined) updatePayload.stop_loss_pips = data.stop_loss_pips;
|
|
|
|
|
|
if (data.take_profit_pips !== undefined) updatePayload.take_profit_pips = data.take_profit_pips;
|
|
|
|
|
|
if (data.risk_percent !== undefined) updatePayload.risk_percent = data.risk_percent;
|
|
|
|
|
|
if (data.result !== undefined) updatePayload.result = data.result;
|
|
|
|
|
|
if (data.profit_r !== undefined) updatePayload.profit_r = data.profit_r;
|
|
|
|
|
|
if (data.profit_percent !== undefined) updatePayload.profit_percent = data.profit_percent;
|
|
|
|
|
|
if (data.profit_amount !== undefined) updatePayload.profit_amount = data.profit_amount;
|
|
|
|
|
|
if (data.setup_type !== undefined) updatePayload.setup_type = data.setup_type;
|
|
|
|
|
|
if (data.source_link !== undefined) updatePayload.source_link = data.source_link;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await trades.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.instrument !== undefined) {
|
|
await trades.setInstrument(
|
|
|
|
data.instrument,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await trades.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.confluence_tags !== undefined) {
|
|
await trades.setConfluence_tags(data.confluence_tags, { transaction });
|
|
}
|
|
|
|
if (data.images !== undefined) {
|
|
await trades.setImages(data.images, { transaction });
|
|
}
|
|
|
|
|
|
|
|
|
|
return trades;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trades = await db.trades.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of trades) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of trades) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return trades;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trades = await db.trades.findByPk(id, options);
|
|
|
|
await trades.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await trades.destroy({
|
|
transaction
|
|
});
|
|
|
|
return trades;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trades = await db.trades.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!trades) {
|
|
return trades;
|
|
}
|
|
|
|
const output = trades.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.gallery_trade = await trades.getGallery_trade({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.instrument = await trades.getInstrument({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.confluence_tags = await trades.getConfluence_tags({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.images = await trades.getImages({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await trades.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.instruments,
|
|
as: 'instrument',
|
|
|
|
where: filter.instrument ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.instrument.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
symbol: {
|
|
[Op.or]: filter.instrument.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
{
|
|
model: db.confluences,
|
|
as: 'confluence_tags',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.gallery,
|
|
as: 'images',
|
|
required: false,
|
|
},
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trades',
|
|
'title',
|
|
filter.title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.instrument_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trades',
|
|
'instrument_name',
|
|
filter.instrument_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.source_link) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trades',
|
|
'source_link',
|
|
filter.source_link,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trades',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
entry_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
exit_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.entry_dateRange) {
|
|
const [start, end] = filter.entry_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
entry_date: {
|
|
...where.entry_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
entry_date: {
|
|
...where.entry_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.exit_dateRange) {
|
|
const [start, end] = filter.exit_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
exit_date: {
|
|
...where.exit_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
exit_date: {
|
|
...where.exit_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.entry_priceRange) {
|
|
const [start, end] = filter.entry_priceRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
entry_price: {
|
|
...where.entry_price,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
entry_price: {
|
|
...where.entry_price,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.stop_loss_pipsRange) {
|
|
const [start, end] = filter.stop_loss_pipsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
stop_loss_pips: {
|
|
...where.stop_loss_pips,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
stop_loss_pips: {
|
|
...where.stop_loss_pips,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.take_profit_pipsRange) {
|
|
const [start, end] = filter.take_profit_pipsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
take_profit_pips: {
|
|
...where.take_profit_pips,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
take_profit_pips: {
|
|
...where.take_profit_pips,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.risk_percentRange) {
|
|
const [start, end] = filter.risk_percentRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
risk_percent: {
|
|
...where.risk_percent,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
risk_percent: {
|
|
...where.risk_percent,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.profit_rRange) {
|
|
const [start, end] = filter.profit_rRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
profit_r: {
|
|
...where.profit_r,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
profit_r: {
|
|
...where.profit_r,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.profit_percentRange) {
|
|
const [start, end] = filter.profit_percentRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
profit_percent: {
|
|
...where.profit_percent,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
profit_percent: {
|
|
...where.profit_percent,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.profit_amountRange) {
|
|
const [start, end] = filter.profit_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
profit_amount: {
|
|
...where.profit_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
profit_amount: {
|
|
...where.profit_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.direction) {
|
|
where = {
|
|
...where,
|
|
direction: filter.direction,
|
|
};
|
|
}
|
|
|
|
if (filter.result) {
|
|
where = {
|
|
...where,
|
|
result: filter.result,
|
|
};
|
|
}
|
|
|
|
if (filter.setup_type) {
|
|
where = {
|
|
...where,
|
|
setup_type: filter.setup_type,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.confluence_tags) {
|
|
const searchTerms = filter.confluence_tags.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.confluences,
|
|
as: 'confluence_tags_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
if (filter.images) {
|
|
const searchTerms = filter.images.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.gallery,
|
|
as: 'images_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
|
|
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.trades.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(
|
|
'trades',
|
|
'title',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.trades.findAll({
|
|
attributes: [ 'id', 'title' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.title,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|