38343-vm/backend/src/db/api/validation_rules.js
2026-02-10 20:20:24 +00:00

557 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 Validation_rulesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const validation_rules = await db.validation_rules.create(
{
id: data.id || undefined,
name: data.name
||
null
,
rule_type: data.rule_type
||
null
,
enabled: data.enabled
||
false
,
int_value: data.int_value
||
null
,
decimal_value: data.decimal_value
||
null
,
window_start_at: data.window_start_at
||
null
,
window_end_at: data.window_end_at
||
null
,
description: data.description
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return validation_rules;
}
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 validation_rulesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
rule_type: item.rule_type
||
null
,
enabled: item.enabled
||
false
,
int_value: item.int_value
||
null
,
decimal_value: item.decimal_value
||
null
,
window_start_at: item.window_start_at
||
null
,
window_end_at: item.window_end_at
||
null
,
description: item.description
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const validation_rules = await db.validation_rules.bulkCreate(validation_rulesData, { transaction });
// For each item created, replace relation files
return validation_rules;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const validation_rules = await db.validation_rules.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.rule_type !== undefined) updatePayload.rule_type = data.rule_type;
if (data.enabled !== undefined) updatePayload.enabled = data.enabled;
if (data.int_value !== undefined) updatePayload.int_value = data.int_value;
if (data.decimal_value !== undefined) updatePayload.decimal_value = data.decimal_value;
if (data.window_start_at !== undefined) updatePayload.window_start_at = data.window_start_at;
if (data.window_end_at !== undefined) updatePayload.window_end_at = data.window_end_at;
if (data.description !== undefined) updatePayload.description = data.description;
updatePayload.updatedById = currentUser.id;
await validation_rules.update(updatePayload, {transaction});
return validation_rules;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const validation_rules = await db.validation_rules.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of validation_rules) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of validation_rules) {
await record.destroy({transaction});
}
});
return validation_rules;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const validation_rules = await db.validation_rules.findByPk(id, options);
await validation_rules.update({
deletedBy: currentUser.id
}, {
transaction,
});
await validation_rules.destroy({
transaction
});
return validation_rules;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const validation_rules = await db.validation_rules.findOne(
{ where },
{ transaction },
);
if (!validation_rules) {
return validation_rules;
}
const output = validation_rules.get({plain: true});
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 = [
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'validation_rules',
'name',
filter.name,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'validation_rules',
'description',
filter.description,
),
};
}
if (filter.int_valueRange) {
const [start, end] = filter.int_valueRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
int_value: {
...where.int_value,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
int_value: {
...where.int_value,
[Op.lte]: end,
},
};
}
}
if (filter.decimal_valueRange) {
const [start, end] = filter.decimal_valueRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
decimal_value: {
...where.decimal_value,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
decimal_value: {
...where.decimal_value,
[Op.lte]: end,
},
};
}
}
if (filter.window_start_atRange) {
const [start, end] = filter.window_start_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
window_start_at: {
...where.window_start_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
window_start_at: {
...where.window_start_at,
[Op.lte]: end,
},
};
}
}
if (filter.window_end_atRange) {
const [start, end] = filter.window_end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
window_end_at: {
...where.window_end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
window_end_at: {
...where.window_end_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.rule_type) {
where = {
...where,
rule_type: filter.rule_type,
};
}
if (filter.enabled) {
where = {
...where,
enabled: filter.enabled,
};
}
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.validation_rules.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(
'validation_rules',
'name',
query,
),
],
};
}
const records = await db.validation_rules.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};