37854-vm/backend/src/db/api/withdrawals.js
2026-01-26 23:17:12 +00:00

587 lines
14 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 WithdrawalsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const withdrawals = await db.withdrawals.create(
{
id: data.id || undefined,
external_tx: data.external_tx
||
null
,
amount: data.amount
||
null
,
currency: data.currency
||
null
,
status: data.status
||
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 withdrawals.setUser( data.user || null, {
transaction,
});
await withdrawals.setWallet( data.wallet || null, {
transaction,
});
return withdrawals;
}
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 withdrawalsData = data.map((item, index) => ({
id: item.id || undefined,
external_tx: item.external_tx
||
null
,
amount: item.amount
||
null
,
currency: item.currency
||
null
,
status: item.status
||
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 withdrawals = await db.withdrawals.bulkCreate(withdrawalsData, { transaction });
// For each item created, replace relation files
return withdrawals;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const withdrawals = await db.withdrawals.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.external_tx !== undefined) updatePayload.external_tx = data.external_tx;
if (data.amount !== undefined) updatePayload.amount = data.amount;
if (data.currency !== undefined) updatePayload.currency = data.currency;
if (data.status !== undefined) updatePayload.status = data.status;
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 withdrawals.update(updatePayload, {transaction});
if (data.user !== undefined) {
await withdrawals.setUser(
data.user,
{ transaction }
);
}
if (data.wallet !== undefined) {
await withdrawals.setWallet(
data.wallet,
{ transaction }
);
}
return withdrawals;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const withdrawals = await db.withdrawals.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of withdrawals) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of withdrawals) {
await record.destroy({transaction});
}
});
return withdrawals;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const withdrawals = await db.withdrawals.findByPk(id, options);
await withdrawals.update({
deletedBy: currentUser.id
}, {
transaction,
});
await withdrawals.destroy({
transaction
});
return withdrawals;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const withdrawals = await db.withdrawals.findOne(
{ where },
{ transaction },
);
if (!withdrawals) {
return withdrawals;
}
const output = withdrawals.get({plain: true});
output.user = await withdrawals.getUser({
transaction
});
output.wallet = await withdrawals.getWallet({
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)) } },
{
label: {
[Op.or]: filter.wallet.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.external_tx) {
where = {
...where,
[Op.and]: Utils.ilike(
'withdrawals',
'external_tx',
filter.external_tx,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
requested_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
processed_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.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.currency) {
where = {
...where,
currency: filter.currency,
};
}
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.withdrawals.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(
'withdrawals',
'external_tx',
query,
),
],
};
}
const records = await db.withdrawals.findAll({
attributes: [ 'id', 'external_tx' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['external_tx', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.external_tx,
}));
}
};