39923-vm/backend/src/db/api/credit_wallets.js
2026-05-07 09:53:46 +00:00

487 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 Credit_walletsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const credit_wallets = await db.credit_wallets.create(
{
id: data.id || undefined,
balance_credits: data.balance_credits
||
null
,
reserved_credits: data.reserved_credits
||
null
,
wallet_status: data.wallet_status
||
null
,
last_recalculated_at: data.last_recalculated_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await credit_wallets.setUser( data.user || null, {
transaction,
});
return credit_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 credit_walletsData = data.map((item, index) => ({
id: item.id || undefined,
balance_credits: item.balance_credits
||
null
,
reserved_credits: item.reserved_credits
||
null
,
wallet_status: item.wallet_status
||
null
,
last_recalculated_at: item.last_recalculated_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const credit_wallets = await db.credit_wallets.bulkCreate(credit_walletsData, { transaction });
// For each item created, replace relation files
return credit_wallets;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const credit_wallets = await db.credit_wallets.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.balance_credits !== undefined) updatePayload.balance_credits = data.balance_credits;
if (data.reserved_credits !== undefined) updatePayload.reserved_credits = data.reserved_credits;
if (data.wallet_status !== undefined) updatePayload.wallet_status = data.wallet_status;
if (data.last_recalculated_at !== undefined) updatePayload.last_recalculated_at = data.last_recalculated_at;
updatePayload.updatedById = currentUser.id;
await credit_wallets.update(updatePayload, {transaction});
if (data.user !== undefined) {
await credit_wallets.setUser(
data.user,
{ transaction }
);
}
return credit_wallets;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const credit_wallets = await db.credit_wallets.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of credit_wallets) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of credit_wallets) {
await record.destroy({transaction});
}
});
return credit_wallets;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const credit_wallets = await db.credit_wallets.findByPk(id, options);
await credit_wallets.update({
deletedBy: currentUser.id
}, {
transaction,
});
await credit_wallets.destroy({
transaction
});
return credit_wallets;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const credit_wallets = await db.credit_wallets.findOne(
{ where },
{ transaction },
);
if (!credit_wallets) {
return credit_wallets;
}
const output = credit_wallets.get({plain: true});
output.credit_transactions_wallet = await credit_wallets.getCredit_transactions_wallet({
transaction
});
output.user = await credit_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.balance_creditsRange) {
const [start, end] = filter.balance_creditsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
balance_credits: {
...where.balance_credits,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
balance_credits: {
...where.balance_credits,
[Op.lte]: end,
},
};
}
}
if (filter.reserved_creditsRange) {
const [start, end] = filter.reserved_creditsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
reserved_credits: {
...where.reserved_credits,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
reserved_credits: {
...where.reserved_credits,
[Op.lte]: end,
},
};
}
}
if (filter.last_recalculated_atRange) {
const [start, end] = filter.last_recalculated_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
last_recalculated_at: {
...where.last_recalculated_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
last_recalculated_at: {
...where.last_recalculated_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.wallet_status) {
where = {
...where,
wallet_status: filter.wallet_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.credit_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(
'credit_wallets',
'wallet_status',
query,
),
],
};
}
const records = await db.credit_wallets.findAll({
attributes: [ 'id', 'wallet_status' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['wallet_status', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.wallet_status,
}));
}
};