598 lines
15 KiB
JavaScript
598 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 Scan_resultsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scan_results = await db.scan_results.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
health_status: data.health_status
|
|
||
|
|
null
|
|
,
|
|
|
|
confidence_score: data.confidence_score
|
|
||
|
|
null
|
|
,
|
|
|
|
summary: data.summary
|
|
||
|
|
null
|
|
,
|
|
|
|
recommended_actions: data.recommended_actions
|
|
||
|
|
null
|
|
,
|
|
|
|
generated_at: data.generated_at
|
|
||
|
|
null
|
|
,
|
|
|
|
is_primary: data.is_primary
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await scan_results.setScan( data.scan || null, {
|
|
transaction,
|
|
});
|
|
|
|
await scan_results.setDisease( data.disease || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.scan_results.getTableName(),
|
|
belongsToColumn: 'heatmap_files',
|
|
belongsToId: scan_results.id,
|
|
},
|
|
data.heatmap_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return scan_results;
|
|
}
|
|
|
|
|
|
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 scan_resultsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
health_status: item.health_status
|
|
||
|
|
null
|
|
,
|
|
|
|
confidence_score: item.confidence_score
|
|
||
|
|
null
|
|
,
|
|
|
|
summary: item.summary
|
|
||
|
|
null
|
|
,
|
|
|
|
recommended_actions: item.recommended_actions
|
|
||
|
|
null
|
|
,
|
|
|
|
generated_at: item.generated_at
|
|
||
|
|
null
|
|
,
|
|
|
|
is_primary: item.is_primary
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const scan_results = await db.scan_results.bulkCreate(scan_resultsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < scan_results.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.scan_results.getTableName(),
|
|
belongsToColumn: 'heatmap_files',
|
|
belongsToId: scan_results[i].id,
|
|
},
|
|
data[i].heatmap_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return scan_results;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const scan_results = await db.scan_results.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.health_status !== undefined) updatePayload.health_status = data.health_status;
|
|
|
|
|
|
if (data.confidence_score !== undefined) updatePayload.confidence_score = data.confidence_score;
|
|
|
|
|
|
if (data.summary !== undefined) updatePayload.summary = data.summary;
|
|
|
|
|
|
if (data.recommended_actions !== undefined) updatePayload.recommended_actions = data.recommended_actions;
|
|
|
|
|
|
if (data.generated_at !== undefined) updatePayload.generated_at = data.generated_at;
|
|
|
|
|
|
if (data.is_primary !== undefined) updatePayload.is_primary = data.is_primary;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await scan_results.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.scan !== undefined) {
|
|
await scan_results.setScan(
|
|
|
|
data.scan,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.disease !== undefined) {
|
|
await scan_results.setDisease(
|
|
|
|
data.disease,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.scan_results.getTableName(),
|
|
belongsToColumn: 'heatmap_files',
|
|
belongsToId: scan_results.id,
|
|
},
|
|
data.heatmap_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return scan_results;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scan_results = await db.scan_results.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of scan_results) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of scan_results) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return scan_results;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scan_results = await db.scan_results.findByPk(id, options);
|
|
|
|
await scan_results.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await scan_results.destroy({
|
|
transaction
|
|
});
|
|
|
|
return scan_results;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scan_results = await db.scan_results.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!scan_results) {
|
|
return scan_results;
|
|
}
|
|
|
|
const output = scan_results.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.scan = await scan_results.getScan({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.disease = await scan_results.getDisease({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.heatmap_files = await scan_results.getHeatmap_files({
|
|
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.detection_scans,
|
|
as: 'scan',
|
|
|
|
where: filter.scan ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.scan.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
model_version: {
|
|
[Op.or]: filter.scan.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.disease_catalog,
|
|
as: 'disease',
|
|
|
|
where: filter.disease ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.disease.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.disease.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'heatmap_files',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.summary) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'scan_results',
|
|
'summary',
|
|
filter.summary,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.recommended_actions) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'scan_results',
|
|
'recommended_actions',
|
|
filter.recommended_actions,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.confidence_scoreRange) {
|
|
const [start, end] = filter.confidence_scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
confidence_score: {
|
|
...where.confidence_score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
confidence_score: {
|
|
...where.confidence_score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.generated_atRange) {
|
|
const [start, end] = filter.generated_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
generated_at: {
|
|
...where.generated_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
generated_at: {
|
|
...where.generated_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.health_status) {
|
|
where = {
|
|
...where,
|
|
health_status: filter.health_status,
|
|
};
|
|
}
|
|
|
|
if (filter.is_primary) {
|
|
where = {
|
|
...where,
|
|
is_primary: filter.is_primary,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.scan_results.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(
|
|
'scan_results',
|
|
'summary',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.scan_results.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,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|