759 lines
19 KiB
JavaScript
759 lines
19 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 Product_resultsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const product_results = await db.product_results.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
observed_at: data.observed_at
|
|
||
|
|
null
|
|
,
|
|
|
|
reported_at: data.reported_at
|
|
||
|
|
null
|
|
,
|
|
|
|
value_decimal: data.value_decimal
|
|
||
|
|
null
|
|
,
|
|
|
|
value_int: data.value_int
|
|
||
|
|
null
|
|
,
|
|
|
|
value_boolean: data.value_boolean
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
value_text: data.value_text
|
|
||
|
|
null
|
|
,
|
|
|
|
value_display: data.value_display
|
|
||
|
|
null
|
|
,
|
|
|
|
quality_flag: data.quality_flag
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await product_results.setProduct( data.product || null, {
|
|
transaction,
|
|
});
|
|
|
|
await product_results.setMetric_definition( data.metric_definition || null, {
|
|
transaction,
|
|
});
|
|
|
|
await product_results.setIngestion( data.ingestion || null, {
|
|
transaction,
|
|
});
|
|
|
|
await product_results.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return product_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 product_resultsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
observed_at: item.observed_at
|
|
||
|
|
null
|
|
,
|
|
|
|
reported_at: item.reported_at
|
|
||
|
|
null
|
|
,
|
|
|
|
value_decimal: item.value_decimal
|
|
||
|
|
null
|
|
,
|
|
|
|
value_int: item.value_int
|
|
||
|
|
null
|
|
,
|
|
|
|
value_boolean: item.value_boolean
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
value_text: item.value_text
|
|
||
|
|
null
|
|
,
|
|
|
|
value_display: item.value_display
|
|
||
|
|
null
|
|
,
|
|
|
|
quality_flag: item.quality_flag
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const product_results = await db.product_results.bulkCreate(product_resultsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return product_results;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const product_results = await db.product_results.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.observed_at !== undefined) updatePayload.observed_at = data.observed_at;
|
|
|
|
|
|
if (data.reported_at !== undefined) updatePayload.reported_at = data.reported_at;
|
|
|
|
|
|
if (data.value_decimal !== undefined) updatePayload.value_decimal = data.value_decimal;
|
|
|
|
|
|
if (data.value_int !== undefined) updatePayload.value_int = data.value_int;
|
|
|
|
|
|
if (data.value_boolean !== undefined) updatePayload.value_boolean = data.value_boolean;
|
|
|
|
|
|
if (data.value_text !== undefined) updatePayload.value_text = data.value_text;
|
|
|
|
|
|
if (data.value_display !== undefined) updatePayload.value_display = data.value_display;
|
|
|
|
|
|
if (data.quality_flag !== undefined) updatePayload.quality_flag = data.quality_flag;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await product_results.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.product !== undefined) {
|
|
await product_results.setProduct(
|
|
|
|
data.product,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.metric_definition !== undefined) {
|
|
await product_results.setMetric_definition(
|
|
|
|
data.metric_definition,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.ingestion !== undefined) {
|
|
await product_results.setIngestion(
|
|
|
|
data.ingestion,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await product_results.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return product_results;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const product_results = await db.product_results.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of product_results) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of product_results) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return product_results;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const product_results = await db.product_results.findByPk(id, options);
|
|
|
|
await product_results.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await product_results.destroy({
|
|
transaction
|
|
});
|
|
|
|
return product_results;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const product_results = await db.product_results.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!product_results) {
|
|
return product_results;
|
|
}
|
|
|
|
const output = product_results.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.product = await product_results.getProduct({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.metric_definition = await product_results.getMetric_definition({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.ingestion = await product_results.getIngestion({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await product_results.getOrganizations({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.products,
|
|
as: 'product',
|
|
|
|
where: filter.product ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.product.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.product.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.metric_definitions,
|
|
as: 'metric_definition',
|
|
|
|
where: filter.metric_definition ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.metric_definition.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.metric_definition.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.result_ingestions,
|
|
as: 'ingestion',
|
|
|
|
where: filter.ingestion ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.ingestion.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
source_name: {
|
|
[Op.or]: filter.ingestion.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.value_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'product_results',
|
|
'value_text',
|
|
filter.value_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.value_display) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'product_results',
|
|
'value_display',
|
|
filter.value_display,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'product_results',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
observed_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
reported_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.observed_atRange) {
|
|
const [start, end] = filter.observed_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
observed_at: {
|
|
...where.observed_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
observed_at: {
|
|
...where.observed_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.reported_atRange) {
|
|
const [start, end] = filter.reported_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
reported_at: {
|
|
...where.reported_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
reported_at: {
|
|
...where.reported_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.value_decimalRange) {
|
|
const [start, end] = filter.value_decimalRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
value_decimal: {
|
|
...where.value_decimal,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
value_decimal: {
|
|
...where.value_decimal,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.value_intRange) {
|
|
const [start, end] = filter.value_intRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
value_int: {
|
|
...where.value_int,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
value_int: {
|
|
...where.value_int,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.value_boolean) {
|
|
where = {
|
|
...where,
|
|
value_boolean: filter.value_boolean,
|
|
};
|
|
}
|
|
|
|
if (filter.quality_flag) {
|
|
where = {
|
|
...where,
|
|
quality_flag: filter.quality_flag,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
|
|
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.product_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, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'product_results',
|
|
'value_display',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.product_results.findAll({
|
|
attributes: [ 'id', 'value_display' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['value_display', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.value_display,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|