39021-vm/backend/src/db/api/calculations.js
2026-03-05 23:43:17 +00:00

731 lines
18 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 CalculationsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const calculations = await db.calculations.create(
{
id: data.id || undefined,
source: data.source
||
null
,
input_expression: data.input_expression
||
null
,
operation_type: data.operation_type
||
null
,
bit_width: data.bit_width
||
null
,
signed_mode: data.signed_mode
||
false
,
rounding_mode: data.rounding_mode
||
null
,
result_dec_int: data.result_dec_int
||
null
,
result_hex: data.result_hex
||
null
,
result_dec: data.result_dec
||
null
,
result_oct: data.result_oct
||
null
,
result_bin: data.result_bin
||
null
,
has_error: data.has_error
||
false
,
error_message: data.error_message
||
null
,
calculated_at: data.calculated_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await calculations.setUser( data.user || null, {
transaction,
});
await calculations.setSession( data.session || null, {
transaction,
});
return calculations;
}
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 calculationsData = data.map((item, index) => ({
id: item.id || undefined,
source: item.source
||
null
,
input_expression: item.input_expression
||
null
,
operation_type: item.operation_type
||
null
,
bit_width: item.bit_width
||
null
,
signed_mode: item.signed_mode
||
false
,
rounding_mode: item.rounding_mode
||
null
,
result_dec_int: item.result_dec_int
||
null
,
result_hex: item.result_hex
||
null
,
result_dec: item.result_dec
||
null
,
result_oct: item.result_oct
||
null
,
result_bin: item.result_bin
||
null
,
has_error: item.has_error
||
false
,
error_message: item.error_message
||
null
,
calculated_at: item.calculated_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const calculations = await db.calculations.bulkCreate(calculationsData, { transaction });
// For each item created, replace relation files
return calculations;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const calculations = await db.calculations.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.source !== undefined) updatePayload.source = data.source;
if (data.input_expression !== undefined) updatePayload.input_expression = data.input_expression;
if (data.operation_type !== undefined) updatePayload.operation_type = data.operation_type;
if (data.bit_width !== undefined) updatePayload.bit_width = data.bit_width;
if (data.signed_mode !== undefined) updatePayload.signed_mode = data.signed_mode;
if (data.rounding_mode !== undefined) updatePayload.rounding_mode = data.rounding_mode;
if (data.result_dec_int !== undefined) updatePayload.result_dec_int = data.result_dec_int;
if (data.result_hex !== undefined) updatePayload.result_hex = data.result_hex;
if (data.result_dec !== undefined) updatePayload.result_dec = data.result_dec;
if (data.result_oct !== undefined) updatePayload.result_oct = data.result_oct;
if (data.result_bin !== undefined) updatePayload.result_bin = data.result_bin;
if (data.has_error !== undefined) updatePayload.has_error = data.has_error;
if (data.error_message !== undefined) updatePayload.error_message = data.error_message;
if (data.calculated_at !== undefined) updatePayload.calculated_at = data.calculated_at;
updatePayload.updatedById = currentUser.id;
await calculations.update(updatePayload, {transaction});
if (data.user !== undefined) {
await calculations.setUser(
data.user,
{ transaction }
);
}
if (data.session !== undefined) {
await calculations.setSession(
data.session,
{ transaction }
);
}
return calculations;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const calculations = await db.calculations.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of calculations) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of calculations) {
await record.destroy({transaction});
}
});
return calculations;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const calculations = await db.calculations.findByPk(id, options);
await calculations.update({
deletedBy: currentUser.id
}, {
transaction,
});
await calculations.destroy({
transaction
});
return calculations;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const calculations = await db.calculations.findOne(
{ where },
{ transaction },
);
if (!calculations) {
return calculations;
}
const output = calculations.get({plain: true});
output.ai_requests_calculation = await calculations.getAi_requests_calculation({
transaction
});
output.user = await calculations.getUser({
transaction
});
output.session = await calculations.getSession({
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.calculator_sessions,
as: 'session',
where: filter.session ? {
[Op.or]: [
{ id: { [Op.in]: filter.session.split('|').map(term => Utils.uuid(term)) } },
{
session_name: {
[Op.or]: filter.session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.input_expression) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculations',
'input_expression',
filter.input_expression,
),
};
}
if (filter.result_hex) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculations',
'result_hex',
filter.result_hex,
),
};
}
if (filter.result_dec) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculations',
'result_dec',
filter.result_dec,
),
};
}
if (filter.result_oct) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculations',
'result_oct',
filter.result_oct,
),
};
}
if (filter.result_bin) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculations',
'result_bin',
filter.result_bin,
),
};
}
if (filter.error_message) {
where = {
...where,
[Op.and]: Utils.ilike(
'calculations',
'error_message',
filter.error_message,
),
};
}
if (filter.result_dec_intRange) {
const [start, end] = filter.result_dec_intRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
result_dec_int: {
...where.result_dec_int,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
result_dec_int: {
...where.result_dec_int,
[Op.lte]: end,
},
};
}
}
if (filter.calculated_atRange) {
const [start, end] = filter.calculated_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
calculated_at: {
...where.calculated_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
calculated_at: {
...where.calculated_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.source) {
where = {
...where,
source: filter.source,
};
}
if (filter.operation_type) {
where = {
...where,
operation_type: filter.operation_type,
};
}
if (filter.bit_width) {
where = {
...where,
bit_width: filter.bit_width,
};
}
if (filter.signed_mode) {
where = {
...where,
signed_mode: filter.signed_mode,
};
}
if (filter.rounding_mode) {
where = {
...where,
rounding_mode: filter.rounding_mode,
};
}
if (filter.has_error) {
where = {
...where,
has_error: filter.has_error,
};
}
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.calculations.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(
'calculations',
'input_expression',
query,
),
],
};
}
const records = await db.calculations.findAll({
attributes: [ 'id', 'input_expression' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['input_expression', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.input_expression,
}));
}
};