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

783 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 Calculator_inputsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const calculator_inputs = await db.calculator_inputs.create(
{
id: data.id || undefined,
label: data.label
||
null
,
key: data.key
||
null
,
input_type: data.input_type
||
null
,
min_value: data.min_value
||
null
,
max_value: data.max_value
||
null
,
step_value: data.step_value
||
null
,
default_decimal: data.default_decimal
||
null
,
default_int: data.default_int
||
null
,
default_text: data.default_text
||
null
,
default_boolean: data.default_boolean
||
false
,
placeholder: data.placeholder
||
null
,
help_text: data.help_text
||
null
,
is_required: data.is_required
||
false
,
sort_order: data.sort_order
||
null
,
status: data.status
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await calculator_inputs.setCalculator( data.calculator || null, {
transaction,
});
return calculator_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 calculator_inputsData = data.map((item, index) => ({
id: item.id || undefined,
label: item.label
||
null
,
key: item.key
||
null
,
input_type: item.input_type
||
null
,
min_value: item.min_value
||
null
,
max_value: item.max_value
||
null
,
step_value: item.step_value
||
null
,
default_decimal: item.default_decimal
||
null
,
default_int: item.default_int
||
null
,
default_text: item.default_text
||
null
,
default_boolean: item.default_boolean
||
false
,
placeholder: item.placeholder
||
null
,
help_text: item.help_text
||
null
,
is_required: item.is_required
||
false
,
sort_order: item.sort_order
||
null
,
status: item.status
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const calculator_inputs = await db.calculator_inputs.bulkCreate(calculator_inputsData, { transaction });
// For each item created, replace relation files
return calculator_inputs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const calculator_inputs = await db.calculator_inputs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.label !== undefined) updatePayload.label = data.label;
if (data.key !== undefined) updatePayload.key = data.key;
if (data.input_type !== undefined) updatePayload.input_type = data.input_type;
if (data.min_value !== undefined) updatePayload.min_value = data.min_value;
if (data.max_value !== undefined) updatePayload.max_value = data.max_value;
if (data.step_value !== undefined) updatePayload.step_value = data.step_value;
if (data.default_decimal !== undefined) updatePayload.default_decimal = data.default_decimal;
if (data.default_int !== undefined) updatePayload.default_int = data.default_int;
if (data.default_text !== undefined) updatePayload.default_text = data.default_text;
if (data.default_boolean !== undefined) updatePayload.default_boolean = data.default_boolean;
if (data.placeholder !== undefined) updatePayload.placeholder = data.placeholder;
if (data.help_text !== undefined) updatePayload.help_text = data.help_text;
if (data.is_required !== undefined) updatePayload.is_required = data.is_required;
if (data.sort_order !== undefined) updatePayload.sort_order = data.sort_order;
if (data.status !== undefined) updatePayload.status = data.status;
updatePayload.updatedById = currentUser.id;
await calculator_inputs.update(updatePayload, {transaction});
if (data.calculator !== undefined) {
await calculator_inputs.setCalculator(
data.calculator,
{ transaction }
);
}
return calculator_inputs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const calculator_inputs = await db.calculator_inputs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of calculator_inputs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of calculator_inputs) {
await record.destroy({transaction});
}
});
return calculator_inputs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const calculator_inputs = await db.calculator_inputs.findByPk(id, options);
await calculator_inputs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await calculator_inputs.destroy({
transaction
});
return calculator_inputs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const calculator_inputs = await db.calculator_inputs.findOne(
{ where },
{ transaction },
);
if (!calculator_inputs) {
return calculator_inputs;
}
const output = calculator_inputs.get({plain: true});
output.calculator_input_choices_calculator_input = await calculator_inputs.getCalculator_input_choices_calculator_input({
transaction
});
output.calculation_inputs_calculator_input = await calculator_inputs.getCalculation_inputs_calculator_input({
transaction
});
output.calculator = await calculator_inputs.getCalculator({
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.calculators,
as: 'calculator',
where: filter.calculator ? {
[Op.or]: [
{ id: { [Op.in]: filter.calculator.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.calculator.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.label) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculator_inputs',
'label',
filter.label,
),
};
}
if (filter.key) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculator_inputs',
'key',
filter.key,
),
};
}
if (filter.default_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculator_inputs',
'default_text',
filter.default_text,
),
};
}
if (filter.placeholder) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculator_inputs',
'placeholder',
filter.placeholder,
),
};
}
if (filter.help_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculator_inputs',
'help_text',
filter.help_text,
),
};
}
if (filter.min_valueRange) {
const [start, end] = filter.min_valueRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
min_value: {
...where.min_value,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
min_value: {
...where.min_value,
[Op.lte]: end,
},
};
}
}
if (filter.max_valueRange) {
const [start, end] = filter.max_valueRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
max_value: {
...where.max_value,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
max_value: {
...where.max_value,
[Op.lte]: end,
},
};
}
}
if (filter.step_valueRange) {
const [start, end] = filter.step_valueRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
step_value: {
...where.step_value,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
step_value: {
...where.step_value,
[Op.lte]: end,
},
};
}
}
if (filter.default_decimalRange) {
const [start, end] = filter.default_decimalRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
default_decimal: {
...where.default_decimal,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
default_decimal: {
...where.default_decimal,
[Op.lte]: end,
},
};
}
}
if (filter.default_intRange) {
const [start, end] = filter.default_intRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
default_int: {
...where.default_int,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
default_int: {
...where.default_int,
[Op.lte]: end,
},
};
}
}
if (filter.sort_orderRange) {
const [start, end] = filter.sort_orderRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
sort_order: {
...where.sort_order,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
sort_order: {
...where.sort_order,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.input_type) {
where = {
...where,
input_type: filter.input_type,
};
}
if (filter.default_boolean) {
where = {
...where,
default_boolean: filter.default_boolean,
};
}
if (filter.is_required) {
where = {
...where,
is_required: filter.is_required,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.calculator_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(
'calculator_inputs',
'label',
query,
),
],
};
}
const records = await db.calculator_inputs.findAll({
attributes: [ 'id', 'label' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['label', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.label,
}));
}
};