822 lines
20 KiB
JavaScript
822 lines
20 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 Budget_itemsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const budget_items = await db.budget_items.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title
|
|
||
|
|
null
|
|
,
|
|
|
|
category: data.category
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
amount_planned: data.amount_planned
|
|
||
|
|
null
|
|
,
|
|
|
|
amount_actual: data.amount_actual
|
|
||
|
|
null
|
|
,
|
|
|
|
tax_amount: data.tax_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
tip_amount: data.tip_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
due_at: data.due_at
|
|
||
|
|
null
|
|
,
|
|
|
|
paid_at: data.paid_at
|
|
||
|
|
null
|
|
,
|
|
|
|
payment_method: data.payment_method
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await budget_items.setEvent( data.event || null, {
|
|
transaction,
|
|
});
|
|
|
|
await budget_items.setVendor_contract( data.vendor_contract || null, {
|
|
transaction,
|
|
});
|
|
|
|
await budget_items.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.budget_items.getTableName(),
|
|
belongsToColumn: 'receipts',
|
|
belongsToId: budget_items.id,
|
|
},
|
|
data.receipts,
|
|
options,
|
|
);
|
|
|
|
|
|
return budget_items;
|
|
}
|
|
|
|
|
|
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 budget_itemsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
title: item.title
|
|
||
|
|
null
|
|
,
|
|
|
|
category: item.category
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
amount_planned: item.amount_planned
|
|
||
|
|
null
|
|
,
|
|
|
|
amount_actual: item.amount_actual
|
|
||
|
|
null
|
|
,
|
|
|
|
tax_amount: item.tax_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
tip_amount: item.tip_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
due_at: item.due_at
|
|
||
|
|
null
|
|
,
|
|
|
|
paid_at: item.paid_at
|
|
||
|
|
null
|
|
,
|
|
|
|
payment_method: item.payment_method
|
|
||
|
|
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 budget_items = await db.budget_items.bulkCreate(budget_itemsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < budget_items.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.budget_items.getTableName(),
|
|
belongsToColumn: 'receipts',
|
|
belongsToId: budget_items[i].id,
|
|
},
|
|
data[i].receipts,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return budget_items;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const budget_items = await db.budget_items.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
|
|
if (data.category !== undefined) updatePayload.category = data.category;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.amount_planned !== undefined) updatePayload.amount_planned = data.amount_planned;
|
|
|
|
|
|
if (data.amount_actual !== undefined) updatePayload.amount_actual = data.amount_actual;
|
|
|
|
|
|
if (data.tax_amount !== undefined) updatePayload.tax_amount = data.tax_amount;
|
|
|
|
|
|
if (data.tip_amount !== undefined) updatePayload.tip_amount = data.tip_amount;
|
|
|
|
|
|
if (data.due_at !== undefined) updatePayload.due_at = data.due_at;
|
|
|
|
|
|
if (data.paid_at !== undefined) updatePayload.paid_at = data.paid_at;
|
|
|
|
|
|
if (data.payment_method !== undefined) updatePayload.payment_method = data.payment_method;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await budget_items.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.event !== undefined) {
|
|
await budget_items.setEvent(
|
|
|
|
data.event,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.vendor_contract !== undefined) {
|
|
await budget_items.setVendor_contract(
|
|
|
|
data.vendor_contract,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await budget_items.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.budget_items.getTableName(),
|
|
belongsToColumn: 'receipts',
|
|
belongsToId: budget_items.id,
|
|
},
|
|
data.receipts,
|
|
options,
|
|
);
|
|
|
|
|
|
return budget_items;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const budget_items = await db.budget_items.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of budget_items) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of budget_items) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return budget_items;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const budget_items = await db.budget_items.findByPk(id, options);
|
|
|
|
await budget_items.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await budget_items.destroy({
|
|
transaction
|
|
});
|
|
|
|
return budget_items;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const budget_items = await db.budget_items.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!budget_items) {
|
|
return budget_items;
|
|
}
|
|
|
|
const output = budget_items.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.payments_budget_item = await budget_items.getPayments_budget_item({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
output.event = await budget_items.getEvent({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.vendor_contract = await budget_items.getVendor_contract({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.receipts = await budget_items.getReceipts({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await budget_items.getOrganizations({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.events,
|
|
as: 'event',
|
|
|
|
where: filter.event ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.event.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.event.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.vendor_contracts,
|
|
as: 'vendor_contract',
|
|
|
|
where: filter.vendor_contract ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.vendor_contract.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.vendor_contract.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'receipts',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'budget_items',
|
|
'title',
|
|
filter.title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'budget_items',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.amount_plannedRange) {
|
|
const [start, end] = filter.amount_plannedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
amount_planned: {
|
|
...where.amount_planned,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
amount_planned: {
|
|
...where.amount_planned,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.amount_actualRange) {
|
|
const [start, end] = filter.amount_actualRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
amount_actual: {
|
|
...where.amount_actual,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
amount_actual: {
|
|
...where.amount_actual,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.tax_amountRange) {
|
|
const [start, end] = filter.tax_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
tax_amount: {
|
|
...where.tax_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
tax_amount: {
|
|
...where.tax_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.tip_amountRange) {
|
|
const [start, end] = filter.tip_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
tip_amount: {
|
|
...where.tip_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
tip_amount: {
|
|
...where.tip_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.due_atRange) {
|
|
const [start, end] = filter.due_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
due_at: {
|
|
...where.due_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
due_at: {
|
|
...where.due_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.paid_atRange) {
|
|
const [start, end] = filter.paid_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
paid_at: {
|
|
...where.paid_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
paid_at: {
|
|
...where.paid_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.category) {
|
|
where = {
|
|
...where,
|
|
category: filter.category,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
if (filter.payment_method) {
|
|
where = {
|
|
...where,
|
|
payment_method: filter.payment_method,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
|
|
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.budget_items.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, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'budget_items',
|
|
'title',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.budget_items.findAll({
|
|
attributes: [ 'id', 'title' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.title,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|