831 lines
20 KiB
JavaScript
831 lines
20 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,
|
|
|
|
type: data.type
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
amount: data.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
fee_amount: data.fee_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
tx_hash: data.tx_hash
|
|
||
|
|
null
|
|
,
|
|
|
|
confirmations: data.confirmations
|
|
||
|
|
null
|
|
,
|
|
|
|
required_confirmations: data.required_confirmations
|
|
||
|
|
null
|
|
,
|
|
|
|
from_address: data.from_address
|
|
||
|
|
null
|
|
,
|
|
|
|
to_address: data.to_address
|
|
||
|
|
null
|
|
,
|
|
|
|
note: data.note
|
|
||
|
|
null
|
|
,
|
|
|
|
requested_at: data.requested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
processed_at: data.processed_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await transactions.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await transactions.setWallet( data.wallet || null, {
|
|
transaction,
|
|
});
|
|
|
|
await transactions.setCurrency( data.currency || null, {
|
|
transaction,
|
|
});
|
|
|
|
await transactions.setNetwork( data.network || 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,
|
|
|
|
type: item.type
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
amount: item.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
fee_amount: item.fee_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
tx_hash: item.tx_hash
|
|
||
|
|
null
|
|
,
|
|
|
|
confirmations: item.confirmations
|
|
||
|
|
null
|
|
,
|
|
|
|
required_confirmations: item.required_confirmations
|
|
||
|
|
null
|
|
,
|
|
|
|
from_address: item.from_address
|
|
||
|
|
null
|
|
,
|
|
|
|
to_address: item.to_address
|
|
||
|
|
null
|
|
,
|
|
|
|
note: item.note
|
|
||
|
|
null
|
|
,
|
|
|
|
requested_at: item.requested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
processed_at: item.processed_at
|
|
||
|
|
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.type !== undefined) updatePayload.type = data.type;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.amount !== undefined) updatePayload.amount = data.amount;
|
|
|
|
|
|
if (data.fee_amount !== undefined) updatePayload.fee_amount = data.fee_amount;
|
|
|
|
|
|
if (data.tx_hash !== undefined) updatePayload.tx_hash = data.tx_hash;
|
|
|
|
|
|
if (data.confirmations !== undefined) updatePayload.confirmations = data.confirmations;
|
|
|
|
|
|
if (data.required_confirmations !== undefined) updatePayload.required_confirmations = data.required_confirmations;
|
|
|
|
|
|
if (data.from_address !== undefined) updatePayload.from_address = data.from_address;
|
|
|
|
|
|
if (data.to_address !== undefined) updatePayload.to_address = data.to_address;
|
|
|
|
|
|
if (data.note !== undefined) updatePayload.note = data.note;
|
|
|
|
|
|
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
|
|
|
|
|
|
if (data.processed_at !== undefined) updatePayload.processed_at = data.processed_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await transactions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await transactions.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.wallet !== undefined) {
|
|
await transactions.setWallet(
|
|
|
|
data.wallet,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.currency !== undefined) {
|
|
await transactions.setCurrency(
|
|
|
|
data.currency,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.network !== undefined) {
|
|
await transactions.setNetwork(
|
|
|
|
data.network,
|
|
|
|
{ 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.user = await transactions.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.wallet = await transactions.getWallet({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.currency = await transactions.getCurrency({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.network = await transactions.getNetwork({
|
|
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.wallets,
|
|
as: 'wallet',
|
|
|
|
where: filter.wallet ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.wallet.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
deposit_address: {
|
|
[Op.or]: filter.wallet.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.currencies,
|
|
as: 'currency',
|
|
|
|
where: filter.currency ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.currency.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
code: {
|
|
[Op.or]: filter.currency.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.networks,
|
|
as: 'network',
|
|
|
|
where: filter.network ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.network.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.network.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.tx_hash) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'tx_hash',
|
|
filter.tx_hash,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.from_address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'from_address',
|
|
filter.from_address,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.to_address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'to_address',
|
|
filter.to_address,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.note) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'transactions',
|
|
'note',
|
|
filter.note,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.fee_amountRange) {
|
|
const [start, end] = filter.fee_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
fee_amount: {
|
|
...where.fee_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
fee_amount: {
|
|
...where.fee_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.confirmationsRange) {
|
|
const [start, end] = filter.confirmationsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
confirmations: {
|
|
...where.confirmations,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
confirmations: {
|
|
...where.confirmations,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.required_confirmationsRange) {
|
|
const [start, end] = filter.required_confirmationsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
required_confirmations: {
|
|
...where.required_confirmations,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
required_confirmations: {
|
|
...where.required_confirmations,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.requested_atRange) {
|
|
const [start, end] = filter.requested_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
requested_at: {
|
|
...where.requested_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
requested_at: {
|
|
...where.requested_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.processed_atRange) {
|
|
const [start, end] = filter.processed_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
processed_at: {
|
|
...where.processed_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
processed_at: {
|
|
...where.processed_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.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
'tx_hash',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.transactions.findAll({
|
|
attributes: [ 'id', 'tx_hash' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['tx_hash', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.tx_hash,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|