527 lines
13 KiB
JavaScript
527 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 Data_providersDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const data_providers = await db.data_providers.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name
|
|
||
|
|
null
|
|
,
|
|
|
|
base_url: data.base_url
|
|
||
|
|
null
|
|
,
|
|
|
|
provider_type: data.provider_type
|
|
||
|
|
null
|
|
,
|
|
|
|
auth_header_name: data.auth_header_name
|
|
||
|
|
null
|
|
,
|
|
|
|
auth_token: data.auth_token
|
|
||
|
|
null
|
|
,
|
|
|
|
is_enabled: data.is_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
rate_limit_per_minute: data.rate_limit_per_minute
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data_providers;
|
|
}
|
|
|
|
|
|
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 data_providersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name
|
|
||
|
|
null
|
|
,
|
|
|
|
base_url: item.base_url
|
|
||
|
|
null
|
|
,
|
|
|
|
provider_type: item.provider_type
|
|
||
|
|
null
|
|
,
|
|
|
|
auth_header_name: item.auth_header_name
|
|
||
|
|
null
|
|
,
|
|
|
|
auth_token: item.auth_token
|
|
||
|
|
null
|
|
,
|
|
|
|
is_enabled: item.is_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
rate_limit_per_minute: item.rate_limit_per_minute
|
|
||
|
|
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 data_providers = await db.data_providers.bulkCreate(data_providersData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return data_providers;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const data_providers = await db.data_providers.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
|
|
if (data.base_url !== undefined) updatePayload.base_url = data.base_url;
|
|
|
|
|
|
if (data.provider_type !== undefined) updatePayload.provider_type = data.provider_type;
|
|
|
|
|
|
if (data.auth_header_name !== undefined) updatePayload.auth_header_name = data.auth_header_name;
|
|
|
|
|
|
if (data.auth_token !== undefined) updatePayload.auth_token = data.auth_token;
|
|
|
|
|
|
if (data.is_enabled !== undefined) updatePayload.is_enabled = data.is_enabled;
|
|
|
|
|
|
if (data.rate_limit_per_minute !== undefined) updatePayload.rate_limit_per_minute = data.rate_limit_per_minute;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await data_providers.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data_providers;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const data_providers = await db.data_providers.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of data_providers) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of data_providers) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return data_providers;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const data_providers = await db.data_providers.findByPk(id, options);
|
|
|
|
await data_providers.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await data_providers.destroy({
|
|
transaction
|
|
});
|
|
|
|
return data_providers;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const data_providers = await db.data_providers.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!data_providers) {
|
|
return data_providers;
|
|
}
|
|
|
|
const output = data_providers.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.assets_data_provider = await data_providers.getAssets_data_provider({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.asset_quotes_data_provider = await data_providers.getAsset_quotes_data_provider({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.sync_jobs_data_provider = await data_providers.getSync_jobs_data_provider({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
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.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'data_providers',
|
|
'name',
|
|
filter.name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.base_url) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'data_providers',
|
|
'base_url',
|
|
filter.base_url,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.auth_header_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'data_providers',
|
|
'auth_header_name',
|
|
filter.auth_header_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.auth_token) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'data_providers',
|
|
'auth_token',
|
|
filter.auth_token,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'data_providers',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.rate_limit_per_minuteRange) {
|
|
const [start, end] = filter.rate_limit_per_minuteRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
rate_limit_per_minute: {
|
|
...where.rate_limit_per_minute,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
rate_limit_per_minute: {
|
|
...where.rate_limit_per_minute,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.provider_type) {
|
|
where = {
|
|
...where,
|
|
provider_type: filter.provider_type,
|
|
};
|
|
}
|
|
|
|
if (filter.is_enabled) {
|
|
where = {
|
|
...where,
|
|
is_enabled: filter.is_enabled,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.data_providers.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(
|
|
'data_providers',
|
|
'name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.data_providers.findAll({
|
|
attributes: [ 'id', 'name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|