38234-vm/backend/src/db/api/fund_requests.js
2026-02-05 22:38:34 +00:00

811 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 Fund_requestsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const fund_requests = await db.fund_requests.create(
{
id: data.id || undefined,
category: data.category
||
null
,
title: data.title
||
null
,
justification: data.justification
||
null
,
amount_requested_usd: data.amount_requested_usd
||
null
,
amount_approved_usd: data.amount_approved_usd
||
null
,
status: data.status
||
null
,
submitted_at: data.submitted_at
||
null
,
approved_at: data.approved_at
||
null
,
paid_at: data.paid_at
||
null
,
admin_decision_notes: data.admin_decision_notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await fund_requests.setStartup( data.startup || null, {
transaction,
});
await fund_requests.setRequested_by_user( data.requested_by_user || null, {
transaction,
});
await fund_requests.setHouse( data.house || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.fund_requests.getTableName(),
belongsToColumn: 'supporting_files',
belongsToId: fund_requests.id,
},
data.supporting_files,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.fund_requests.getTableName(),
belongsToColumn: 'receipt_images',
belongsToId: fund_requests.id,
},
data.receipt_images,
options,
);
return fund_requests;
}
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 fund_requestsData = data.map((item, index) => ({
id: item.id || undefined,
category: item.category
||
null
,
title: item.title
||
null
,
justification: item.justification
||
null
,
amount_requested_usd: item.amount_requested_usd
||
null
,
amount_approved_usd: item.amount_approved_usd
||
null
,
status: item.status
||
null
,
submitted_at: item.submitted_at
||
null
,
approved_at: item.approved_at
||
null
,
paid_at: item.paid_at
||
null
,
admin_decision_notes: item.admin_decision_notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const fund_requests = await db.fund_requests.bulkCreate(fund_requestsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < fund_requests.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.fund_requests.getTableName(),
belongsToColumn: 'supporting_files',
belongsToId: fund_requests[i].id,
},
data[i].supporting_files,
options,
);
}
for (let i = 0; i < fund_requests.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.fund_requests.getTableName(),
belongsToColumn: 'receipt_images',
belongsToId: fund_requests[i].id,
},
data[i].receipt_images,
options,
);
}
return fund_requests;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const fund_requests = await db.fund_requests.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.category !== undefined) updatePayload.category = data.category;
if (data.title !== undefined) updatePayload.title = data.title;
if (data.justification !== undefined) updatePayload.justification = data.justification;
if (data.amount_requested_usd !== undefined) updatePayload.amount_requested_usd = data.amount_requested_usd;
if (data.amount_approved_usd !== undefined) updatePayload.amount_approved_usd = data.amount_approved_usd;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.submitted_at !== undefined) updatePayload.submitted_at = data.submitted_at;
if (data.approved_at !== undefined) updatePayload.approved_at = data.approved_at;
if (data.paid_at !== undefined) updatePayload.paid_at = data.paid_at;
if (data.admin_decision_notes !== undefined) updatePayload.admin_decision_notes = data.admin_decision_notes;
updatePayload.updatedById = currentUser.id;
await fund_requests.update(updatePayload, {transaction});
if (data.startup !== undefined) {
await fund_requests.setStartup(
data.startup,
{ transaction }
);
}
if (data.requested_by_user !== undefined) {
await fund_requests.setRequested_by_user(
data.requested_by_user,
{ transaction }
);
}
if (data.house !== undefined) {
await fund_requests.setHouse(
data.house,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.fund_requests.getTableName(),
belongsToColumn: 'supporting_files',
belongsToId: fund_requests.id,
},
data.supporting_files,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.fund_requests.getTableName(),
belongsToColumn: 'receipt_images',
belongsToId: fund_requests.id,
},
data.receipt_images,
options,
);
return fund_requests;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const fund_requests = await db.fund_requests.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of fund_requests) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of fund_requests) {
await record.destroy({transaction});
}
});
return fund_requests;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const fund_requests = await db.fund_requests.findByPk(id, options);
await fund_requests.update({
deletedBy: currentUser.id
}, {
transaction,
});
await fund_requests.destroy({
transaction
});
return fund_requests;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const fund_requests = await db.fund_requests.findOne(
{ where },
{ transaction },
);
if (!fund_requests) {
return fund_requests;
}
const output = fund_requests.get({plain: true});
output.payments_fund_request = await fund_requests.getPayments_fund_request({
transaction
});
output.startup = await fund_requests.getStartup({
transaction
});
output.requested_by_user = await fund_requests.getRequested_by_user({
transaction
});
output.house = await fund_requests.getHouse({
transaction
});
output.supporting_files = await fund_requests.getSupporting_files({
transaction
});
output.receipt_images = await fund_requests.getReceipt_images({
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.startups,
as: 'startup',
where: filter.startup ? {
[Op.or]: [
{ id: { [Op.in]: filter.startup.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.startup.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'requested_by_user',
where: filter.requested_by_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.requested_by_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.requested_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.houses,
as: 'house',
where: filter.house ? {
[Op.or]: [
{ id: { [Op.in]: filter.house.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.house.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'supporting_files',
},
{
model: db.file,
as: 'receipt_images',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'fund_requests',
'title',
filter.title,
),
};
}
if (filter.justification) {
where = {
...where,
[Op.and]: Utils.ilike(
'fund_requests',
'justification',
filter.justification,
),
};
}
if (filter.admin_decision_notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'fund_requests',
'admin_decision_notes',
filter.admin_decision_notes,
),
};
}
if (filter.amount_requested_usdRange) {
const [start, end] = filter.amount_requested_usdRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
amount_requested_usd: {
...where.amount_requested_usd,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
amount_requested_usd: {
...where.amount_requested_usd,
[Op.lte]: end,
},
};
}
}
if (filter.amount_approved_usdRange) {
const [start, end] = filter.amount_approved_usdRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
amount_approved_usd: {
...where.amount_approved_usd,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
amount_approved_usd: {
...where.amount_approved_usd,
[Op.lte]: end,
},
};
}
}
if (filter.submitted_atRange) {
const [start, end] = filter.submitted_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
submitted_at: {
...where.submitted_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
submitted_at: {
...where.submitted_at,
[Op.lte]: end,
},
};
}
}
if (filter.approved_atRange) {
const [start, end] = filter.approved_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
approved_at: {
...where.approved_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
approved_at: {
...where.approved_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.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.fund_requests.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(
'fund_requests',
'title',
query,
),
],
};
}
const records = await db.fund_requests.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,
}));
}
};