38254-vm/backend/src/db/api/risk_assessments.js
2026-02-06 21:25:34 +00:00

800 lines
20 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 Risk_assessmentsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const risk_assessments = await db.risk_assessments.create(
{
id: data.id || undefined,
assessed_at: data.assessed_at
||
null
,
input_source: data.input_source
||
null
,
age: data.age
||
null
,
gender: data.gender
||
null
,
hypertension: data.hypertension
||
null
,
heart_disease: data.heart_disease
||
null
,
ever_married: data.ever_married
||
null
,
work_type: data.work_type
||
null
,
residence_type: data.residence_type
||
null
,
avg_glucose_level: data.avg_glucose_level
||
null
,
bmi: data.bmi
||
null
,
smoking_status: data.smoking_status
||
null
,
stroke_probability: data.stroke_probability
||
null
,
risk_level: data.risk_level
||
null
,
recommendation_text: data.recommendation_text
||
null
,
consent_to_store: data.consent_to_store
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await risk_assessments.setUser( data.user || null, {
transaction,
});
await risk_assessments.setMl_model( data.ml_model || null, {
transaction,
});
return 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 risk_assessmentsData = data.map((item, index) => ({
id: item.id || undefined,
assessed_at: item.assessed_at
||
null
,
input_source: item.input_source
||
null
,
age: item.age
||
null
,
gender: item.gender
||
null
,
hypertension: item.hypertension
||
null
,
heart_disease: item.heart_disease
||
null
,
ever_married: item.ever_married
||
null
,
work_type: item.work_type
||
null
,
residence_type: item.residence_type
||
null
,
avg_glucose_level: item.avg_glucose_level
||
null
,
bmi: item.bmi
||
null
,
smoking_status: item.smoking_status
||
null
,
stroke_probability: item.stroke_probability
||
null
,
risk_level: item.risk_level
||
null
,
recommendation_text: item.recommendation_text
||
null
,
consent_to_store: item.consent_to_store
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const risk_assessments = await db.risk_assessments.bulkCreate(risk_assessmentsData, { transaction });
// For each item created, replace relation files
return risk_assessments;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const risk_assessments = await db.risk_assessments.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.assessed_at !== undefined) updatePayload.assessed_at = data.assessed_at;
if (data.input_source !== undefined) updatePayload.input_source = data.input_source;
if (data.age !== undefined) updatePayload.age = data.age;
if (data.gender !== undefined) updatePayload.gender = data.gender;
if (data.hypertension !== undefined) updatePayload.hypertension = data.hypertension;
if (data.heart_disease !== undefined) updatePayload.heart_disease = data.heart_disease;
if (data.ever_married !== undefined) updatePayload.ever_married = data.ever_married;
if (data.work_type !== undefined) updatePayload.work_type = data.work_type;
if (data.residence_type !== undefined) updatePayload.residence_type = data.residence_type;
if (data.avg_glucose_level !== undefined) updatePayload.avg_glucose_level = data.avg_glucose_level;
if (data.bmi !== undefined) updatePayload.bmi = data.bmi;
if (data.smoking_status !== undefined) updatePayload.smoking_status = data.smoking_status;
if (data.stroke_probability !== undefined) updatePayload.stroke_probability = data.stroke_probability;
if (data.risk_level !== undefined) updatePayload.risk_level = data.risk_level;
if (data.recommendation_text !== undefined) updatePayload.recommendation_text = data.recommendation_text;
if (data.consent_to_store !== undefined) updatePayload.consent_to_store = data.consent_to_store;
updatePayload.updatedById = currentUser.id;
await risk_assessments.update(updatePayload, {transaction});
if (data.user !== undefined) {
await risk_assessments.setUser(
data.user,
{ transaction }
);
}
if (data.ml_model !== undefined) {
await risk_assessments.setMl_model(
data.ml_model,
{ transaction }
);
}
return risk_assessments;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const risk_assessments = await db.risk_assessments.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of risk_assessments) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of risk_assessments) {
await record.destroy({transaction});
}
});
return risk_assessments;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const risk_assessments = await db.risk_assessments.findByPk(id, options);
await risk_assessments.update({
deletedBy: currentUser.id
}, {
transaction,
});
await risk_assessments.destroy({
transaction
});
return risk_assessments;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const risk_assessments = await db.risk_assessments.findOne(
{ where },
{ transaction },
);
if (!risk_assessments) {
return risk_assessments;
}
const output = risk_assessments.get({plain: true});
output.api_requests_risk_assessment = await risk_assessments.getApi_requests_risk_assessment({
transaction
});
output.user = await risk_assessments.getUser({
transaction
});
output.ml_model = await risk_assessments.getMl_model({
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.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.ml_models,
as: 'ml_model',
where: filter.ml_model ? {
[Op.or]: [
{ id: { [Op.in]: filter.ml_model.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.ml_model.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.recommendation_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'risk_assessments',
'recommendation_text',
filter.recommendation_text,
),
};
}
if (filter.assessed_atRange) {
const [start, end] = filter.assessed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
assessed_at: {
...where.assessed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
assessed_at: {
...where.assessed_at,
[Op.lte]: end,
},
};
}
}
if (filter.ageRange) {
const [start, end] = filter.ageRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
age: {
...where.age,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
age: {
...where.age,
[Op.lte]: end,
},
};
}
}
if (filter.avg_glucose_levelRange) {
const [start, end] = filter.avg_glucose_levelRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
avg_glucose_level: {
...where.avg_glucose_level,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
avg_glucose_level: {
...where.avg_glucose_level,
[Op.lte]: end,
},
};
}
}
if (filter.bmiRange) {
const [start, end] = filter.bmiRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
bmi: {
...where.bmi,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
bmi: {
...where.bmi,
[Op.lte]: end,
},
};
}
}
if (filter.stroke_probabilityRange) {
const [start, end] = filter.stroke_probabilityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
stroke_probability: {
...where.stroke_probability,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
stroke_probability: {
...where.stroke_probability,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.input_source) {
where = {
...where,
input_source: filter.input_source,
};
}
if (filter.gender) {
where = {
...where,
gender: filter.gender,
};
}
if (filter.hypertension) {
where = {
...where,
hypertension: filter.hypertension,
};
}
if (filter.heart_disease) {
where = {
...where,
heart_disease: filter.heart_disease,
};
}
if (filter.ever_married) {
where = {
...where,
ever_married: filter.ever_married,
};
}
if (filter.work_type) {
where = {
...where,
work_type: filter.work_type,
};
}
if (filter.residence_type) {
where = {
...where,
residence_type: filter.residence_type,
};
}
if (filter.smoking_status) {
where = {
...where,
smoking_status: filter.smoking_status,
};
}
if (filter.risk_level) {
where = {
...where,
risk_level: filter.risk_level,
};
}
if (filter.consent_to_store) {
where = {
...where,
consent_to_store: filter.consent_to_store,
};
}
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.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(
'risk_assessments',
'recommendation_text',
query,
),
],
};
}
const records = await db.risk_assessments.findAll({
attributes: [ 'id', 'recommendation_text' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['recommendation_text', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.recommendation_text,
}));
}
};