467 lines
11 KiB
JavaScript
467 lines
11 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 InvestmentsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const investments = await db.investments.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
investment_amount: data.investment_amount || null,
|
|
type: data.type || null,
|
|
date: data.date || null,
|
|
expected_returns: data.expected_returns || null,
|
|
actual_returns: data.actual_returns || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await investments.setChama(data.chama || null, {
|
|
transaction,
|
|
});
|
|
|
|
await investments.setCompanies(data.companies || null, {
|
|
transaction,
|
|
});
|
|
|
|
return investments;
|
|
}
|
|
|
|
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 investmentsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
investment_amount: item.investment_amount || null,
|
|
type: item.type || null,
|
|
date: item.date || null,
|
|
expected_returns: item.expected_returns || null,
|
|
actual_returns: item.actual_returns || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const investments = await db.investments.bulkCreate(investmentsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return investments;
|
|
}
|
|
|
|
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 investments = await db.investments.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.investment_amount !== undefined)
|
|
updatePayload.investment_amount = data.investment_amount;
|
|
|
|
if (data.type !== undefined) updatePayload.type = data.type;
|
|
|
|
if (data.date !== undefined) updatePayload.date = data.date;
|
|
|
|
if (data.expected_returns !== undefined)
|
|
updatePayload.expected_returns = data.expected_returns;
|
|
|
|
if (data.actual_returns !== undefined)
|
|
updatePayload.actual_returns = data.actual_returns;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await investments.update(updatePayload, { transaction });
|
|
|
|
if (data.chama !== undefined) {
|
|
await investments.setChama(
|
|
data.chama,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.companies !== undefined) {
|
|
await investments.setCompanies(
|
|
data.companies,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return investments;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const investments = await db.investments.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of investments) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of investments) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return investments;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const investments = await db.investments.findByPk(id, options);
|
|
|
|
await investments.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await investments.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return investments;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const investments = await db.investments.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!investments) {
|
|
return investments;
|
|
}
|
|
|
|
const output = investments.get({ plain: true });
|
|
|
|
output.chama = await investments.getChama({
|
|
transaction,
|
|
});
|
|
|
|
output.companies = await investments.getCompanies({
|
|
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 userCompanies = (user && user.companies?.id) || null;
|
|
|
|
if (userCompanies) {
|
|
if (options?.currentUser?.companiesId) {
|
|
where.companiesId = options.currentUser.companiesId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.chamas,
|
|
as: 'chama',
|
|
|
|
where: filter.chama
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.chama
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.chama
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.companies,
|
|
as: 'companies',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.type) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('investments', 'type', filter.type),
|
|
};
|
|
}
|
|
|
|
if (filter.investment_amountRange) {
|
|
const [start, end] = filter.investment_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
investment_amount: {
|
|
...where.investment_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
investment_amount: {
|
|
...where.investment_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.dateRange) {
|
|
const [start, end] = filter.dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
date: {
|
|
...where.date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
date: {
|
|
...where.date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.expected_returnsRange) {
|
|
const [start, end] = filter.expected_returnsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
expected_returns: {
|
|
...where.expected_returns,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
expected_returns: {
|
|
...where.expected_returns,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.actual_returnsRange) {
|
|
const [start, end] = filter.actual_returnsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
actual_returns: {
|
|
...where.actual_returns,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
actual_returns: {
|
|
...where.actual_returns,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.companies) {
|
|
const listItems = filter.companies.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
companiesId: { [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.companiesId;
|
|
}
|
|
|
|
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.investments.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('investments', 'type', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.investments.findAll({
|
|
attributes: ['id', 'type'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['type', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.type,
|
|
}));
|
|
}
|
|
};
|