540 lines
13 KiB
JavaScript
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,
|
|
|
|
target_type: data.target_type
|
|
||
|
|
null
|
|
,
|
|
|
|
target_ref: data.target_ref
|
|
||
|
|
null
|
|
,
|
|
|
|
commented_datetime: data.commented_datetime
|
|
||
|
|
null
|
|
,
|
|
|
|
body: data.body
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await comments.setEvent( data.event || null, {
|
|
transaction,
|
|
});
|
|
|
|
await comments.setAuthor( data.author || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.comments.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: comments.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
|
|
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,
|
|
|
|
target_type: item.target_type
|
|
||
|
|
null
|
|
,
|
|
|
|
target_ref: item.target_ref
|
|
||
|
|
null
|
|
,
|
|
|
|
commented_datetime: item.commented_datetime
|
|
||
|
|
null
|
|
,
|
|
|
|
body: item.body
|
|
||
|
|
null
|
|
,
|
|
|
|
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
|
|
|
|
for (let i = 0; i < comments.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.comments.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: comments[i].id,
|
|
},
|
|
data[i].attachments,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
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.target_type !== undefined) updatePayload.target_type = data.target_type;
|
|
|
|
|
|
if (data.target_ref !== undefined) updatePayload.target_ref = data.target_ref;
|
|
|
|
|
|
if (data.commented_datetime !== undefined) updatePayload.commented_datetime = data.commented_datetime;
|
|
|
|
|
|
if (data.body !== undefined) updatePayload.body = data.body;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await comments.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.event !== undefined) {
|
|
await comments.setEvent(
|
|
|
|
data.event,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.author !== undefined) {
|
|
await comments.setAuthor(
|
|
|
|
data.author,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.comments.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: comments.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
|
|
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.event = await comments.getEvent({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.author = await comments.getAuthor({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.attachments = await comments.getAttachments({
|
|
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.events,
|
|
as: 'event',
|
|
|
|
where: filter.event ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.event.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.event.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'author',
|
|
|
|
where: filter.author ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.author.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.author.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'attachments',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.target_ref) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'comments',
|
|
'target_ref',
|
|
filter.target_ref,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.body) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'comments',
|
|
'body',
|
|
filter.body,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.commented_datetimeRange) {
|
|
const [start, end] = filter.commented_datetimeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
commented_datetime: {
|
|
...where.commented_datetime,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
commented_datetime: {
|
|
...where.commented_datetime,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.target_type) {
|
|
where = {
|
|
...where,
|
|
target_type: filter.target_type,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
'target_ref',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.comments.findAll({
|
|
attributes: [ 'id', 'target_ref' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['target_ref', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.target_ref,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|