37772-vm/backend/src/db/api/mentions.js
2026-01-24 09:31:30 +00:00

507 lines
12 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 MentionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const mentions = await db.mentions.create(
{
id: data.id || undefined,
excerpt: data.excerpt
||
null
,
start_index: data.start_index
||
null
,
end_index: data.end_index
||
null
,
context: data.context
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await mentions.setDocument( data.document || null, {
transaction,
});
await mentions.setTheme( data.theme || null, {
transaction,
});
return mentions;
}
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 mentionsData = data.map((item, index) => ({
id: item.id || undefined,
excerpt: item.excerpt
||
null
,
start_index: item.start_index
||
null
,
end_index: item.end_index
||
null
,
context: item.context
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const mentions = await db.mentions.bulkCreate(mentionsData, { transaction });
// For each item created, replace relation files
return mentions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const mentions = await db.mentions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.excerpt !== undefined) updatePayload.excerpt = data.excerpt;
if (data.start_index !== undefined) updatePayload.start_index = data.start_index;
if (data.end_index !== undefined) updatePayload.end_index = data.end_index;
if (data.context !== undefined) updatePayload.context = data.context;
updatePayload.updatedById = currentUser.id;
await mentions.update(updatePayload, {transaction});
if (data.document !== undefined) {
await mentions.setDocument(
data.document,
{ transaction }
);
}
if (data.theme !== undefined) {
await mentions.setTheme(
data.theme,
{ transaction }
);
}
return mentions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const mentions = await db.mentions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of mentions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of mentions) {
await record.destroy({transaction});
}
});
return mentions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const mentions = await db.mentions.findByPk(id, options);
await mentions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await mentions.destroy({
transaction
});
return mentions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const mentions = await db.mentions.findOne(
{ where },
{ transaction },
);
if (!mentions) {
return mentions;
}
const output = mentions.get({plain: true});
output.document = await mentions.getDocument({
transaction
});
output.theme = await mentions.getTheme({
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.documents,
as: 'document',
where: filter.document ? {
[Op.or]: [
{ id: { [Op.in]: filter.document.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.document.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.themes,
as: 'theme',
where: filter.theme ? {
[Op.or]: [
{ id: { [Op.in]: filter.theme.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.theme.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.excerpt) {
where = {
...where,
[Op.and]: Utils.ilike(
'mentions',
'excerpt',
filter.excerpt,
),
};
}
if (filter.context) {
where = {
...where,
[Op.and]: Utils.ilike(
'mentions',
'context',
filter.context,
),
};
}
if (filter.start_indexRange) {
const [start, end] = filter.start_indexRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
start_index: {
...where.start_index,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
start_index: {
...where.start_index,
[Op.lte]: end,
},
};
}
}
if (filter.end_indexRange) {
const [start, end] = filter.end_indexRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
end_index: {
...where.end_index,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
end_index: {
...where.end_index,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
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.mentions.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(
'mentions',
'excerpt',
query,
),
],
};
}
const records = await db.mentions.findAll({
attributes: [ 'id', 'excerpt' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['excerpt', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.excerpt,
}));
}
};