524 lines
13 KiB
JavaScript
524 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 Tool_access_rulesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const tool_access_rules = await db.tool_access_rules.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
min_role_allowed: data.min_role_allowed
|
|
||
|
|
null
|
|
,
|
|
|
|
max_sensitivity_allowed: data.max_sensitivity_allowed
|
|
||
|
|
null
|
|
,
|
|
|
|
requires_training: data.requires_training
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
requires_client_consent: data.requires_client_consent
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
restrictions_notes: data.restrictions_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await tool_access_rules.setTool( data.tool || null, {
|
|
transaction,
|
|
});
|
|
|
|
await tool_access_rules.setRequired_course( data.required_course || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tool_access_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 tool_access_rulesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
min_role_allowed: item.min_role_allowed
|
|
||
|
|
null
|
|
,
|
|
|
|
max_sensitivity_allowed: item.max_sensitivity_allowed
|
|
||
|
|
null
|
|
,
|
|
|
|
requires_training: item.requires_training
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
requires_client_consent: item.requires_client_consent
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
restrictions_notes: item.restrictions_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const tool_access_rules = await db.tool_access_rules.bulkCreate(tool_access_rulesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return tool_access_rules;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const tool_access_rules = await db.tool_access_rules.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.min_role_allowed !== undefined) updatePayload.min_role_allowed = data.min_role_allowed;
|
|
|
|
|
|
if (data.max_sensitivity_allowed !== undefined) updatePayload.max_sensitivity_allowed = data.max_sensitivity_allowed;
|
|
|
|
|
|
if (data.requires_training !== undefined) updatePayload.requires_training = data.requires_training;
|
|
|
|
|
|
if (data.requires_client_consent !== undefined) updatePayload.requires_client_consent = data.requires_client_consent;
|
|
|
|
|
|
if (data.restrictions_notes !== undefined) updatePayload.restrictions_notes = data.restrictions_notes;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await tool_access_rules.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.tool !== undefined) {
|
|
await tool_access_rules.setTool(
|
|
|
|
data.tool,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.required_course !== undefined) {
|
|
await tool_access_rules.setRequired_course(
|
|
|
|
data.required_course,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tool_access_rules;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const tool_access_rules = await db.tool_access_rules.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of tool_access_rules) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of tool_access_rules) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return tool_access_rules;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const tool_access_rules = await db.tool_access_rules.findByPk(id, options);
|
|
|
|
await tool_access_rules.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await tool_access_rules.destroy({
|
|
transaction
|
|
});
|
|
|
|
return tool_access_rules;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const tool_access_rules = await db.tool_access_rules.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!tool_access_rules) {
|
|
return tool_access_rules;
|
|
}
|
|
|
|
const output = tool_access_rules.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.tool = await tool_access_rules.getTool({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.required_course = await tool_access_rules.getRequired_course({
|
|
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.ai_tools,
|
|
as: 'tool',
|
|
|
|
where: filter.tool ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.tool.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
tool_name: {
|
|
[Op.or]: filter.tool.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.training_courses,
|
|
as: 'required_course',
|
|
|
|
where: filter.required_course ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.required_course.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
course_title: {
|
|
[Op.or]: filter.required_course.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.restrictions_notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'tool_access_rules',
|
|
'restrictions_notes',
|
|
filter.restrictions_notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.min_role_allowed) {
|
|
where = {
|
|
...where,
|
|
min_role_allowed: filter.min_role_allowed,
|
|
};
|
|
}
|
|
|
|
if (filter.max_sensitivity_allowed) {
|
|
where = {
|
|
...where,
|
|
max_sensitivity_allowed: filter.max_sensitivity_allowed,
|
|
};
|
|
}
|
|
|
|
if (filter.requires_training) {
|
|
where = {
|
|
...where,
|
|
requires_training: filter.requires_training,
|
|
};
|
|
}
|
|
|
|
if (filter.requires_client_consent) {
|
|
where = {
|
|
...where,
|
|
requires_client_consent: filter.requires_client_consent,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.tool_access_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, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'tool_access_rules',
|
|
'restrictions_notes',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.tool_access_rules.findAll({
|
|
attributes: [ 'id', 'restrictions_notes' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['restrictions_notes', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.restrictions_notes,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|