38409-vm/backend/src/db/api/recovery_analyses.js
2026-02-13 17:35:49 +00:00

586 lines
15 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 Recovery_analysesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recovery_analyses = await db.recovery_analyses.create(
{
id: data.id || undefined,
recovery_status: data.recovery_status
||
null
,
recovery_vector: data.recovery_vector
||
null
,
approach_summary: data.approach_summary
||
null
,
estimated_cost_usd: data.estimated_cost_usd
||
null
,
estimated_benefit_usd: data.estimated_benefit_usd
||
null
,
estimated_probability_percent: data.estimated_probability_percent
||
null
,
analysis_at: data.analysis_at
||
null
,
risk_notes: data.risk_notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await recovery_analyses.setCase_file( data.case_file || null, {
transaction,
});
return recovery_analyses;
}
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 recovery_analysesData = data.map((item, index) => ({
id: item.id || undefined,
recovery_status: item.recovery_status
||
null
,
recovery_vector: item.recovery_vector
||
null
,
approach_summary: item.approach_summary
||
null
,
estimated_cost_usd: item.estimated_cost_usd
||
null
,
estimated_benefit_usd: item.estimated_benefit_usd
||
null
,
estimated_probability_percent: item.estimated_probability_percent
||
null
,
analysis_at: item.analysis_at
||
null
,
risk_notes: item.risk_notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const recovery_analyses = await db.recovery_analyses.bulkCreate(recovery_analysesData, { transaction });
// For each item created, replace relation files
return recovery_analyses;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const recovery_analyses = await db.recovery_analyses.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.recovery_status !== undefined) updatePayload.recovery_status = data.recovery_status;
if (data.recovery_vector !== undefined) updatePayload.recovery_vector = data.recovery_vector;
if (data.approach_summary !== undefined) updatePayload.approach_summary = data.approach_summary;
if (data.estimated_cost_usd !== undefined) updatePayload.estimated_cost_usd = data.estimated_cost_usd;
if (data.estimated_benefit_usd !== undefined) updatePayload.estimated_benefit_usd = data.estimated_benefit_usd;
if (data.estimated_probability_percent !== undefined) updatePayload.estimated_probability_percent = data.estimated_probability_percent;
if (data.analysis_at !== undefined) updatePayload.analysis_at = data.analysis_at;
if (data.risk_notes !== undefined) updatePayload.risk_notes = data.risk_notes;
updatePayload.updatedById = currentUser.id;
await recovery_analyses.update(updatePayload, {transaction});
if (data.case_file !== undefined) {
await recovery_analyses.setCase_file(
data.case_file,
{ transaction }
);
}
return recovery_analyses;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recovery_analyses = await db.recovery_analyses.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of recovery_analyses) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of recovery_analyses) {
await record.destroy({transaction});
}
});
return recovery_analyses;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const recovery_analyses = await db.recovery_analyses.findByPk(id, options);
await recovery_analyses.update({
deletedBy: currentUser.id
}, {
transaction,
});
await recovery_analyses.destroy({
transaction
});
return recovery_analyses;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const recovery_analyses = await db.recovery_analyses.findOne(
{ where },
{ transaction },
);
if (!recovery_analyses) {
return recovery_analyses;
}
const output = recovery_analyses.get({plain: true});
output.case_file = await recovery_analyses.getCase_file({
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.case_files,
as: 'case_file',
where: filter.case_file ? {
[Op.or]: [
{ id: { [Op.in]: filter.case_file.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.case_file.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.approach_summary) {
where = {
...where,
[Op.and]: Utils.ilike(
'recovery_analyses',
'approach_summary',
filter.approach_summary,
),
};
}
if (filter.risk_notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'recovery_analyses',
'risk_notes',
filter.risk_notes,
),
};
}
if (filter.estimated_cost_usdRange) {
const [start, end] = filter.estimated_cost_usdRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
estimated_cost_usd: {
...where.estimated_cost_usd,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
estimated_cost_usd: {
...where.estimated_cost_usd,
[Op.lte]: end,
},
};
}
}
if (filter.estimated_benefit_usdRange) {
const [start, end] = filter.estimated_benefit_usdRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
estimated_benefit_usd: {
...where.estimated_benefit_usd,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
estimated_benefit_usd: {
...where.estimated_benefit_usd,
[Op.lte]: end,
},
};
}
}
if (filter.estimated_probability_percentRange) {
const [start, end] = filter.estimated_probability_percentRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
estimated_probability_percent: {
...where.estimated_probability_percent,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
estimated_probability_percent: {
...where.estimated_probability_percent,
[Op.lte]: end,
},
};
}
}
if (filter.analysis_atRange) {
const [start, end] = filter.analysis_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
analysis_at: {
...where.analysis_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
analysis_at: {
...where.analysis_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.recovery_status) {
where = {
...where,
recovery_status: filter.recovery_status,
};
}
if (filter.recovery_vector) {
where = {
...where,
recovery_vector: filter.recovery_vector,
};
}
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.recovery_analyses.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(
'recovery_analyses',
'approach_summary',
query,
),
],
};
}
const records = await db.recovery_analyses.findAll({
attributes: [ 'id', 'approach_summary' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['approach_summary', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.approach_summary,
}));
}
};