850 lines
22 KiB
JavaScript
850 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 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,
|
|
|
|
direction: data.direction
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
amount: data.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency_code: data.currency_code
|
|
||
|
|
null
|
|
,
|
|
|
|
gbp_equivalent_amount: data.gbp_equivalent_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
authorised_at: data.authorised_at
|
|
||
|
|
null
|
|
,
|
|
|
|
settled_at: data.settled_at
|
|
||
|
|
null
|
|
,
|
|
|
|
category: data.category
|
|
||
|
|
null
|
|
,
|
|
|
|
auto_optimised: data.auto_optimised
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
cashback_earned_amount: data.cashback_earned_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
fx_fee_amount: data.fx_fee_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_savings_amount: data.estimated_savings_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
description: data.description
|
|
||
|
|
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.setPaid_with_card( data.paid_with_card || null, {
|
|
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,
|
|
|
|
direction: item.direction
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
amount: item.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency_code: item.currency_code
|
|
||
|
|
null
|
|
,
|
|
|
|
gbp_equivalent_amount: item.gbp_equivalent_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
authorised_at: item.authorised_at
|
|
||
|
|
null
|
|
,
|
|
|
|
settled_at: item.settled_at
|
|
||
|
|
null
|
|
,
|
|
|
|
category: item.category
|
|
||
|
|
null
|
|
,
|
|
|
|
auto_optimised: item.auto_optimised
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
cashback_earned_amount: item.cashback_earned_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
fx_fee_amount: item.fx_fee_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_savings_amount: item.estimated_savings_amount
|
|
||
|
|
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 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.direction !== undefined) updatePayload.direction = data.direction;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.amount !== undefined) updatePayload.amount = data.amount;
|
|
|
|
|
|
if (data.currency_code !== undefined) updatePayload.currency_code = data.currency_code;
|
|
|
|
|
|
if (data.gbp_equivalent_amount !== undefined) updatePayload.gbp_equivalent_amount = data.gbp_equivalent_amount;
|
|
|
|
|
|
if (data.authorised_at !== undefined) updatePayload.authorised_at = data.authorised_at;
|
|
|
|
|
|
if (data.settled_at !== undefined) updatePayload.settled_at = data.settled_at;
|
|
|
|
|
|
if (data.category !== undefined) updatePayload.category = data.category;
|
|
|
|
|
|
if (data.auto_optimised !== undefined) updatePayload.auto_optimised = data.auto_optimised;
|
|
|
|
|
|
if (data.cashback_earned_amount !== undefined) updatePayload.cashback_earned_amount = data.cashback_earned_amount;
|
|
|
|
|
|
if (data.fx_fee_amount !== undefined) updatePayload.fx_fee_amount = data.fx_fee_amount;
|
|
|
|
|
|
if (data.estimated_savings_amount !== undefined) updatePayload.estimated_savings_amount = data.estimated_savings_amount;
|
|
|
|
|
|
if (data.description !== undefined) updatePayload.description = data.description;
|
|
|
|
|
|
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.paid_with_card !== undefined) {
|
|
await transactions.setPaid_with_card(
|
|
|
|
data.paid_with_card,
|
|
|
|
{ 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.transaction_allocations_transaction = await transactions.getTransaction_allocations_transaction({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.payment_strategies_transaction = await transactions.getPayment_strategies_transaction({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await transactions.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.merchant = await transactions.getMerchant({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.paid_with_card = await transactions.getPaid_with_card({
|
|
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.payment_cards,
|
|
as: 'paid_with_card',
|
|
|
|
where: filter.paid_with_card ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.paid_with_card.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
card_nickname: {
|
|
[Op.or]: filter.paid_with_card.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.currency_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'currency_code',
|
|
filter.currency_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.description) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'description',
|
|
filter.description,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
authorised_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
settled_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.gbp_equivalent_amountRange) {
|
|
const [start, end] = filter.gbp_equivalent_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
gbp_equivalent_amount: {
|
|
...where.gbp_equivalent_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
gbp_equivalent_amount: {
|
|
...where.gbp_equivalent_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.authorised_atRange) {
|
|
const [start, end] = filter.authorised_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
authorised_at: {
|
|
...where.authorised_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
authorised_at: {
|
|
...where.authorised_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.settled_atRange) {
|
|
const [start, end] = filter.settled_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
settled_at: {
|
|
...where.settled_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
settled_at: {
|
|
...where.settled_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.cashback_earned_amountRange) {
|
|
const [start, end] = filter.cashback_earned_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
cashback_earned_amount: {
|
|
...where.cashback_earned_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
cashback_earned_amount: {
|
|
...where.cashback_earned_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.fx_fee_amountRange) {
|
|
const [start, end] = filter.fx_fee_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
fx_fee_amount: {
|
|
...where.fx_fee_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
fx_fee_amount: {
|
|
...where.fx_fee_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.estimated_savings_amountRange) {
|
|
const [start, end] = filter.estimated_savings_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_savings_amount: {
|
|
...where.estimated_savings_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_savings_amount: {
|
|
...where.estimated_savings_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.direction) {
|
|
where = {
|
|
...where,
|
|
direction: filter.direction,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
if (filter.category) {
|
|
where = {
|
|
...where,
|
|
category: filter.category,
|
|
};
|
|
}
|
|
|
|
if (filter.auto_optimised) {
|
|
where = {
|
|
...where,
|
|
auto_optimised: filter.auto_optimised,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
'description',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.transactions.findAll({
|
|
attributes: [ 'id', 'description' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['description', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.description,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|