38422-vm/backend/src/db/api/ledger_entries.js
2026-02-14 09:16:18 +00:00

534 lines
13 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 Ledger_entriesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ledger_entries = await db.ledger_entries.create(
{
id: data.id || undefined,
occurred_at: data.occurred_at
||
null
,
entry_type: data.entry_type
||
null
,
amount: data.amount
||
null
,
balance_after: data.balance_after
||
null
,
reference_key: data.reference_key
||
null
,
note: data.note
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await ledger_entries.setPlayer( data.player || null, {
transaction,
});
return ledger_entries;
}
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 ledger_entriesData = data.map((item, index) => ({
id: item.id || undefined,
occurred_at: item.occurred_at
||
null
,
entry_type: item.entry_type
||
null
,
amount: item.amount
||
null
,
balance_after: item.balance_after
||
null
,
reference_key: item.reference_key
||
null
,
note: item.note
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const ledger_entries = await db.ledger_entries.bulkCreate(ledger_entriesData, { transaction });
// For each item created, replace relation files
return ledger_entries;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ledger_entries = await db.ledger_entries.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.occurred_at !== undefined) updatePayload.occurred_at = data.occurred_at;
if (data.entry_type !== undefined) updatePayload.entry_type = data.entry_type;
if (data.amount !== undefined) updatePayload.amount = data.amount;
if (data.balance_after !== undefined) updatePayload.balance_after = data.balance_after;
if (data.reference_key !== undefined) updatePayload.reference_key = data.reference_key;
if (data.note !== undefined) updatePayload.note = data.note;
updatePayload.updatedById = currentUser.id;
await ledger_entries.update(updatePayload, {transaction});
if (data.player !== undefined) {
await ledger_entries.setPlayer(
data.player,
{ transaction }
);
}
return ledger_entries;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ledger_entries = await db.ledger_entries.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of ledger_entries) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of ledger_entries) {
await record.destroy({transaction});
}
});
return ledger_entries;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ledger_entries = await db.ledger_entries.findByPk(id, options);
await ledger_entries.update({
deletedBy: currentUser.id
}, {
transaction,
});
await ledger_entries.destroy({
transaction
});
return ledger_entries;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const ledger_entries = await db.ledger_entries.findOne(
{ where },
{ transaction },
);
if (!ledger_entries) {
return ledger_entries;
}
const output = ledger_entries.get({plain: true});
output.player = await ledger_entries.getPlayer({
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.game_players,
as: 'player',
where: filter.player ? {
[Op.or]: [
{ id: { [Op.in]: filter.player.split('|').map(term => Utils.uuid(term)) } },
{
display_name: {
[Op.or]: filter.player.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.reference_key) {
where = {
...where,
[Op.and]: Utils.ilike(
'ledger_entries',
'reference_key',
filter.reference_key,
),
};
}
if (filter.note) {
where = {
...where,
[Op.and]: Utils.ilike(
'ledger_entries',
'note',
filter.note,
),
};
}
if (filter.occurred_atRange) {
const [start, end] = filter.occurred_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
occurred_at: {
...where.occurred_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
occurred_at: {
...where.occurred_at,
[Op.lte]: end,
},
};
}
}
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.balance_afterRange) {
const [start, end] = filter.balance_afterRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
balance_after: {
...where.balance_after,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
balance_after: {
...where.balance_after,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.entry_type) {
where = {
...where,
entry_type: filter.entry_type,
};
}
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.ledger_entries.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(
'ledger_entries',
'reference_key',
query,
),
],
};
}
const records = await db.ledger_entries.findAll({
attributes: [ 'id', 'reference_key' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['reference_key', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.reference_key,
}));
}
};