38796-vm/backend/src/db/api/wallets.js
2026-02-26 23:34:46 +00:00

517 lines
12 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 WalletsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const wallets = await db.wallets.create(
{
id: data.id || undefined,
chain: data.chain
||
null
,
label: data.label
||
null
,
address: data.address
||
null
,
is_default_payout: data.is_default_payout
||
false
,
verified_at: data.verified_at
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await wallets.setUser( data.user || null, {
transaction,
});
return wallets;
}
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 walletsData = data.map((item, index) => ({
id: item.id || undefined,
chain: item.chain
||
null
,
label: item.label
||
null
,
address: item.address
||
null
,
is_default_payout: item.is_default_payout
||
false
,
verified_at: item.verified_at
||
null
,
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 wallets = await db.wallets.bulkCreate(walletsData, { transaction });
// For each item created, replace relation files
return wallets;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const wallets = await db.wallets.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.chain !== undefined) updatePayload.chain = data.chain;
if (data.label !== undefined) updatePayload.label = data.label;
if (data.address !== undefined) updatePayload.address = data.address;
if (data.is_default_payout !== undefined) updatePayload.is_default_payout = data.is_default_payout;
if (data.verified_at !== undefined) updatePayload.verified_at = data.verified_at;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await wallets.update(updatePayload, {transaction});
if (data.user !== undefined) {
await wallets.setUser(
data.user,
{ transaction }
);
}
return wallets;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const wallets = await db.wallets.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of wallets) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of wallets) {
await record.destroy({transaction});
}
});
return wallets;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const wallets = await db.wallets.findByPk(id, options);
await wallets.update({
deletedBy: currentUser.id
}, {
transaction,
});
await wallets.destroy({
transaction
});
return wallets;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const wallets = await db.wallets.findOne(
{ where },
{ transaction },
);
if (!wallets) {
return wallets;
}
const output = wallets.get({plain: true});
output.workers_payout_wallet = await wallets.getWorkers_payout_wallet({
transaction
});
output.payout_rules_wallet = await wallets.getPayout_rules_wallet({
transaction
});
output.payout_requests_wallet = await wallets.getPayout_requests_wallet({
transaction
});
output.user = await wallets.getUser({
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}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.label) {
where = {
...where,
[Op.and]: Utils.ilike(
'wallets',
'label',
filter.label,
),
};
}
if (filter.address) {
where = {
...where,
[Op.and]: Utils.ilike(
'wallets',
'address',
filter.address,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'wallets',
'notes',
filter.notes,
),
};
}
if (filter.verified_atRange) {
const [start, end] = filter.verified_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
verified_at: {
...where.verified_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
verified_at: {
...where.verified_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.chain) {
where = {
...where,
chain: filter.chain,
};
}
if (filter.is_default_payout) {
where = {
...where,
is_default_payout: filter.is_default_payout,
};
}
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.wallets.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(
'wallets',
'label',
query,
),
],
};
}
const records = await db.wallets.findAll({
attributes: [ 'id', 'label' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['label', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.label,
}));
}
};