38895-vm/backend/src/db/api/calculation_inputs.js
2026-03-01 01:07:46 +00:00

567 lines
14 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 Calculation_inputsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const calculation_inputs = await db.calculation_inputs.create(
{
id: data.id || undefined,
value_text: data.value_text
||
null
,
value_decimal: data.value_decimal
||
null
,
value_int: data.value_int
||
null
,
value_boolean: data.value_boolean
||
false
,
value_datetime: data.value_datetime
||
null
,
value_unit: data.value_unit
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await calculation_inputs.setCalculation_session( data.calculation_session || null, {
transaction,
});
await calculation_inputs.setCalculator_input( data.calculator_input || null, {
transaction,
});
return calculation_inputs;
}
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 calculation_inputsData = data.map((item, index) => ({
id: item.id || undefined,
value_text: item.value_text
||
null
,
value_decimal: item.value_decimal
||
null
,
value_int: item.value_int
||
null
,
value_boolean: item.value_boolean
||
false
,
value_datetime: item.value_datetime
||
null
,
value_unit: item.value_unit
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const calculation_inputs = await db.calculation_inputs.bulkCreate(calculation_inputsData, { transaction });
// For each item created, replace relation files
return calculation_inputs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const calculation_inputs = await db.calculation_inputs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.value_text !== undefined) updatePayload.value_text = data.value_text;
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_datetime !== undefined) updatePayload.value_datetime = data.value_datetime;
if (data.value_unit !== undefined) updatePayload.value_unit = data.value_unit;
updatePayload.updatedById = currentUser.id;
await calculation_inputs.update(updatePayload, {transaction});
if (data.calculation_session !== undefined) {
await calculation_inputs.setCalculation_session(
data.calculation_session,
{ transaction }
);
}
if (data.calculator_input !== undefined) {
await calculation_inputs.setCalculator_input(
data.calculator_input,
{ transaction }
);
}
return calculation_inputs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const calculation_inputs = await db.calculation_inputs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of calculation_inputs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of calculation_inputs) {
await record.destroy({transaction});
}
});
return calculation_inputs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const calculation_inputs = await db.calculation_inputs.findByPk(id, options);
await calculation_inputs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await calculation_inputs.destroy({
transaction
});
return calculation_inputs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const calculation_inputs = await db.calculation_inputs.findOne(
{ where },
{ transaction },
);
if (!calculation_inputs) {
return calculation_inputs;
}
const output = calculation_inputs.get({plain: true});
output.calculation_session = await calculation_inputs.getCalculation_session({
transaction
});
output.calculator_input = await calculation_inputs.getCalculator_input({
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.calculation_sessions,
as: 'calculation_session',
where: filter.calculation_session ? {
[Op.or]: [
{ id: { [Op.in]: filter.calculation_session.split('|').map(term => Utils.uuid(term)) } },
{
share_token: {
[Op.or]: filter.calculation_session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.calculator_inputs,
as: 'calculator_input',
where: filter.calculator_input ? {
[Op.or]: [
{ id: { [Op.in]: filter.calculator_input.split('|').map(term => Utils.uuid(term)) } },
{
label: {
[Op.or]: filter.calculator_input.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.value_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculation_inputs',
'value_text',
filter.value_text,
),
};
}
if (filter.value_unit) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculation_inputs',
'value_unit',
filter.value_unit,
),
};
}
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.value_datetimeRange) {
const [start, end] = filter.value_datetimeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
value_datetime: {
...where.value_datetime,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
value_datetime: {
...where.value_datetime,
[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.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.calculation_inputs.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(
'calculation_inputs',
'value_text',
query,
),
],
};
}
const records = await db.calculation_inputs.findAll({
attributes: [ 'id', 'value_text' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['value_text', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.value_text,
}));
}
};