38684-vm/backend/src/db/api/budget_checks.js
2026-02-22 10:13:56 +00:00

623 lines
16 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 Budget_checksDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const budget_checks = await db.budget_checks.create(
{
id: data.id || undefined,
checked_at: data.checked_at
||
null
,
current_spend: data.current_spend
||
null
,
projected_spend: data.projected_spend
||
null
,
remaining_budget: data.remaining_budget
||
null
,
over_budget: data.over_budget
||
false
,
utilization_percent: data.utilization_percent
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await budget_checks.setBudget( data.budget || null, {
transaction,
});
await budget_checks.setReport( data.report || null, {
transaction,
});
return budget_checks;
}
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 budget_checksData = data.map((item, index) => ({
id: item.id || undefined,
checked_at: item.checked_at
||
null
,
current_spend: item.current_spend
||
null
,
projected_spend: item.projected_spend
||
null
,
remaining_budget: item.remaining_budget
||
null
,
over_budget: item.over_budget
||
false
,
utilization_percent: item.utilization_percent
||
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 budget_checks = await db.budget_checks.bulkCreate(budget_checksData, { transaction });
// For each item created, replace relation files
return budget_checks;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const budget_checks = await db.budget_checks.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.checked_at !== undefined) updatePayload.checked_at = data.checked_at;
if (data.current_spend !== undefined) updatePayload.current_spend = data.current_spend;
if (data.projected_spend !== undefined) updatePayload.projected_spend = data.projected_spend;
if (data.remaining_budget !== undefined) updatePayload.remaining_budget = data.remaining_budget;
if (data.over_budget !== undefined) updatePayload.over_budget = data.over_budget;
if (data.utilization_percent !== undefined) updatePayload.utilization_percent = data.utilization_percent;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await budget_checks.update(updatePayload, {transaction});
if (data.budget !== undefined) {
await budget_checks.setBudget(
data.budget,
{ transaction }
);
}
if (data.report !== undefined) {
await budget_checks.setReport(
data.report,
{ transaction }
);
}
return budget_checks;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const budget_checks = await db.budget_checks.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of budget_checks) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of budget_checks) {
await record.destroy({transaction});
}
});
return budget_checks;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const budget_checks = await db.budget_checks.findByPk(id, options);
await budget_checks.update({
deletedBy: currentUser.id
}, {
transaction,
});
await budget_checks.destroy({
transaction
});
return budget_checks;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const budget_checks = await db.budget_checks.findOne(
{ where },
{ transaction },
);
if (!budget_checks) {
return budget_checks;
}
const output = budget_checks.get({plain: true});
output.budget = await budget_checks.getBudget({
transaction
});
output.report = await budget_checks.getReport({
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.budgets,
as: 'budget',
where: filter.budget ? {
[Op.or]: [
{ id: { [Op.in]: filter.budget.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.budget.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.expense_reports,
as: 'report',
where: filter.report ? {
[Op.or]: [
{ id: { [Op.in]: filter.report.split('|').map(term => Utils.uuid(term)) } },
{
report_number: {
[Op.or]: filter.report.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'budget_checks',
'notes',
filter.notes,
),
};
}
if (filter.checked_atRange) {
const [start, end] = filter.checked_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
checked_at: {
...where.checked_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
checked_at: {
...where.checked_at,
[Op.lte]: end,
},
};
}
}
if (filter.current_spendRange) {
const [start, end] = filter.current_spendRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
current_spend: {
...where.current_spend,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
current_spend: {
...where.current_spend,
[Op.lte]: end,
},
};
}
}
if (filter.projected_spendRange) {
const [start, end] = filter.projected_spendRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
projected_spend: {
...where.projected_spend,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
projected_spend: {
...where.projected_spend,
[Op.lte]: end,
},
};
}
}
if (filter.remaining_budgetRange) {
const [start, end] = filter.remaining_budgetRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
remaining_budget: {
...where.remaining_budget,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
remaining_budget: {
...where.remaining_budget,
[Op.lte]: end,
},
};
}
}
if (filter.utilization_percentRange) {
const [start, end] = filter.utilization_percentRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
utilization_percent: {
...where.utilization_percent,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
utilization_percent: {
...where.utilization_percent,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.over_budget) {
where = {
...where,
over_budget: filter.over_budget,
};
}
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.budget_checks.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(
'budget_checks',
'notes',
query,
),
],
};
}
const records = await db.budget_checks.findAll({
attributes: [ 'id', 'notes' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['notes', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.notes,
}));
}
};