40182-vm/backend/src/db/api/link_replacements.js
2026-06-02 19:41:05 +00:00

462 lines
11 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 Link_replacementsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const link_replacements = await db.link_replacements.create(
{
id: data.id || undefined,
replacement_url: data.replacement_url
||
null
,
replacement_notes: data.replacement_notes
||
null
,
is_applied: data.is_applied
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await link_replacements.setCustomization( data.customization || null, {
transaction,
});
await link_replacements.setAffiliate_link( data.affiliate_link || null, {
transaction,
});
return link_replacements;
}
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 link_replacementsData = data.map((item, index) => ({
id: item.id || undefined,
replacement_url: item.replacement_url
||
null
,
replacement_notes: item.replacement_notes
||
null
,
is_applied: item.is_applied
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const link_replacements = await db.link_replacements.bulkCreate(link_replacementsData, { transaction });
// For each item created, replace relation files
return link_replacements;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const link_replacements = await db.link_replacements.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.replacement_url !== undefined) updatePayload.replacement_url = data.replacement_url;
if (data.replacement_notes !== undefined) updatePayload.replacement_notes = data.replacement_notes;
if (data.is_applied !== undefined) updatePayload.is_applied = data.is_applied;
updatePayload.updatedById = currentUser.id;
await link_replacements.update(updatePayload, {transaction});
if (data.customization !== undefined) {
await link_replacements.setCustomization(
data.customization,
{ transaction }
);
}
if (data.affiliate_link !== undefined) {
await link_replacements.setAffiliate_link(
data.affiliate_link,
{ transaction }
);
}
return link_replacements;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const link_replacements = await db.link_replacements.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of link_replacements) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of link_replacements) {
await record.destroy({transaction});
}
});
return link_replacements;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const link_replacements = await db.link_replacements.findByPk(id, options);
await link_replacements.update({
deletedBy: currentUser.id
}, {
transaction,
});
await link_replacements.destroy({
transaction
});
return link_replacements;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const link_replacements = await db.link_replacements.findOne(
{ where },
{ transaction },
);
if (!link_replacements) {
return link_replacements;
}
const output = link_replacements.get({plain: true});
output.customization = await link_replacements.getCustomization({
transaction
});
output.affiliate_link = await link_replacements.getAffiliate_link({
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.article_customizations,
as: 'customization',
where: filter.customization ? {
[Op.or]: [
{ id: { [Op.in]: filter.customization.split('|').map(term => Utils.uuid(term)) } },
{
custom_title: {
[Op.or]: filter.customization.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.affiliate_links,
as: 'affiliate_link',
where: filter.affiliate_link ? {
[Op.or]: [
{ id: { [Op.in]: filter.affiliate_link.split('|').map(term => Utils.uuid(term)) } },
{
original_url: {
[Op.or]: filter.affiliate_link.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.replacement_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'link_replacements',
'replacement_url',
filter.replacement_url,
),
};
}
if (filter.replacement_notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'link_replacements',
'replacement_notes',
filter.replacement_notes,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.is_applied) {
where = {
...where,
is_applied: filter.is_applied,
};
}
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.link_replacements.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(
'link_replacements',
'replacement_url',
query,
),
],
};
}
const records = await db.link_replacements.findAll({
attributes: [ 'id', 'replacement_url' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['replacement_url', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.replacement_url,
}));
}
};