907 lines
23 KiB
JavaScript
907 lines
23 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 ProjectsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const projects = await db.projects.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
project_title: data.project_title
|
|
||
|
|
null
|
|
,
|
|
|
|
project_description: data.project_description
|
|
||
|
|
null
|
|
,
|
|
|
|
project_type: data.project_type
|
|
||
|
|
null
|
|
,
|
|
|
|
engagement_model: data.engagement_model
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_min: data.budget_min
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_max: data.budget_max
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_currency: data.budget_currency
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_hours: data.estimated_hours
|
|
||
|
|
null
|
|
,
|
|
|
|
location_preference: data.location_preference
|
|
||
|
|
null
|
|
,
|
|
|
|
remote_ok: data.remote_ok
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
desired_start_at: data.desired_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
desired_end_at: data.desired_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await projects.setCompany( data.company || null, {
|
|
transaction,
|
|
});
|
|
|
|
await projects.setOwner_user( data.owner_user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await projects.setErp_system( data.erp_system || null, {
|
|
transaction,
|
|
});
|
|
|
|
await projects.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.projects.getTableName(),
|
|
belongsToColumn: 'attachment_files',
|
|
belongsToId: projects.id,
|
|
},
|
|
data.attachment_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return projects;
|
|
}
|
|
|
|
|
|
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 projectsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
project_title: item.project_title
|
|
||
|
|
null
|
|
,
|
|
|
|
project_description: item.project_description
|
|
||
|
|
null
|
|
,
|
|
|
|
project_type: item.project_type
|
|
||
|
|
null
|
|
,
|
|
|
|
engagement_model: item.engagement_model
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_min: item.budget_min
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_max: item.budget_max
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_currency: item.budget_currency
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_hours: item.estimated_hours
|
|
||
|
|
null
|
|
,
|
|
|
|
location_preference: item.location_preference
|
|
||
|
|
null
|
|
,
|
|
|
|
remote_ok: item.remote_ok
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
desired_start_at: item.desired_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
desired_end_at: item.desired_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const projects = await db.projects.bulkCreate(projectsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < projects.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.projects.getTableName(),
|
|
belongsToColumn: 'attachment_files',
|
|
belongsToId: projects[i].id,
|
|
},
|
|
data[i].attachment_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return projects;
|
|
}
|
|
|
|
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 projects = await db.projects.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.project_title !== undefined) updatePayload.project_title = data.project_title;
|
|
|
|
|
|
if (data.project_description !== undefined) updatePayload.project_description = data.project_description;
|
|
|
|
|
|
if (data.project_type !== undefined) updatePayload.project_type = data.project_type;
|
|
|
|
|
|
if (data.engagement_model !== undefined) updatePayload.engagement_model = data.engagement_model;
|
|
|
|
|
|
if (data.budget_min !== undefined) updatePayload.budget_min = data.budget_min;
|
|
|
|
|
|
if (data.budget_max !== undefined) updatePayload.budget_max = data.budget_max;
|
|
|
|
|
|
if (data.budget_currency !== undefined) updatePayload.budget_currency = data.budget_currency;
|
|
|
|
|
|
if (data.estimated_hours !== undefined) updatePayload.estimated_hours = data.estimated_hours;
|
|
|
|
|
|
if (data.location_preference !== undefined) updatePayload.location_preference = data.location_preference;
|
|
|
|
|
|
if (data.remote_ok !== undefined) updatePayload.remote_ok = data.remote_ok;
|
|
|
|
|
|
if (data.desired_start_at !== undefined) updatePayload.desired_start_at = data.desired_start_at;
|
|
|
|
|
|
if (data.desired_end_at !== undefined) updatePayload.desired_end_at = data.desired_end_at;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await projects.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.company !== undefined) {
|
|
await projects.setCompany(
|
|
|
|
data.company,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.owner_user !== undefined) {
|
|
await projects.setOwner_user(
|
|
|
|
data.owner_user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.erp_system !== undefined) {
|
|
await projects.setErp_system(
|
|
|
|
data.erp_system,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await projects.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.projects.getTableName(),
|
|
belongsToColumn: 'attachment_files',
|
|
belongsToId: projects.id,
|
|
},
|
|
data.attachment_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return projects;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const projects = await db.projects.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of projects) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of projects) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return projects;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const projects = await db.projects.findByPk(id, options);
|
|
|
|
await projects.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await projects.destroy({
|
|
transaction
|
|
});
|
|
|
|
return projects;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const projects = await db.projects.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!projects) {
|
|
return projects;
|
|
}
|
|
|
|
const output = projects.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.project_skill_requirements_project = await projects.getProject_skill_requirements_project({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.proposals_project = await projects.getProposals_project({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.contracts_project = await projects.getContracts_project({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.conversations_project = await projects.getConversations_project({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.company = await projects.getCompany({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.owner_user = await projects.getOwner_user({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.erp_system = await projects.getErp_system({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.attachment_files = await projects.getAttachment_files({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await projects.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.companies,
|
|
as: 'company',
|
|
|
|
where: filter.company ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.company.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
company_name: {
|
|
[Op.or]: filter.company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'owner_user',
|
|
|
|
where: filter.owner_user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.owner_user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.owner_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.erp_systems,
|
|
as: 'erp_system',
|
|
|
|
where: filter.erp_system ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.erp_system.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
system_name: {
|
|
[Op.or]: filter.erp_system.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'attachment_files',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.project_title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'projects',
|
|
'project_title',
|
|
filter.project_title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.project_description) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'projects',
|
|
'project_description',
|
|
filter.project_description,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.budget_currency) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'projects',
|
|
'budget_currency',
|
|
filter.budget_currency,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.location_preference) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'projects',
|
|
'location_preference',
|
|
filter.location_preference,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.budget_minRange) {
|
|
const [start, end] = filter.budget_minRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
budget_min: {
|
|
...where.budget_min,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
budget_min: {
|
|
...where.budget_min,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.budget_maxRange) {
|
|
const [start, end] = filter.budget_maxRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
budget_max: {
|
|
...where.budget_max,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
budget_max: {
|
|
...where.budget_max,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.estimated_hoursRange) {
|
|
const [start, end] = filter.estimated_hoursRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_hours: {
|
|
...where.estimated_hours,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_hours: {
|
|
...where.estimated_hours,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.desired_start_atRange) {
|
|
const [start, end] = filter.desired_start_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
desired_start_at: {
|
|
...where.desired_start_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
desired_start_at: {
|
|
...where.desired_start_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.desired_end_atRange) {
|
|
const [start, end] = filter.desired_end_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
desired_end_at: {
|
|
...where.desired_end_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
desired_end_at: {
|
|
...where.desired_end_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.project_type) {
|
|
where = {
|
|
...where,
|
|
project_type: filter.project_type,
|
|
};
|
|
}
|
|
|
|
if (filter.engagement_model) {
|
|
where = {
|
|
...where,
|
|
engagement_model: filter.engagement_model,
|
|
};
|
|
}
|
|
|
|
if (filter.remote_ok) {
|
|
where = {
|
|
...where,
|
|
remote_ok: filter.remote_ok,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.projects.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(
|
|
'projects',
|
|
'project_title',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.projects.findAll({
|
|
attributes: [ 'id', 'project_title' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['project_title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.project_title,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|