727 lines
17 KiB
JavaScript
727 lines
17 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 TransactionsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const transactions = await db.transactions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
item_name: data.item_name
|
|
||
|
|
null
|
|
,
|
|
|
|
amount: data.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency: data.currency
|
|
||
|
|
null
|
|
,
|
|
|
|
is_recurring: data.is_recurring
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
is_planned: data.is_planned
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
outcome: data.outcome
|
|
||
|
|
null
|
|
,
|
|
|
|
source: data.source
|
|
||
|
|
null
|
|
,
|
|
|
|
occurred_at: data.occurred_at
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
external_ref: data.external_ref
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await transactions.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await transactions.setMerchant( data.merchant || null, {
|
|
transaction,
|
|
});
|
|
|
|
await transactions.setCategory( data.category || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await transactions.setTags(data.tags || [], {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
return transactions;
|
|
}
|
|
|
|
|
|
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 transactionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
item_name: item.item_name
|
|
||
|
|
null
|
|
,
|
|
|
|
amount: item.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency: item.currency
|
|
||
|
|
null
|
|
,
|
|
|
|
is_recurring: item.is_recurring
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
is_planned: item.is_planned
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
outcome: item.outcome
|
|
||
|
|
null
|
|
,
|
|
|
|
source: item.source
|
|
||
|
|
null
|
|
,
|
|
|
|
occurred_at: item.occurred_at
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
external_ref: item.external_ref
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const transactions = await db.transactions.bulkCreate(transactionsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return transactions;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const transactions = await db.transactions.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.item_name !== undefined) updatePayload.item_name = data.item_name;
|
|
|
|
|
|
if (data.amount !== undefined) updatePayload.amount = data.amount;
|
|
|
|
|
|
if (data.currency !== undefined) updatePayload.currency = data.currency;
|
|
|
|
|
|
if (data.is_recurring !== undefined) updatePayload.is_recurring = data.is_recurring;
|
|
|
|
|
|
if (data.is_planned !== undefined) updatePayload.is_planned = data.is_planned;
|
|
|
|
|
|
if (data.outcome !== undefined) updatePayload.outcome = data.outcome;
|
|
|
|
|
|
if (data.source !== undefined) updatePayload.source = data.source;
|
|
|
|
|
|
if (data.occurred_at !== undefined) updatePayload.occurred_at = data.occurred_at;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
if (data.external_ref !== undefined) updatePayload.external_ref = data.external_ref;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await transactions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await transactions.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.merchant !== undefined) {
|
|
await transactions.setMerchant(
|
|
|
|
data.merchant,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.category !== undefined) {
|
|
await transactions.setCategory(
|
|
|
|
data.category,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.tags !== undefined) {
|
|
await transactions.setTags(data.tags, { transaction });
|
|
}
|
|
|
|
|
|
|
|
|
|
return transactions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const transactions = await db.transactions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of transactions) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of transactions) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return transactions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const transactions = await db.transactions.findByPk(id, options);
|
|
|
|
await transactions.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await transactions.destroy({
|
|
transaction
|
|
});
|
|
|
|
return transactions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const transactions = await db.transactions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!transactions) {
|
|
return transactions;
|
|
}
|
|
|
|
const output = transactions.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.pause_checks_transaction = await transactions.getPause_checks_transaction({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await transactions.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.merchant = await transactions.getMerchant({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.category = await transactions.getCategory({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.tags = await transactions.getTags({
|
|
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.merchants,
|
|
as: 'merchant',
|
|
|
|
where: filter.merchant ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.merchant.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.merchant.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.tags,
|
|
as: 'tags',
|
|
required: false,
|
|
},
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.item_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'item_name',
|
|
filter.item_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.currency) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'currency',
|
|
filter.currency,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.external_ref) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'external_ref',
|
|
filter.external_ref,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.occurred_atRange) {
|
|
const [start, end] = filter.occurred_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
occurred_at: {
|
|
...where.occurred_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
occurred_at: {
|
|
...where.occurred_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.is_recurring) {
|
|
where = {
|
|
...where,
|
|
is_recurring: filter.is_recurring,
|
|
};
|
|
}
|
|
|
|
if (filter.is_planned) {
|
|
where = {
|
|
...where,
|
|
is_planned: filter.is_planned,
|
|
};
|
|
}
|
|
|
|
if (filter.outcome) {
|
|
where = {
|
|
...where,
|
|
outcome: filter.outcome,
|
|
};
|
|
}
|
|
|
|
if (filter.source) {
|
|
where = {
|
|
...where,
|
|
source: filter.source,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.tags) {
|
|
const searchTerms = filter.tags.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.tags,
|
|
as: 'tags_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
|
|
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.transactions.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(
|
|
'transactions',
|
|
'item_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.transactions.findAll({
|
|
attributes: [ 'id', 'item_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['item_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.item_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|