38825-vm/backend/src/db/api/recommendation_rules.js
2026-02-28 02:28:10 +00:00

712 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 Recommendation_rulesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recommendation_rules = await db.recommendation_rules.create(
{
id: data.id || undefined,
name: data.name
||
null
,
description: data.description
||
null
,
status: data.status
||
null
,
algorithm: data.algorithm
||
null
,
weight_click: data.weight_click
||
null
,
weight_like: data.weight_like
||
null
,
weight_bookmark: data.weight_bookmark
||
null
,
weight_recency: data.weight_recency
||
null
,
window_days: data.window_days
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await recommendation_rules.setInclude_categories(data.include_categories || [], {
transaction,
});
await recommendation_rules.setInclude_tags(data.include_tags || [], {
transaction,
});
await recommendation_rules.setExclude_tags(data.exclude_tags || [], {
transaction,
});
return recommendation_rules;
}
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 recommendation_rulesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
description: item.description
||
null
,
status: item.status
||
null
,
algorithm: item.algorithm
||
null
,
weight_click: item.weight_click
||
null
,
weight_like: item.weight_like
||
null
,
weight_bookmark: item.weight_bookmark
||
null
,
weight_recency: item.weight_recency
||
null
,
window_days: item.window_days
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const recommendation_rules = await db.recommendation_rules.bulkCreate(recommendation_rulesData, { transaction });
// For each item created, replace relation files
return recommendation_rules;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const recommendation_rules = await db.recommendation_rules.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.description !== undefined) updatePayload.description = data.description;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.algorithm !== undefined) updatePayload.algorithm = data.algorithm;
if (data.weight_click !== undefined) updatePayload.weight_click = data.weight_click;
if (data.weight_like !== undefined) updatePayload.weight_like = data.weight_like;
if (data.weight_bookmark !== undefined) updatePayload.weight_bookmark = data.weight_bookmark;
if (data.weight_recency !== undefined) updatePayload.weight_recency = data.weight_recency;
if (data.window_days !== undefined) updatePayload.window_days = data.window_days;
updatePayload.updatedById = currentUser.id;
await recommendation_rules.update(updatePayload, {transaction});
if (data.include_categories !== undefined) {
await recommendation_rules.setInclude_categories(data.include_categories, { transaction });
}
if (data.include_tags !== undefined) {
await recommendation_rules.setInclude_tags(data.include_tags, { transaction });
}
if (data.exclude_tags !== undefined) {
await recommendation_rules.setExclude_tags(data.exclude_tags, { transaction });
}
return recommendation_rules;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recommendation_rules = await db.recommendation_rules.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of recommendation_rules) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of recommendation_rules) {
await record.destroy({transaction});
}
});
return recommendation_rules;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const recommendation_rules = await db.recommendation_rules.findByPk(id, options);
await recommendation_rules.update({
deletedBy: currentUser.id
}, {
transaction,
});
await recommendation_rules.destroy({
transaction
});
return recommendation_rules;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const recommendation_rules = await db.recommendation_rules.findOne(
{ where },
{ transaction },
);
if (!recommendation_rules) {
return recommendation_rules;
}
const output = recommendation_rules.get({plain: true});
output.include_categories = await recommendation_rules.getInclude_categories({
transaction
});
output.include_tags = await recommendation_rules.getInclude_tags({
transaction
});
output.exclude_tags = await recommendation_rules.getExclude_tags({
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.categories,
as: 'include_categories',
required: false,
},
{
model: db.tags,
as: 'include_tags',
required: false,
},
{
model: db.tags,
as: 'exclude_tags',
required: false,
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'recommendation_rules',
'name',
filter.name,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'recommendation_rules',
'description',
filter.description,
),
};
}
if (filter.weight_clickRange) {
const [start, end] = filter.weight_clickRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
weight_click: {
...where.weight_click,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
weight_click: {
...where.weight_click,
[Op.lte]: end,
},
};
}
}
if (filter.weight_likeRange) {
const [start, end] = filter.weight_likeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
weight_like: {
...where.weight_like,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
weight_like: {
...where.weight_like,
[Op.lte]: end,
},
};
}
}
if (filter.weight_bookmarkRange) {
const [start, end] = filter.weight_bookmarkRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
weight_bookmark: {
...where.weight_bookmark,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
weight_bookmark: {
...where.weight_bookmark,
[Op.lte]: end,
},
};
}
}
if (filter.weight_recencyRange) {
const [start, end] = filter.weight_recencyRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
weight_recency: {
...where.weight_recency,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
weight_recency: {
...where.weight_recency,
[Op.lte]: end,
},
};
}
}
if (filter.window_daysRange) {
const [start, end] = filter.window_daysRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
window_days: {
...where.window_days,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
window_days: {
...where.window_days,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.algorithm) {
where = {
...where,
algorithm: filter.algorithm,
};
}
if (filter.include_categories) {
const searchTerms = filter.include_categories.split('|');
include = [
{
model: db.categories,
as: 'include_categories_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.include_tags) {
const searchTerms = filter.include_tags.split('|');
include = [
{
model: db.tags,
as: 'include_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.exclude_tags) {
const searchTerms = filter.exclude_tags.split('|');
include = [
{
model: db.tags,
as: 'exclude_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.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.recommendation_rules.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(
'recommendation_rules',
'name',
query,
),
],
};
}
const records = await db.recommendation_rules.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,
}));
}
};