40072-vm/backend/src/db/api/human_review_checklists.js
2026-05-25 12:48:16 +00:00

532 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 Human_review_checklistsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const human_review_checklists = await db.human_review_checklists.create(
{
id: data.id || undefined,
name: data.name
||
null
,
purpose: data.purpose
||
null
,
status: data.status
||
null
,
instructions: data.instructions
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await human_review_checklists.setPractice_group( data.practice_group || null, {
transaction,
});
await human_review_checklists.setData_classification( data.data_classification || null, {
transaction,
});
await human_review_checklists.setRequired_reviewer_role( data.required_reviewer_role || null, {
transaction,
});
return human_review_checklists;
}
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_review_checklistsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
purpose: item.purpose
||
null
,
status: item.status
||
null
,
instructions: item.instructions
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const human_review_checklists = await db.human_review_checklists.bulkCreate(human_review_checklistsData, { transaction });
// For each item created, replace relation files
return human_review_checklists;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const human_review_checklists = await db.human_review_checklists.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.purpose !== undefined) updatePayload.purpose = data.purpose;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.instructions !== undefined) updatePayload.instructions = data.instructions;
updatePayload.updatedById = currentUser.id;
await human_review_checklists.update(updatePayload, {transaction});
if (data.practice_group !== undefined) {
await human_review_checklists.setPractice_group(
data.practice_group,
{ transaction }
);
}
if (data.data_classification !== undefined) {
await human_review_checklists.setData_classification(
data.data_classification,
{ transaction }
);
}
if (data.required_reviewer_role !== undefined) {
await human_review_checklists.setRequired_reviewer_role(
data.required_reviewer_role,
{ transaction }
);
}
return human_review_checklists;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const human_review_checklists = await db.human_review_checklists.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of human_review_checklists) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of human_review_checklists) {
await record.destroy({transaction});
}
});
return human_review_checklists;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const human_review_checklists = await db.human_review_checklists.findByPk(id, options);
await human_review_checklists.update({
deletedBy: currentUser.id
}, {
transaction,
});
await human_review_checklists.destroy({
transaction
});
return human_review_checklists;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const human_review_checklists = await db.human_review_checklists.findOne(
{ where },
{ transaction },
);
if (!human_review_checklists) {
return human_review_checklists;
}
const output = human_review_checklists.get({plain: true});
output.checklist_items_checklist = await human_review_checklists.getChecklist_items_checklist({
transaction
});
output.review_exceptions_checklist = await human_review_checklists.getReview_exceptions_checklist({
transaction
});
output.practice_group = await human_review_checklists.getPractice_group({
transaction
});
output.data_classification = await human_review_checklists.getData_classification({
transaction
});
output.required_reviewer_role = await human_review_checklists.getRequired_reviewer_role({
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.practice_groups,
as: 'practice_group',
where: filter.practice_group ? {
[Op.or]: [
{ id: { [Op.in]: filter.practice_group.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.practice_group.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.data_classifications,
as: 'data_classification',
where: filter.data_classification ? {
[Op.or]: [
{ id: { [Op.in]: filter.data_classification.split('|').map(term => Utils.uuid(term)) } },
{
label: {
[Op.or]: filter.data_classification.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.roles_catalog,
as: 'required_reviewer_role',
where: filter.required_reviewer_role ? {
[Op.or]: [
{ id: { [Op.in]: filter.required_reviewer_role.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.required_reviewer_role.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'human_review_checklists',
'name',
filter.name,
),
};
}
if (filter.instructions) {
where = {
...where,
[Op.and]: Utils.ilike(
'human_review_checklists',
'instructions',
filter.instructions,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.purpose) {
where = {
...where,
purpose: filter.purpose,
};
}
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.human_review_checklists.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(
'human_review_checklists',
'name',
query,
),
],
};
}
const records = await db.human_review_checklists.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};