39949-vm/backend/src/db/api/vendor_risk_assessments.js
2026-05-11 09:50:25 +00:00

1025 lines
27 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 Vendor_risk_assessmentsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const vendor_risk_assessments = await db.vendor_risk_assessments.create(
{
id: data.id || undefined,
assessment_status: data.assessment_status
||
null
,
risk_rating: data.risk_rating
||
null
,
data_retention_policy: data.data_retention_policy
||
null
,
data_residency: data.data_residency
||
null
,
subprocessors_used: data.subprocessors_used
||
false
,
supports_deletion: data.supports_deletion
||
false
,
supports_export: data.supports_export
||
false
,
audit_logs_available: data.audit_logs_available
||
false
,
incident_response_sla: data.incident_response_sla
||
false
,
security_certifications_present: data.security_certifications_present
||
false
,
security_score: data.security_score
||
null
,
privacy_score: data.privacy_score
||
null
,
legal_risk_score: data.legal_risk_score
||
null
,
integration_readiness_score: data.integration_readiness_score
||
null
,
cost_score: data.cost_score
||
null
,
summary: data.summary
||
null
,
started_at: data.started_at
||
null
,
completed_at: data.completed_at
||
null
,
expires_at: data.expires_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await vendor_risk_assessments.setVendor( data.vendor || null, {
transaction,
});
await vendor_risk_assessments.setTool( data.tool || null, {
transaction,
});
await vendor_risk_assessments.setOwner( data.owner || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.vendor_risk_assessments.getTableName(),
belongsToColumn: 'attachments',
belongsToId: vendor_risk_assessments.id,
},
data.attachments,
options,
);
return vendor_risk_assessments;
}
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 vendor_risk_assessmentsData = data.map((item, index) => ({
id: item.id || undefined,
assessment_status: item.assessment_status
||
null
,
risk_rating: item.risk_rating
||
null
,
data_retention_policy: item.data_retention_policy
||
null
,
data_residency: item.data_residency
||
null
,
subprocessors_used: item.subprocessors_used
||
false
,
supports_deletion: item.supports_deletion
||
false
,
supports_export: item.supports_export
||
false
,
audit_logs_available: item.audit_logs_available
||
false
,
incident_response_sla: item.incident_response_sla
||
false
,
security_certifications_present: item.security_certifications_present
||
false
,
security_score: item.security_score
||
null
,
privacy_score: item.privacy_score
||
null
,
legal_risk_score: item.legal_risk_score
||
null
,
integration_readiness_score: item.integration_readiness_score
||
null
,
cost_score: item.cost_score
||
null
,
summary: item.summary
||
null
,
started_at: item.started_at
||
null
,
completed_at: item.completed_at
||
null
,
expires_at: item.expires_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const vendor_risk_assessments = await db.vendor_risk_assessments.bulkCreate(vendor_risk_assessmentsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < vendor_risk_assessments.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.vendor_risk_assessments.getTableName(),
belongsToColumn: 'attachments',
belongsToId: vendor_risk_assessments[i].id,
},
data[i].attachments,
options,
);
}
return vendor_risk_assessments;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const vendor_risk_assessments = await db.vendor_risk_assessments.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.assessment_status !== undefined) updatePayload.assessment_status = data.assessment_status;
if (data.risk_rating !== undefined) updatePayload.risk_rating = data.risk_rating;
if (data.data_retention_policy !== undefined) updatePayload.data_retention_policy = data.data_retention_policy;
if (data.data_residency !== undefined) updatePayload.data_residency = data.data_residency;
if (data.subprocessors_used !== undefined) updatePayload.subprocessors_used = data.subprocessors_used;
if (data.supports_deletion !== undefined) updatePayload.supports_deletion = data.supports_deletion;
if (data.supports_export !== undefined) updatePayload.supports_export = data.supports_export;
if (data.audit_logs_available !== undefined) updatePayload.audit_logs_available = data.audit_logs_available;
if (data.incident_response_sla !== undefined) updatePayload.incident_response_sla = data.incident_response_sla;
if (data.security_certifications_present !== undefined) updatePayload.security_certifications_present = data.security_certifications_present;
if (data.security_score !== undefined) updatePayload.security_score = data.security_score;
if (data.privacy_score !== undefined) updatePayload.privacy_score = data.privacy_score;
if (data.legal_risk_score !== undefined) updatePayload.legal_risk_score = data.legal_risk_score;
if (data.integration_readiness_score !== undefined) updatePayload.integration_readiness_score = data.integration_readiness_score;
if (data.cost_score !== undefined) updatePayload.cost_score = data.cost_score;
if (data.summary !== undefined) updatePayload.summary = data.summary;
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at;
if (data.expires_at !== undefined) updatePayload.expires_at = data.expires_at;
updatePayload.updatedById = currentUser.id;
await vendor_risk_assessments.update(updatePayload, {transaction});
if (data.vendor !== undefined) {
await vendor_risk_assessments.setVendor(
data.vendor,
{ transaction }
);
}
if (data.tool !== undefined) {
await vendor_risk_assessments.setTool(
data.tool,
{ transaction }
);
}
if (data.owner !== undefined) {
await vendor_risk_assessments.setOwner(
data.owner,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.vendor_risk_assessments.getTableName(),
belongsToColumn: 'attachments',
belongsToId: vendor_risk_assessments.id,
},
data.attachments,
options,
);
return vendor_risk_assessments;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const vendor_risk_assessments = await db.vendor_risk_assessments.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of vendor_risk_assessments) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of vendor_risk_assessments) {
await record.destroy({transaction});
}
});
return vendor_risk_assessments;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const vendor_risk_assessments = await db.vendor_risk_assessments.findByPk(id, options);
await vendor_risk_assessments.update({
deletedBy: currentUser.id
}, {
transaction,
});
await vendor_risk_assessments.destroy({
transaction
});
return vendor_risk_assessments;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const vendor_risk_assessments = await db.vendor_risk_assessments.findOne(
{ where },
{ transaction },
);
if (!vendor_risk_assessments) {
return vendor_risk_assessments;
}
const output = vendor_risk_assessments.get({plain: true});
output.vendor = await vendor_risk_assessments.getVendor({
transaction
});
output.tool = await vendor_risk_assessments.getTool({
transaction
});
output.attachments = await vendor_risk_assessments.getAttachments({
transaction
});
output.owner = await vendor_risk_assessments.getOwner({
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.vendors,
as: 'vendor',
where: filter.vendor ? {
[Op.or]: [
{ id: { [Op.in]: filter.vendor.split('|').map(term => Utils.uuid(term)) } },
{
vendor_name: {
[Op.or]: filter.vendor.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
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.users,
as: 'owner',
where: filter.owner ? {
[Op.or]: [
{ id: { [Op.in]: filter.owner.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.owner.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.summary) {
where = {
...where,
[Op.and]: Utils.ilike(
'vendor_risk_assessments',
'summary',
filter.summary,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
started_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
expires_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.security_scoreRange) {
const [start, end] = filter.security_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
security_score: {
...where.security_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
security_score: {
...where.security_score,
[Op.lte]: end,
},
};
}
}
if (filter.privacy_scoreRange) {
const [start, end] = filter.privacy_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
privacy_score: {
...where.privacy_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
privacy_score: {
...where.privacy_score,
[Op.lte]: end,
},
};
}
}
if (filter.legal_risk_scoreRange) {
const [start, end] = filter.legal_risk_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
legal_risk_score: {
...where.legal_risk_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
legal_risk_score: {
...where.legal_risk_score,
[Op.lte]: end,
},
};
}
}
if (filter.integration_readiness_scoreRange) {
const [start, end] = filter.integration_readiness_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
integration_readiness_score: {
...where.integration_readiness_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
integration_readiness_score: {
...where.integration_readiness_score,
[Op.lte]: end,
},
};
}
}
if (filter.cost_scoreRange) {
const [start, end] = filter.cost_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
cost_score: {
...where.cost_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
cost_score: {
...where.cost_score,
[Op.lte]: end,
},
};
}
}
if (filter.started_atRange) {
const [start, end] = filter.started_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.lte]: end,
},
};
}
}
if (filter.completed_atRange) {
const [start, end] = filter.completed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.lte]: end,
},
};
}
}
if (filter.expires_atRange) {
const [start, end] = filter.expires_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.assessment_status) {
where = {
...where,
assessment_status: filter.assessment_status,
};
}
if (filter.risk_rating) {
where = {
...where,
risk_rating: filter.risk_rating,
};
}
if (filter.data_retention_policy) {
where = {
...where,
data_retention_policy: filter.data_retention_policy,
};
}
if (filter.data_residency) {
where = {
...where,
data_residency: filter.data_residency,
};
}
if (filter.subprocessors_used) {
where = {
...where,
subprocessors_used: filter.subprocessors_used,
};
}
if (filter.supports_deletion) {
where = {
...where,
supports_deletion: filter.supports_deletion,
};
}
if (filter.supports_export) {
where = {
...where,
supports_export: filter.supports_export,
};
}
if (filter.audit_logs_available) {
where = {
...where,
audit_logs_available: filter.audit_logs_available,
};
}
if (filter.incident_response_sla) {
where = {
...where,
incident_response_sla: filter.incident_response_sla,
};
}
if (filter.security_certifications_present) {
where = {
...where,
security_certifications_present: filter.security_certifications_present,
};
}
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.vendor_risk_assessments.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(
'vendor_risk_assessments',
'summary',
query,
),
],
};
}
const records = await db.vendor_risk_assessments.findAll({
attributes: [ 'id', 'summary' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['summary', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.summary,
}));
}
};