37417-vm/backend/src/db/api/transactions.js
2026-01-13 10:06:40 +00:00

546 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 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,
transaction_ref: data.transaction_ref
||
null
,
total: data.total
||
null
,
tax: data.tax
||
null
,
tip: data.tip
||
null
,
timestamp: data.timestamp
||
null
,
refunded: data.refunded
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await transactions.setTab( data.tab || 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,
transaction_ref: item.transaction_ref
||
null
,
total: item.total
||
null
,
tax: item.tax
||
null
,
tip: item.tip
||
null
,
timestamp: item.timestamp
||
null
,
refunded: item.refunded
||
false
,
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.transaction_ref !== undefined) updatePayload.transaction_ref = data.transaction_ref;
if (data.total !== undefined) updatePayload.total = data.total;
if (data.tax !== undefined) updatePayload.tax = data.tax;
if (data.tip !== undefined) updatePayload.tip = data.tip;
if (data.timestamp !== undefined) updatePayload.timestamp = data.timestamp;
if (data.refunded !== undefined) updatePayload.refunded = data.refunded;
updatePayload.updatedById = currentUser.id;
await transactions.update(updatePayload, {transaction});
if (data.tab !== undefined) {
await transactions.setTab(
data.tab,
{ 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.tab = await transactions.getTab({
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.tabs,
as: 'tab',
where: filter.tab ? {
[Op.or]: [
{ id: { [Op.in]: filter.tab.split('|').map(term => Utils.uuid(term)) } },
{
tab_number: {
[Op.or]: filter.tab.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.transaction_ref) {
where = {
...where,
[Op.and]: Utils.ilike(
'transactions',
'transaction_ref',
filter.transaction_ref,
),
};
}
if (filter.totalRange) {
const [start, end] = filter.totalRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total: {
...where.total,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total: {
...where.total,
[Op.lte]: end,
},
};
}
}
if (filter.taxRange) {
const [start, end] = filter.taxRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
tax: {
...where.tax,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
tax: {
...where.tax,
[Op.lte]: end,
},
};
}
}
if (filter.tipRange) {
const [start, end] = filter.tipRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
tip: {
...where.tip,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
tip: {
...where.tip,
[Op.lte]: end,
},
};
}
}
if (filter.timestampRange) {
const [start, end] = filter.timestampRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
timestamp: {
...where.timestamp,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
timestamp: {
...where.timestamp,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.refunded) {
where = {
...where,
refunded: filter.refunded,
};
}
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',
'transaction_ref',
query,
),
],
};
}
const records = await db.transactions.findAll({
attributes: [ 'id', 'transaction_ref' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['transaction_ref', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.transaction_ref,
}));
}
};