682 lines
18 KiB
JavaScript
682 lines
18 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 App_settingsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const app_settings = await db.app_settings.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
default_language: data.default_language
|
|
||
|
|
null
|
|
,
|
|
|
|
buy_license_telegram_link: data.buy_license_telegram_link
|
|
||
|
|
null
|
|
,
|
|
|
|
auto_create_folders: data.auto_create_folders
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
default_download_root: data.default_download_root
|
|
||
|
|
null
|
|
,
|
|
|
|
default_parallel_downloads: data.default_parallel_downloads
|
|
||
|
|
null
|
|
,
|
|
|
|
show_real_time_speed: data.show_real_time_speed
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
enable_sound_notifications: data.enable_sound_notifications
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
enable_url_auto_capture: data.enable_url_auto_capture
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
heartbeat_interval_seconds: data.heartbeat_interval_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
max_clock_skew_seconds: data.max_clock_skew_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
block_on_time_tamper: data.block_on_time_tamper
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await app_settings.setProduct( data.product || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.app_settings.getTableName(),
|
|
belongsToColumn: 'sound_pack_files',
|
|
belongsToId: app_settings.id,
|
|
},
|
|
data.sound_pack_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return app_settings;
|
|
}
|
|
|
|
|
|
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 app_settingsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
default_language: item.default_language
|
|
||
|
|
null
|
|
,
|
|
|
|
buy_license_telegram_link: item.buy_license_telegram_link
|
|
||
|
|
null
|
|
,
|
|
|
|
auto_create_folders: item.auto_create_folders
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
default_download_root: item.default_download_root
|
|
||
|
|
null
|
|
,
|
|
|
|
default_parallel_downloads: item.default_parallel_downloads
|
|
||
|
|
null
|
|
,
|
|
|
|
show_real_time_speed: item.show_real_time_speed
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
enable_sound_notifications: item.enable_sound_notifications
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
enable_url_auto_capture: item.enable_url_auto_capture
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
heartbeat_interval_seconds: item.heartbeat_interval_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
max_clock_skew_seconds: item.max_clock_skew_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
block_on_time_tamper: item.block_on_time_tamper
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const app_settings = await db.app_settings.bulkCreate(app_settingsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < app_settings.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.app_settings.getTableName(),
|
|
belongsToColumn: 'sound_pack_files',
|
|
belongsToId: app_settings[i].id,
|
|
},
|
|
data[i].sound_pack_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return app_settings;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const app_settings = await db.app_settings.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.default_language !== undefined) updatePayload.default_language = data.default_language;
|
|
|
|
|
|
if (data.buy_license_telegram_link !== undefined) updatePayload.buy_license_telegram_link = data.buy_license_telegram_link;
|
|
|
|
|
|
if (data.auto_create_folders !== undefined) updatePayload.auto_create_folders = data.auto_create_folders;
|
|
|
|
|
|
if (data.default_download_root !== undefined) updatePayload.default_download_root = data.default_download_root;
|
|
|
|
|
|
if (data.default_parallel_downloads !== undefined) updatePayload.default_parallel_downloads = data.default_parallel_downloads;
|
|
|
|
|
|
if (data.show_real_time_speed !== undefined) updatePayload.show_real_time_speed = data.show_real_time_speed;
|
|
|
|
|
|
if (data.enable_sound_notifications !== undefined) updatePayload.enable_sound_notifications = data.enable_sound_notifications;
|
|
|
|
|
|
if (data.enable_url_auto_capture !== undefined) updatePayload.enable_url_auto_capture = data.enable_url_auto_capture;
|
|
|
|
|
|
if (data.heartbeat_interval_seconds !== undefined) updatePayload.heartbeat_interval_seconds = data.heartbeat_interval_seconds;
|
|
|
|
|
|
if (data.max_clock_skew_seconds !== undefined) updatePayload.max_clock_skew_seconds = data.max_clock_skew_seconds;
|
|
|
|
|
|
if (data.block_on_time_tamper !== undefined) updatePayload.block_on_time_tamper = data.block_on_time_tamper;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await app_settings.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.product !== undefined) {
|
|
await app_settings.setProduct(
|
|
|
|
data.product,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.app_settings.getTableName(),
|
|
belongsToColumn: 'sound_pack_files',
|
|
belongsToId: app_settings.id,
|
|
},
|
|
data.sound_pack_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return app_settings;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const app_settings = await db.app_settings.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of app_settings) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of app_settings) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return app_settings;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const app_settings = await db.app_settings.findByPk(id, options);
|
|
|
|
await app_settings.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await app_settings.destroy({
|
|
transaction
|
|
});
|
|
|
|
return app_settings;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const app_settings = await db.app_settings.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!app_settings) {
|
|
return app_settings;
|
|
}
|
|
|
|
const output = app_settings.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.product = await app_settings.getProduct({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.sound_pack_files = await app_settings.getSound_pack_files({
|
|
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 = [
|
|
|
|
{
|
|
model: db.products,
|
|
as: 'product',
|
|
|
|
where: filter.product ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.product.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.product.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'sound_pack_files',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.buy_license_telegram_link) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'app_settings',
|
|
'buy_license_telegram_link',
|
|
filter.buy_license_telegram_link,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.default_download_root) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'app_settings',
|
|
'default_download_root',
|
|
filter.default_download_root,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.default_parallel_downloadsRange) {
|
|
const [start, end] = filter.default_parallel_downloadsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
default_parallel_downloads: {
|
|
...where.default_parallel_downloads,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
default_parallel_downloads: {
|
|
...where.default_parallel_downloads,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.heartbeat_interval_secondsRange) {
|
|
const [start, end] = filter.heartbeat_interval_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
heartbeat_interval_seconds: {
|
|
...where.heartbeat_interval_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
heartbeat_interval_seconds: {
|
|
...where.heartbeat_interval_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.max_clock_skew_secondsRange) {
|
|
const [start, end] = filter.max_clock_skew_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
max_clock_skew_seconds: {
|
|
...where.max_clock_skew_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
max_clock_skew_seconds: {
|
|
...where.max_clock_skew_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.default_language) {
|
|
where = {
|
|
...where,
|
|
default_language: filter.default_language,
|
|
};
|
|
}
|
|
|
|
if (filter.auto_create_folders) {
|
|
where = {
|
|
...where,
|
|
auto_create_folders: filter.auto_create_folders,
|
|
};
|
|
}
|
|
|
|
if (filter.show_real_time_speed) {
|
|
where = {
|
|
...where,
|
|
show_real_time_speed: filter.show_real_time_speed,
|
|
};
|
|
}
|
|
|
|
if (filter.enable_sound_notifications) {
|
|
where = {
|
|
...where,
|
|
enable_sound_notifications: filter.enable_sound_notifications,
|
|
};
|
|
}
|
|
|
|
if (filter.enable_url_auto_capture) {
|
|
where = {
|
|
...where,
|
|
enable_url_auto_capture: filter.enable_url_auto_capture,
|
|
};
|
|
}
|
|
|
|
if (filter.block_on_time_tamper) {
|
|
where = {
|
|
...where,
|
|
block_on_time_tamper: filter.block_on_time_tamper,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.app_settings.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(
|
|
'app_settings',
|
|
'default_download_root',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.app_settings.findAll({
|
|
attributes: [ 'id', 'default_download_root' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['default_download_root', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.default_download_root,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|