38896-vm/backend/src/db/api/recurring_transaction_rules.js
2026-03-01 01:42:01 +00:00

862 lines
22 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 Recurring_transaction_rulesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recurring_transaction_rules = await db.recurring_transaction_rules.create(
{
id: data.id || undefined,
name: data.name
||
null
,
type: data.type
||
null
,
amount: data.amount
||
null
,
currency: data.currency
||
null
,
frequency: data.frequency
||
null
,
interval_count: data.interval_count
||
null
,
start_at: data.start_at
||
null
,
end_at: data.end_at
||
null
,
next_run_at: data.next_run_at
||
null
,
active: data.active
||
false
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await recurring_transaction_rules.setMerchant_profile( data.merchant_profile || null, {
transaction,
});
await recurring_transaction_rules.setUser( data.user || null, {
transaction,
});
await recurring_transaction_rules.setCategory( data.category || null, {
transaction,
});
await recurring_transaction_rules.setBudget( data.budget || null, {
transaction,
});
await recurring_transaction_rules.setOrganizations( data.organizations || null, {
transaction,
});
return recurring_transaction_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 recurring_transaction_rulesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
type: item.type
||
null
,
amount: item.amount
||
null
,
currency: item.currency
||
null
,
frequency: item.frequency
||
null
,
interval_count: item.interval_count
||
null
,
start_at: item.start_at
||
null
,
end_at: item.end_at
||
null
,
next_run_at: item.next_run_at
||
null
,
active: item.active
||
false
,
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 recurring_transaction_rules = await db.recurring_transaction_rules.bulkCreate(recurring_transaction_rulesData, { transaction });
// For each item created, replace relation files
return recurring_transaction_rules;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const globalAccess = currentUser.app_role?.globalAccess;
const recurring_transaction_rules = await db.recurring_transaction_rules.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.type !== undefined) updatePayload.type = data.type;
if (data.amount !== undefined) updatePayload.amount = data.amount;
if (data.currency !== undefined) updatePayload.currency = data.currency;
if (data.frequency !== undefined) updatePayload.frequency = data.frequency;
if (data.interval_count !== undefined) updatePayload.interval_count = data.interval_count;
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
if (data.end_at !== undefined) updatePayload.end_at = data.end_at;
if (data.next_run_at !== undefined) updatePayload.next_run_at = data.next_run_at;
if (data.active !== undefined) updatePayload.active = data.active;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await recurring_transaction_rules.update(updatePayload, {transaction});
if (data.merchant_profile !== undefined) {
await recurring_transaction_rules.setMerchant_profile(
data.merchant_profile,
{ transaction }
);
}
if (data.user !== undefined) {
await recurring_transaction_rules.setUser(
data.user,
{ transaction }
);
}
if (data.category !== undefined) {
await recurring_transaction_rules.setCategory(
data.category,
{ transaction }
);
}
if (data.budget !== undefined) {
await recurring_transaction_rules.setBudget(
data.budget,
{ transaction }
);
}
if (data.organizations !== undefined) {
await recurring_transaction_rules.setOrganizations(
data.organizations,
{ transaction }
);
}
return recurring_transaction_rules;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recurring_transaction_rules = await db.recurring_transaction_rules.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of recurring_transaction_rules) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of recurring_transaction_rules) {
await record.destroy({transaction});
}
});
return recurring_transaction_rules;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const recurring_transaction_rules = await db.recurring_transaction_rules.findByPk(id, options);
await recurring_transaction_rules.update({
deletedBy: currentUser.id
}, {
transaction,
});
await recurring_transaction_rules.destroy({
transaction
});
return recurring_transaction_rules;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const recurring_transaction_rules = await db.recurring_transaction_rules.findOne(
{ where },
{ transaction },
);
if (!recurring_transaction_rules) {
return recurring_transaction_rules;
}
const output = recurring_transaction_rules.get({plain: true});
output.merchant_profile = await recurring_transaction_rules.getMerchant_profile({
transaction
});
output.user = await recurring_transaction_rules.getUser({
transaction
});
output.category = await recurring_transaction_rules.getCategory({
transaction
});
output.budget = await recurring_transaction_rules.getBudget({
transaction
});
output.organizations = await recurring_transaction_rules.getOrganizations({
transaction
});
return output;
}
static async findAll(
filter,
globalAccess, options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
const user = (options && options.currentUser) || null;
const userOrganizations = (user && user.organizations?.id) || null;
if (userOrganizations) {
if (options?.currentUser?.organizationsId) {
where.organizationsId = options.currentUser.organizationsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.merchant_profiles,
as: 'merchant_profile',
where: filter.merchant_profile ? {
[Op.or]: [
{ id: { [Op.in]: filter.merchant_profile.split('|').map(term => Utils.uuid(term)) } },
{
display_name: {
[Op.or]: filter.merchant_profile.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
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.categories,
as: 'category',
where: filter.category ? {
[Op.or]: [
{ id: { [Op.in]: filter.category.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.category.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
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.organizations,
as: 'organizations',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'recurring_transaction_rules',
'name',
filter.name,
),
};
}
if (filter.currency) {
where = {
...where,
[Op.and]: Utils.ilike(
'recurring_transaction_rules',
'currency',
filter.currency,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'recurring_transaction_rules',
'notes',
filter.notes,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
start_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
next_run_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.amountRange) {
const [start, end] = filter.amountRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
amount: {
...where.amount,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
amount: {
...where.amount,
[Op.lte]: end,
},
};
}
}
if (filter.interval_countRange) {
const [start, end] = filter.interval_countRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
interval_count: {
...where.interval_count,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
interval_count: {
...where.interval_count,
[Op.lte]: end,
},
};
}
}
if (filter.start_atRange) {
const [start, end] = filter.start_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
start_at: {
...where.start_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
start_at: {
...where.start_at,
[Op.lte]: end,
},
};
}
}
if (filter.end_atRange) {
const [start, end] = filter.end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
end_at: {
...where.end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
end_at: {
...where.end_at,
[Op.lte]: end,
},
};
}
}
if (filter.next_run_atRange) {
const [start, end] = filter.next_run_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
next_run_at: {
...where.next_run_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
next_run_at: {
...where.next_run_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.type) {
where = {
...where,
type: filter.type,
};
}
if (filter.frequency) {
where = {
...where,
frequency: filter.frequency,
};
}
if (filter.active) {
where = {
...where,
active: filter.active,
};
}
if (filter.organizations) {
const listItems = filter.organizations.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationsId: {[Op.or]: listItems}
};
}
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,
},
};
}
}
}
if (globalAccess) {
delete where.organizationsId;
}
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.recurring_transaction_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, globalAccess, organizationId,) {
let where = {};
if (!globalAccess && organizationId) {
where.organizationId = organizationId;
}
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'recurring_transaction_rules',
'name',
query,
),
],
};
}
const records = await db.recurring_transaction_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,
}));
}
};