39289-vm/backend/src/db/api/mining_fundamentals.js
2026-03-24 09:55:44 +00:00

795 lines
21 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 Mining_fundamentalsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const mining_fundamentals = await db.mining_fundamentals.create(
{
id: data.id || undefined,
reporting_period: data.reporting_period
||
null
,
period_end_at: data.period_end_at
||
null
,
production_oz: data.production_oz
||
null
,
all_in_sustaining_cost: data.all_in_sustaining_cost
||
null
,
cash_cost: data.cash_cost
||
null
,
reserves_oz: data.reserves_oz
||
null
,
revenue: data.revenue
||
null
,
ebitda: data.ebitda
||
null
,
free_cash_flow: data.free_cash_flow
||
null
,
debt_to_equity: data.debt_to_equity
||
null
,
operating_margin: data.operating_margin
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await mining_fundamentals.setMining_company( data.mining_company || null, {
transaction,
});
return mining_fundamentals;
}
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 mining_fundamentalsData = data.map((item, index) => ({
id: item.id || undefined,
reporting_period: item.reporting_period
||
null
,
period_end_at: item.period_end_at
||
null
,
production_oz: item.production_oz
||
null
,
all_in_sustaining_cost: item.all_in_sustaining_cost
||
null
,
cash_cost: item.cash_cost
||
null
,
reserves_oz: item.reserves_oz
||
null
,
revenue: item.revenue
||
null
,
ebitda: item.ebitda
||
null
,
free_cash_flow: item.free_cash_flow
||
null
,
debt_to_equity: item.debt_to_equity
||
null
,
operating_margin: item.operating_margin
||
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 mining_fundamentals = await db.mining_fundamentals.bulkCreate(mining_fundamentalsData, { transaction });
// For each item created, replace relation files
return mining_fundamentals;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const mining_fundamentals = await db.mining_fundamentals.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.reporting_period !== undefined) updatePayload.reporting_period = data.reporting_period;
if (data.period_end_at !== undefined) updatePayload.period_end_at = data.period_end_at;
if (data.production_oz !== undefined) updatePayload.production_oz = data.production_oz;
if (data.all_in_sustaining_cost !== undefined) updatePayload.all_in_sustaining_cost = data.all_in_sustaining_cost;
if (data.cash_cost !== undefined) updatePayload.cash_cost = data.cash_cost;
if (data.reserves_oz !== undefined) updatePayload.reserves_oz = data.reserves_oz;
if (data.revenue !== undefined) updatePayload.revenue = data.revenue;
if (data.ebitda !== undefined) updatePayload.ebitda = data.ebitda;
if (data.free_cash_flow !== undefined) updatePayload.free_cash_flow = data.free_cash_flow;
if (data.debt_to_equity !== undefined) updatePayload.debt_to_equity = data.debt_to_equity;
if (data.operating_margin !== undefined) updatePayload.operating_margin = data.operating_margin;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await mining_fundamentals.update(updatePayload, {transaction});
if (data.mining_company !== undefined) {
await mining_fundamentals.setMining_company(
data.mining_company,
{ transaction }
);
}
return mining_fundamentals;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const mining_fundamentals = await db.mining_fundamentals.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of mining_fundamentals) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of mining_fundamentals) {
await record.destroy({transaction});
}
});
return mining_fundamentals;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const mining_fundamentals = await db.mining_fundamentals.findByPk(id, options);
await mining_fundamentals.update({
deletedBy: currentUser.id
}, {
transaction,
});
await mining_fundamentals.destroy({
transaction
});
return mining_fundamentals;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const mining_fundamentals = await db.mining_fundamentals.findOne(
{ where },
{ transaction },
);
if (!mining_fundamentals) {
return mining_fundamentals;
}
const output = mining_fundamentals.get({plain: true});
output.mining_company = await mining_fundamentals.getMining_company({
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.mining_companies,
as: 'mining_company',
where: filter.mining_company ? {
[Op.or]: [
{ id: { [Op.in]: filter.mining_company.split('|').map(term => Utils.uuid(term)) } },
{
company_name: {
[Op.or]: filter.mining_company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'mining_fundamentals',
'notes',
filter.notes,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
period_end_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
period_end_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.period_end_atRange) {
const [start, end] = filter.period_end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
period_end_at: {
...where.period_end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
period_end_at: {
...where.period_end_at,
[Op.lte]: end,
},
};
}
}
if (filter.production_ozRange) {
const [start, end] = filter.production_ozRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
production_oz: {
...where.production_oz,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
production_oz: {
...where.production_oz,
[Op.lte]: end,
},
};
}
}
if (filter.all_in_sustaining_costRange) {
const [start, end] = filter.all_in_sustaining_costRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
all_in_sustaining_cost: {
...where.all_in_sustaining_cost,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
all_in_sustaining_cost: {
...where.all_in_sustaining_cost,
[Op.lte]: end,
},
};
}
}
if (filter.cash_costRange) {
const [start, end] = filter.cash_costRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
cash_cost: {
...where.cash_cost,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
cash_cost: {
...where.cash_cost,
[Op.lte]: end,
},
};
}
}
if (filter.reserves_ozRange) {
const [start, end] = filter.reserves_ozRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
reserves_oz: {
...where.reserves_oz,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
reserves_oz: {
...where.reserves_oz,
[Op.lte]: end,
},
};
}
}
if (filter.revenueRange) {
const [start, end] = filter.revenueRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
revenue: {
...where.revenue,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
revenue: {
...where.revenue,
[Op.lte]: end,
},
};
}
}
if (filter.ebitdaRange) {
const [start, end] = filter.ebitdaRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ebitda: {
...where.ebitda,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ebitda: {
...where.ebitda,
[Op.lte]: end,
},
};
}
}
if (filter.free_cash_flowRange) {
const [start, end] = filter.free_cash_flowRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
free_cash_flow: {
...where.free_cash_flow,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
free_cash_flow: {
...where.free_cash_flow,
[Op.lte]: end,
},
};
}
}
if (filter.debt_to_equityRange) {
const [start, end] = filter.debt_to_equityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
debt_to_equity: {
...where.debt_to_equity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
debt_to_equity: {
...where.debt_to_equity,
[Op.lte]: end,
},
};
}
}
if (filter.operating_marginRange) {
const [start, end] = filter.operating_marginRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
operating_margin: {
...where.operating_margin,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
operating_margin: {
...where.operating_margin,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.reporting_period) {
where = {
...where,
reporting_period: filter.reporting_period,
};
}
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.mining_fundamentals.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(
'mining_fundamentals',
'reporting_period',
query,
),
],
};
}
const records = await db.mining_fundamentals.findAll({
attributes: [ 'id', 'reporting_period' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['reporting_period', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.reporting_period,
}));
}
};