34040/backend/src/db/api/transactions.js
2025-09-13 03:20:00 +00:00

520 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_id: data.transaction_id || null,
issue_date: data.issue_date || null,
due_time: data.due_time || null,
return_date: data.return_date || null,
renewal_status: data.renewal_status || null,
total_renewals: data.total_renewals || null,
book_status: data.book_status || null,
user_queries: data.user_queries || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await transactions.setUser(data.user || null, {
transaction,
});
await transactions.setBook(data.book || 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_id: item.transaction_id || null,
issue_date: item.issue_date || null,
due_time: item.due_time || null,
return_date: item.return_date || null,
renewal_status: item.renewal_status || null,
total_renewals: item.total_renewals || null,
book_status: item.book_status || null,
user_queries: item.user_queries || null,
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_id !== undefined)
updatePayload.transaction_id = data.transaction_id;
if (data.issue_date !== undefined)
updatePayload.issue_date = data.issue_date;
if (data.due_time !== undefined) updatePayload.due_time = data.due_time;
if (data.return_date !== undefined)
updatePayload.return_date = data.return_date;
if (data.renewal_status !== undefined)
updatePayload.renewal_status = data.renewal_status;
if (data.total_renewals !== undefined)
updatePayload.total_renewals = data.total_renewals;
if (data.book_status !== undefined)
updatePayload.book_status = data.book_status;
if (data.user_queries !== undefined)
updatePayload.user_queries = data.user_queries;
updatePayload.updatedById = currentUser.id;
await transactions.update(updatePayload, { transaction });
if (data.user !== undefined) {
await transactions.setUser(
data.user,
{ transaction },
);
}
if (data.book !== undefined) {
await transactions.setBook(
data.book,
{ 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.user = await transactions.getUser({
transaction,
});
output.book = await transactions.getBook({
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.books,
as: 'book',
where: filter.book
? {
[Op.or]: [
{
id: {
[Op.in]: filter.book
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
book_name: {
[Op.or]: filter.book
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.transaction_id) {
where = {
...where,
[Op.and]: Utils.ilike(
'transactions',
'transaction_id',
filter.transaction_id,
),
};
}
if (filter.user_queries) {
where = {
...where,
[Op.and]: Utils.ilike(
'transactions',
'user_queries',
filter.user_queries,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
issue_date: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
due_time: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.issue_dateRange) {
const [start, end] = filter.issue_dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
issue_date: {
...where.issue_date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
issue_date: {
...where.issue_date,
[Op.lte]: end,
},
};
}
}
if (filter.due_timeRange) {
const [start, end] = filter.due_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
due_time: {
...where.due_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
due_time: {
...where.due_time,
[Op.lte]: end,
},
};
}
}
if (filter.return_dateRange) {
const [start, end] = filter.return_dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
return_date: {
...where.return_date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
return_date: {
...where.return_date,
[Op.lte]: end,
},
};
}
}
if (filter.total_renewalsRange) {
const [start, end] = filter.total_renewalsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_renewals: {
...where.total_renewals,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_renewals: {
...where.total_renewals,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.renewal_status) {
where = {
...where,
renewal_status: filter.renewal_status,
};
}
if (filter.book_status) {
where = {
...where,
book_status: filter.book_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.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_id', query),
],
};
}
const records = await db.transactions.findAll({
attributes: ['id', 'transaction_id'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['transaction_id', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.transaction_id,
}));
}
};