662 lines
17 KiB
JavaScript
662 lines
17 KiB
JavaScript
|
|
const db = require('../models');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class Ai_agent_settingsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ai_agent_settings = await db.ai_agent_settings.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
auto_generate_plan: data.auto_generate_plan
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
posts_per_week: data.posts_per_week
|
|
||
|
|
null
|
|
,
|
|
|
|
stories_per_week: data.stories_per_week
|
|
||
|
|
null
|
|
,
|
|
|
|
reels_per_week: data.reels_per_week
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_language: data.primary_language
|
|
||
|
|
null
|
|
,
|
|
|
|
forbidden_topics: data.forbidden_topics
|
|
||
|
|
null
|
|
,
|
|
|
|
required_phrases: data.required_phrases
|
|
||
|
|
null
|
|
,
|
|
|
|
cta_templates: data.cta_templates
|
|
||
|
|
null
|
|
,
|
|
|
|
auto_suggest_hashtags: data.auto_suggest_hashtags
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
auto_reply_enabled: data.auto_reply_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
auto_reply_rules: data.auto_reply_rules
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await ai_agent_settings.setOrganization(currentUser.organization?.id || currentUser.organizations?.id || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ai_agent_settings;
|
|
}
|
|
|
|
|
|
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 ai_agent_settingsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
auto_generate_plan: item.auto_generate_plan
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
posts_per_week: item.posts_per_week
|
|
||
|
|
null
|
|
,
|
|
|
|
stories_per_week: item.stories_per_week
|
|
||
|
|
null
|
|
,
|
|
|
|
reels_per_week: item.reels_per_week
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_language: item.primary_language
|
|
||
|
|
null
|
|
,
|
|
|
|
forbidden_topics: item.forbidden_topics
|
|
||
|
|
null
|
|
,
|
|
|
|
required_phrases: item.required_phrases
|
|
||
|
|
null
|
|
,
|
|
|
|
cta_templates: item.cta_templates
|
|
||
|
|
null
|
|
,
|
|
|
|
auto_suggest_hashtags: item.auto_suggest_hashtags
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
auto_reply_enabled: item.auto_reply_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
auto_reply_rules: item.auto_reply_rules
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const ai_agent_settings = await db.ai_agent_settings.bulkCreate(ai_agent_settingsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return ai_agent_settings;
|
|
}
|
|
|
|
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 ai_agent_settings = await db.ai_agent_settings.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.auto_generate_plan !== undefined) updatePayload.auto_generate_plan = data.auto_generate_plan;
|
|
|
|
|
|
if (data.posts_per_week !== undefined) updatePayload.posts_per_week = data.posts_per_week;
|
|
|
|
|
|
if (data.stories_per_week !== undefined) updatePayload.stories_per_week = data.stories_per_week;
|
|
|
|
|
|
if (data.reels_per_week !== undefined) updatePayload.reels_per_week = data.reels_per_week;
|
|
|
|
|
|
if (data.primary_language !== undefined) updatePayload.primary_language = data.primary_language;
|
|
|
|
|
|
if (data.forbidden_topics !== undefined) updatePayload.forbidden_topics = data.forbidden_topics;
|
|
|
|
|
|
if (data.required_phrases !== undefined) updatePayload.required_phrases = data.required_phrases;
|
|
|
|
|
|
if (data.cta_templates !== undefined) updatePayload.cta_templates = data.cta_templates;
|
|
|
|
|
|
if (data.auto_suggest_hashtags !== undefined) updatePayload.auto_suggest_hashtags = data.auto_suggest_hashtags;
|
|
|
|
|
|
if (data.auto_reply_enabled !== undefined) updatePayload.auto_reply_enabled = data.auto_reply_enabled;
|
|
|
|
|
|
if (data.auto_reply_rules !== undefined) updatePayload.auto_reply_rules = data.auto_reply_rules;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await ai_agent_settings.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.organization !== undefined) {
|
|
await ai_agent_settings.setOrganization(
|
|
|
|
(globalAccess ? data.organization : (currentUser.organization?.id || currentUser.organizations?.id)),
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ai_agent_settings;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ai_agent_settings = await db.ai_agent_settings.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of ai_agent_settings) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of ai_agent_settings) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return ai_agent_settings;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ai_agent_settings = await db.ai_agent_settings.findByPk(id, options);
|
|
|
|
await ai_agent_settings.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await ai_agent_settings.destroy({
|
|
transaction
|
|
});
|
|
|
|
return ai_agent_settings;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ai_agent_settings = await db.ai_agent_settings.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!ai_agent_settings) {
|
|
return ai_agent_settings;
|
|
}
|
|
|
|
const output = ai_agent_settings.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.organization = await ai_agent_settings.getOrganization({
|
|
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.organizationId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organization',
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.forbidden_topics) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'ai_agent_settings',
|
|
'forbidden_topics',
|
|
filter.forbidden_topics,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.required_phrases) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'ai_agent_settings',
|
|
'required_phrases',
|
|
filter.required_phrases,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.cta_templates) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'ai_agent_settings',
|
|
'cta_templates',
|
|
filter.cta_templates,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.auto_reply_rules) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'ai_agent_settings',
|
|
'auto_reply_rules',
|
|
filter.auto_reply_rules,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.posts_per_weekRange) {
|
|
const [start, end] = filter.posts_per_weekRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
posts_per_week: {
|
|
...where.posts_per_week,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
posts_per_week: {
|
|
...where.posts_per_week,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.stories_per_weekRange) {
|
|
const [start, end] = filter.stories_per_weekRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
stories_per_week: {
|
|
...where.stories_per_week,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
stories_per_week: {
|
|
...where.stories_per_week,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.reels_per_weekRange) {
|
|
const [start, end] = filter.reels_per_weekRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
reels_per_week: {
|
|
...where.reels_per_week,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
reels_per_week: {
|
|
...where.reels_per_week,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.auto_generate_plan) {
|
|
where = {
|
|
...where,
|
|
auto_generate_plan: filter.auto_generate_plan,
|
|
};
|
|
}
|
|
|
|
if (filter.primary_language) {
|
|
where = {
|
|
...where,
|
|
primary_language: filter.primary_language,
|
|
};
|
|
}
|
|
|
|
if (filter.auto_suggest_hashtags) {
|
|
where = {
|
|
...where,
|
|
auto_suggest_hashtags: filter.auto_suggest_hashtags,
|
|
};
|
|
}
|
|
|
|
if (filter.auto_reply_enabled) {
|
|
where = {
|
|
...where,
|
|
auto_reply_enabled: filter.auto_reply_enabled,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.organization) {
|
|
const listItems = filter.organization.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationId: {[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.ai_agent_settings.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(
|
|
'ai_agent_settings',
|
|
'primary_language',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.ai_agent_settings.findAll({
|
|
attributes: [ 'id', 'primary_language' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['primary_language', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.primary_language,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|