411 lines
9.8 KiB
JavaScript
411 lines
9.8 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 RfpsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const rfps = await db.rfps.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title || null,
|
|
uploaded_at: data.uploaded_at || null,
|
|
status: data.status || null,
|
|
client: data.client || null,
|
|
deadline: data.deadline || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await rfps.setOrganization(currentUser.organization.id || null, {
|
|
transaction,
|
|
});
|
|
|
|
return rfps;
|
|
}
|
|
|
|
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 rfpsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
title: item.title || null,
|
|
uploaded_at: item.uploaded_at || null,
|
|
status: item.status || null,
|
|
client: item.client || null,
|
|
deadline: item.deadline || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const rfps = await db.rfps.bulkCreate(rfpsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return rfps;
|
|
}
|
|
|
|
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 rfps = await db.rfps.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
if (data.uploaded_at !== undefined)
|
|
updatePayload.uploaded_at = data.uploaded_at;
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
if (data.client !== undefined) updatePayload.client = data.client;
|
|
|
|
if (data.deadline !== undefined) updatePayload.deadline = data.deadline;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await rfps.update(updatePayload, { transaction });
|
|
|
|
if (data.organization !== undefined) {
|
|
await rfps.setOrganization(
|
|
globalAccess ? data.organization : currentUser.organization.id,
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return rfps;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const rfps = await db.rfps.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of rfps) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of rfps) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return rfps;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const rfps = await db.rfps.findByPk(id, options);
|
|
|
|
await rfps.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await rfps.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return rfps;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const rfps = await db.rfps.findOne({ where }, { transaction });
|
|
|
|
if (!rfps) {
|
|
return rfps;
|
|
}
|
|
|
|
const output = rfps.get({ plain: true });
|
|
|
|
output.proposals_rfp = await rfps.getProposals_rfp({
|
|
transaction,
|
|
});
|
|
|
|
output.schedules_c_rfp = await rfps.getSchedules_c_rfp({
|
|
transaction,
|
|
});
|
|
|
|
output.schedules_l_rfp = await rfps.getSchedules_l_rfp({
|
|
transaction,
|
|
});
|
|
|
|
output.schedules_m_rfp = await rfps.getSchedules_m_rfp({
|
|
transaction,
|
|
});
|
|
|
|
output.tasks_rfp = await rfps.getTasks_rfp({
|
|
transaction,
|
|
});
|
|
|
|
output.organization = await rfps.getOrganization({
|
|
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 userOrganization = (user && user.Organization?.id) || null;
|
|
|
|
if (userOrganization) {
|
|
if (options?.currentUser?.OrganizationId) {
|
|
where.OrganizationId = options.currentUser.OrganizationId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.organization,
|
|
as: 'organization',
|
|
|
|
where: filter.organization
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.organization
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.organization
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('rfps', 'title', filter.title),
|
|
};
|
|
}
|
|
|
|
if (filter.client) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('rfps', 'client', filter.client),
|
|
};
|
|
}
|
|
|
|
if (filter.uploaded_atRange) {
|
|
const [start, end] = filter.uploaded_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
uploaded_at: {
|
|
...where.uploaded_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
uploaded_at: {
|
|
...where.uploaded_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.deadlineRange) {
|
|
const [start, end] = filter.deadlineRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
deadline: {
|
|
...where.deadline,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
deadline: {
|
|
...where.deadline,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (globalAccess) {
|
|
delete where.OrganizationId;
|
|
}
|
|
|
|
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.rfps.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('rfps', 'title', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.rfps.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,
|
|
}));
|
|
}
|
|
};
|