367 lines
8.6 KiB
JavaScript
367 lines
8.6 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 Auto_reply_rulesDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const auto_reply_rules = await db.auto_reply_rules.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
keywords: data.keywords || null,
|
|
reply_text: data.reply_text || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await auto_reply_rules.setAccount(data.account || null, {
|
|
transaction,
|
|
});
|
|
|
|
await auto_reply_rules.setAccounts(data.accounts || null, {
|
|
transaction,
|
|
});
|
|
|
|
return auto_reply_rules;
|
|
}
|
|
|
|
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 auto_reply_rulesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
keywords: item.keywords || null,
|
|
reply_text: item.reply_text || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const auto_reply_rules = await db.auto_reply_rules.bulkCreate(
|
|
auto_reply_rulesData,
|
|
{ transaction },
|
|
);
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return auto_reply_rules;
|
|
}
|
|
|
|
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 auto_reply_rules = await db.auto_reply_rules.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.keywords !== undefined) updatePayload.keywords = data.keywords;
|
|
|
|
if (data.reply_text !== undefined)
|
|
updatePayload.reply_text = data.reply_text;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await auto_reply_rules.update(updatePayload, { transaction });
|
|
|
|
if (data.account !== undefined) {
|
|
await auto_reply_rules.setAccount(
|
|
data.account,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.accounts !== undefined) {
|
|
await auto_reply_rules.setAccounts(
|
|
data.accounts,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return auto_reply_rules;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const auto_reply_rules = await db.auto_reply_rules.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of auto_reply_rules) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of auto_reply_rules) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return auto_reply_rules;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const auto_reply_rules = await db.auto_reply_rules.findByPk(id, options);
|
|
|
|
await auto_reply_rules.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await auto_reply_rules.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return auto_reply_rules;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const auto_reply_rules = await db.auto_reply_rules.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!auto_reply_rules) {
|
|
return auto_reply_rules;
|
|
}
|
|
|
|
const output = auto_reply_rules.get({ plain: true });
|
|
|
|
output.account = await auto_reply_rules.getAccount({
|
|
transaction,
|
|
});
|
|
|
|
output.accounts = await auto_reply_rules.getAccounts({
|
|
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 userAccounts = (user && user.accounts?.id) || null;
|
|
|
|
if (userAccounts) {
|
|
if (options?.currentUser?.accountsId) {
|
|
where.accountsId = options.currentUser.accountsId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.accounts,
|
|
as: 'account',
|
|
},
|
|
|
|
{
|
|
model: db.accounts,
|
|
as: 'accounts',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.keywords) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'auto_reply_rules',
|
|
'keywords',
|
|
filter.keywords,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.reply_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'auto_reply_rules',
|
|
'reply_text',
|
|
filter.reply_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.account) {
|
|
const listItems = filter.account.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
accountId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
if (filter.accounts) {
|
|
const listItems = filter.accounts.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
accountsId: { [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.accountsId;
|
|
}
|
|
|
|
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.auto_reply_rules.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('auto_reply_rules', 'keywords', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.auto_reply_rules.findAll({
|
|
attributes: ['id', 'keywords'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['keywords', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.keywords,
|
|
}));
|
|
}
|
|
};
|