649 lines
16 KiB
JavaScript
649 lines
16 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 Human_repliesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const human_replies = await db.human_replies.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
slack_user_id: data.slack_user_id
|
|
||
|
|
null
|
|
,
|
|
|
|
slack_user_name: data.slack_user_name
|
|
||
|
|
null
|
|
,
|
|
|
|
reply_text: data.reply_text
|
|
||
|
|
null
|
|
,
|
|
|
|
received_at: data.received_at
|
|
||
|
|
null
|
|
,
|
|
|
|
processing_status: data.processing_status
|
|
||
|
|
null
|
|
,
|
|
|
|
error_message: data.error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await human_replies.setMessage( data.message || null, {
|
|
transaction,
|
|
});
|
|
|
|
await human_replies.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.human_replies.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: human_replies.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.human_replies.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: human_replies.id,
|
|
},
|
|
data.images,
|
|
options,
|
|
);
|
|
|
|
|
|
return human_replies;
|
|
}
|
|
|
|
|
|
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 human_repliesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
slack_user_id: item.slack_user_id
|
|
||
|
|
null
|
|
,
|
|
|
|
slack_user_name: item.slack_user_name
|
|
||
|
|
null
|
|
,
|
|
|
|
reply_text: item.reply_text
|
|
||
|
|
null
|
|
,
|
|
|
|
received_at: item.received_at
|
|
||
|
|
null
|
|
,
|
|
|
|
processing_status: item.processing_status
|
|
||
|
|
null
|
|
,
|
|
|
|
error_message: item.error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const human_replies = await db.human_replies.bulkCreate(human_repliesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < human_replies.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.human_replies.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: human_replies[i].id,
|
|
},
|
|
data[i].attachments,
|
|
options,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < human_replies.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.human_replies.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: human_replies[i].id,
|
|
},
|
|
data[i].images,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return human_replies;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const human_replies = await db.human_replies.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.slack_user_id !== undefined) updatePayload.slack_user_id = data.slack_user_id;
|
|
|
|
|
|
if (data.slack_user_name !== undefined) updatePayload.slack_user_name = data.slack_user_name;
|
|
|
|
|
|
if (data.reply_text !== undefined) updatePayload.reply_text = data.reply_text;
|
|
|
|
|
|
if (data.received_at !== undefined) updatePayload.received_at = data.received_at;
|
|
|
|
|
|
if (data.processing_status !== undefined) updatePayload.processing_status = data.processing_status;
|
|
|
|
|
|
if (data.error_message !== undefined) updatePayload.error_message = data.error_message;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await human_replies.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.message !== undefined) {
|
|
await human_replies.setMessage(
|
|
|
|
data.message,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await human_replies.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.human_replies.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: human_replies.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.human_replies.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: human_replies.id,
|
|
},
|
|
data.images,
|
|
options,
|
|
);
|
|
|
|
|
|
return human_replies;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const human_replies = await db.human_replies.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of human_replies) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of human_replies) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return human_replies;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const human_replies = await db.human_replies.findByPk(id, options);
|
|
|
|
await human_replies.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await human_replies.destroy({
|
|
transaction
|
|
});
|
|
|
|
return human_replies;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const human_replies = await db.human_replies.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!human_replies) {
|
|
return human_replies;
|
|
}
|
|
|
|
const output = human_replies.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.uploads_reply = await human_replies.getUploads_reply({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.message = await human_replies.getMessage({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.attachments = await human_replies.getAttachments({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.images = await human_replies.getImages({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await human_replies.getOrganizations({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.slack_messages,
|
|
as: 'message',
|
|
|
|
where: filter.message ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.message.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
slack_ts: {
|
|
[Op.or]: filter.message.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'attachments',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'images',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.slack_user_id) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'human_replies',
|
|
'slack_user_id',
|
|
filter.slack_user_id,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.slack_user_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'human_replies',
|
|
'slack_user_name',
|
|
filter.slack_user_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.reply_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'human_replies',
|
|
'reply_text',
|
|
filter.reply_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.error_message) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'human_replies',
|
|
'error_message',
|
|
filter.error_message,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.received_atRange) {
|
|
const [start, end] = filter.received_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
received_at: {
|
|
...where.received_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
received_at: {
|
|
...where.received_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.processing_status) {
|
|
where = {
|
|
...where,
|
|
processing_status: filter.processing_status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
|
|
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.human_replies.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, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'human_replies',
|
|
'slack_user_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.human_replies.findAll({
|
|
attributes: [ 'id', 'slack_user_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['slack_user_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.slack_user_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|