39950-vm/backend/src/db/api/policies.js
2026-05-11 09:55:08 +00:00

645 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 PoliciesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const policies = await db.policies.create(
{
id: data.id || undefined,
title: data.title
||
null
,
policy_type: data.policy_type
||
null
,
status: data.status
||
null
,
summary: data.summary
||
null
,
content: data.content
||
null
,
effective_from: data.effective_from
||
null
,
effective_to: data.effective_to
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await policies.setPractice_group( data.practice_group || null, {
transaction,
});
await policies.setData_classification( data.data_classification || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.policies.getTableName(),
belongsToColumn: 'attachments',
belongsToId: policies.id,
},
data.attachments,
options,
);
return policies;
}
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 policiesData = data.map((item, index) => ({
id: item.id || undefined,
title: item.title
||
null
,
policy_type: item.policy_type
||
null
,
status: item.status
||
null
,
summary: item.summary
||
null
,
content: item.content
||
null
,
effective_from: item.effective_from
||
null
,
effective_to: item.effective_to
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const policies = await db.policies.bulkCreate(policiesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < policies.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.policies.getTableName(),
belongsToColumn: 'attachments',
belongsToId: policies[i].id,
},
data[i].attachments,
options,
);
}
return policies;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const policies = await db.policies.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.policy_type !== undefined) updatePayload.policy_type = data.policy_type;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.summary !== undefined) updatePayload.summary = data.summary;
if (data.content !== undefined) updatePayload.content = data.content;
if (data.effective_from !== undefined) updatePayload.effective_from = data.effective_from;
if (data.effective_to !== undefined) updatePayload.effective_to = data.effective_to;
updatePayload.updatedById = currentUser.id;
await policies.update(updatePayload, {transaction});
if (data.practice_group !== undefined) {
await policies.setPractice_group(
data.practice_group,
{ transaction }
);
}
if (data.data_classification !== undefined) {
await policies.setData_classification(
data.data_classification,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.policies.getTableName(),
belongsToColumn: 'attachments',
belongsToId: policies.id,
},
data.attachments,
options,
);
return policies;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const policies = await db.policies.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of policies) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of policies) {
await record.destroy({transaction});
}
});
return policies;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const policies = await db.policies.findByPk(id, options);
await policies.update({
deletedBy: currentUser.id
}, {
transaction,
});
await policies.destroy({
transaction
});
return policies;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const policies = await db.policies.findOne(
{ where },
{ transaction },
);
if (!policies) {
return policies;
}
const output = policies.get({plain: true});
output.practice_group = await policies.getPractice_group({
transaction
});
output.data_classification = await policies.getData_classification({
transaction
});
output.attachments = await policies.getAttachments({
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.file,
as: 'attachments',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'policies',
'title',
filter.title,
),
};
}
if (filter.summary) {
where = {
...where,
[Op.and]: Utils.ilike(
'policies',
'summary',
filter.summary,
),
};
}
if (filter.content) {
where = {
...where,
[Op.and]: Utils.ilike(
'policies',
'content',
filter.content,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
effective_from: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
effective_to: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.effective_fromRange) {
const [start, end] = filter.effective_fromRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
effective_from: {
...where.effective_from,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
effective_from: {
...where.effective_from,
[Op.lte]: end,
},
};
}
}
if (filter.effective_toRange) {
const [start, end] = filter.effective_toRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
effective_to: {
...where.effective_to,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
effective_to: {
...where.effective_to,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.policy_type) {
where = {
...where,
policy_type: filter.policy_type,
};
}
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.policies.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(
'policies',
'title',
query,
),
],
};
}
const records = await db.policies.findAll({
attributes: [ 'id', 'title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title,
}));
}
};