30027/backend/src/db/api/financial_overviews.js
2025-03-19 07:53:51 +00:00

379 lines
9.3 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 Financial_overviewsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const financial_overviews = await db.financial_overviews.create(
{
id: data.id || undefined,
total_income: data.total_income || null,
total_expenses: data.total_expenses || null,
savings: data.savings || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await financial_overviews.setUser(data.user || null, {
transaction,
});
return financial_overviews;
}
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 financial_overviewsData = data.map((item, index) => ({
id: item.id || undefined,
total_income: item.total_income || null,
total_expenses: item.total_expenses || null,
savings: item.savings || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const financial_overviews = await db.financial_overviews.bulkCreate(
financial_overviewsData,
{ transaction },
);
// For each item created, replace relation files
return financial_overviews;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const financial_overviews = await db.financial_overviews.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.total_income !== undefined)
updatePayload.total_income = data.total_income;
if (data.total_expenses !== undefined)
updatePayload.total_expenses = data.total_expenses;
if (data.savings !== undefined) updatePayload.savings = data.savings;
updatePayload.updatedById = currentUser.id;
await financial_overviews.update(updatePayload, { transaction });
if (data.user !== undefined) {
await financial_overviews.setUser(
data.user,
{ transaction },
);
}
return financial_overviews;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const financial_overviews = await db.financial_overviews.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of financial_overviews) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of financial_overviews) {
await record.destroy({ transaction });
}
});
return financial_overviews;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const financial_overviews = await db.financial_overviews.findByPk(
id,
options,
);
await financial_overviews.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await financial_overviews.destroy({
transaction,
});
return financial_overviews;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const financial_overviews = await db.financial_overviews.findOne(
{ where },
{ transaction },
);
if (!financial_overviews) {
return financial_overviews;
}
const output = financial_overviews.get({ plain: true });
output.user = await financial_overviews.getUser({
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}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.total_incomeRange) {
const [start, end] = filter.total_incomeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_income: {
...where.total_income,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_income: {
...where.total_income,
[Op.lte]: end,
},
};
}
}
if (filter.total_expensesRange) {
const [start, end] = filter.total_expensesRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_expenses: {
...where.total_expenses,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_expenses: {
...where.total_expenses,
[Op.lte]: end,
},
};
}
}
if (filter.savingsRange) {
const [start, end] = filter.savingsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
savings: {
...where.savings,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
savings: {
...where.savings,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
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.financial_overviews.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('financial_overviews', 'total_income', query),
],
};
}
const records = await db.financial_overviews.findAll({
attributes: ['id', 'total_income'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['total_income', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.total_income,
}));
}
};