39442-vm/backend/src/db/api/comments.js
2026-04-03 00:57:22 +00:00

540 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 CommentsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const comments = await db.comments.create(
{
id: data.id || undefined,
comment_text: data.comment_text
||
null
,
moderation_status: data.moderation_status
||
null
,
moderated_at: data.moderated_at
||
null
,
is_deleted: data.is_deleted
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await comments.setPost( data.post || null, {
transaction,
});
await comments.setAuthor_user( data.author_user || null, {
transaction,
});
await comments.setModerated_by_user( data.moderated_by_user || null, {
transaction,
});
return comments;
}
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 commentsData = data.map((item, index) => ({
id: item.id || undefined,
comment_text: item.comment_text
||
null
,
moderation_status: item.moderation_status
||
null
,
moderated_at: item.moderated_at
||
null
,
is_deleted: item.is_deleted
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const comments = await db.comments.bulkCreate(commentsData, { transaction });
// For each item created, replace relation files
return comments;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const comments = await db.comments.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.comment_text !== undefined) updatePayload.comment_text = data.comment_text;
if (data.moderation_status !== undefined) updatePayload.moderation_status = data.moderation_status;
if (data.moderated_at !== undefined) updatePayload.moderated_at = data.moderated_at;
if (data.is_deleted !== undefined) updatePayload.is_deleted = data.is_deleted;
updatePayload.updatedById = currentUser.id;
await comments.update(updatePayload, {transaction});
if (data.post !== undefined) {
await comments.setPost(
data.post,
{ transaction }
);
}
if (data.author_user !== undefined) {
await comments.setAuthor_user(
data.author_user,
{ transaction }
);
}
if (data.moderated_by_user !== undefined) {
await comments.setModerated_by_user(
data.moderated_by_user,
{ transaction }
);
}
return comments;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const comments = await db.comments.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of comments) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of comments) {
await record.destroy({transaction});
}
});
return comments;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const comments = await db.comments.findByPk(id, options);
await comments.update({
deletedBy: currentUser.id
}, {
transaction,
});
await comments.destroy({
transaction
});
return comments;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const comments = await db.comments.findOne(
{ where },
{ transaction },
);
if (!comments) {
return comments;
}
const output = comments.get({plain: true});
output.reactions_comment = await comments.getReactions_comment({
transaction
});
output.moderation_reports_reported_comment = await comments.getModeration_reports_reported_comment({
transaction
});
output.post = await comments.getPost({
transaction
});
output.author_user = await comments.getAuthor_user({
transaction
});
output.moderated_by_user = await comments.getModerated_by_user({
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.posts,
as: 'post',
where: filter.post ? {
[Op.or]: [
{ id: { [Op.in]: filter.post.split('|').map(term => Utils.uuid(term)) } },
{
caption: {
[Op.or]: filter.post.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'author_user',
where: filter.author_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.author_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.author_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'moderated_by_user',
where: filter.moderated_by_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.moderated_by_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.moderated_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.comment_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'comments',
'comment_text',
filter.comment_text,
),
};
}
if (filter.moderated_atRange) {
const [start, end] = filter.moderated_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
moderated_at: {
...where.moderated_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
moderated_at: {
...where.moderated_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.moderation_status) {
where = {
...where,
moderation_status: filter.moderation_status,
};
}
if (filter.is_deleted) {
where = {
...where,
is_deleted: filter.is_deleted,
};
}
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.comments.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(
'comments',
'comment_text',
query,
),
],
};
}
const records = await db.comments.findAll({
attributes: [ 'id', 'comment_text' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['comment_text', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.comment_text,
}));
}
};