948 lines
24 KiB
JavaScript
948 lines
24 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 Job_requestsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_requests = await db.job_requests.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
request_no: data.request_no
|
|
||
|
|
null
|
|
,
|
|
|
|
title: data.title
|
|
||
|
|
null
|
|
,
|
|
|
|
description: data.description
|
|
||
|
|
null
|
|
,
|
|
|
|
site_address: data.site_address
|
|
||
|
|
null
|
|
,
|
|
|
|
latitude: data.latitude
|
|
||
|
|
null
|
|
,
|
|
|
|
longitude: data.longitude
|
|
||
|
|
null
|
|
,
|
|
|
|
work_height_level: data.work_height_level
|
|
||
|
|
null
|
|
,
|
|
|
|
urgency: data.urgency
|
|
||
|
|
null
|
|
,
|
|
|
|
preferred_start_at: data.preferred_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
preferred_end_at: data.preferred_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_min: data.budget_min
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_max: data.budget_max
|
|
||
|
|
null
|
|
,
|
|
|
|
needs_invoice: data.needs_invoice
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await job_requests.setClient( data.client || null, {
|
|
transaction,
|
|
});
|
|
|
|
await job_requests.setCategory( data.category || null, {
|
|
transaction,
|
|
});
|
|
|
|
await job_requests.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.job_requests.getTableName(),
|
|
belongsToColumn: 'site_images',
|
|
belongsToId: job_requests.id,
|
|
},
|
|
data.site_images,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.job_requests.getTableName(),
|
|
belongsToColumn: 'requirements_files',
|
|
belongsToId: job_requests.id,
|
|
},
|
|
data.requirements_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return job_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 job_requestsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
request_no: item.request_no
|
|
||
|
|
null
|
|
,
|
|
|
|
title: item.title
|
|
||
|
|
null
|
|
,
|
|
|
|
description: item.description
|
|
||
|
|
null
|
|
,
|
|
|
|
site_address: item.site_address
|
|
||
|
|
null
|
|
,
|
|
|
|
latitude: item.latitude
|
|
||
|
|
null
|
|
,
|
|
|
|
longitude: item.longitude
|
|
||
|
|
null
|
|
,
|
|
|
|
work_height_level: item.work_height_level
|
|
||
|
|
null
|
|
,
|
|
|
|
urgency: item.urgency
|
|
||
|
|
null
|
|
,
|
|
|
|
preferred_start_at: item.preferred_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
preferred_end_at: item.preferred_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_min: item.budget_min
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_max: item.budget_max
|
|
||
|
|
null
|
|
,
|
|
|
|
needs_invoice: item.needs_invoice
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const job_requests = await db.job_requests.bulkCreate(job_requestsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < job_requests.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.job_requests.getTableName(),
|
|
belongsToColumn: 'site_images',
|
|
belongsToId: job_requests[i].id,
|
|
},
|
|
data[i].site_images,
|
|
options,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < job_requests.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.job_requests.getTableName(),
|
|
belongsToColumn: 'requirements_files',
|
|
belongsToId: job_requests[i].id,
|
|
},
|
|
data[i].requirements_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return job_requests;
|
|
}
|
|
|
|
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 job_requests = await db.job_requests.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.request_no !== undefined) updatePayload.request_no = data.request_no;
|
|
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
|
|
if (data.description !== undefined) updatePayload.description = data.description;
|
|
|
|
|
|
if (data.site_address !== undefined) updatePayload.site_address = data.site_address;
|
|
|
|
|
|
if (data.latitude !== undefined) updatePayload.latitude = data.latitude;
|
|
|
|
|
|
if (data.longitude !== undefined) updatePayload.longitude = data.longitude;
|
|
|
|
|
|
if (data.work_height_level !== undefined) updatePayload.work_height_level = data.work_height_level;
|
|
|
|
|
|
if (data.urgency !== undefined) updatePayload.urgency = data.urgency;
|
|
|
|
|
|
if (data.preferred_start_at !== undefined) updatePayload.preferred_start_at = data.preferred_start_at;
|
|
|
|
|
|
if (data.preferred_end_at !== undefined) updatePayload.preferred_end_at = data.preferred_end_at;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.budget_min !== undefined) updatePayload.budget_min = data.budget_min;
|
|
|
|
|
|
if (data.budget_max !== undefined) updatePayload.budget_max = data.budget_max;
|
|
|
|
|
|
if (data.needs_invoice !== undefined) updatePayload.needs_invoice = data.needs_invoice;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await job_requests.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.client !== undefined) {
|
|
await job_requests.setClient(
|
|
|
|
data.client,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.category !== undefined) {
|
|
await job_requests.setCategory(
|
|
|
|
data.category,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await job_requests.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.job_requests.getTableName(),
|
|
belongsToColumn: 'site_images',
|
|
belongsToId: job_requests.id,
|
|
},
|
|
data.site_images,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.job_requests.getTableName(),
|
|
belongsToColumn: 'requirements_files',
|
|
belongsToId: job_requests.id,
|
|
},
|
|
data.requirements_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return job_requests;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_requests = await db.job_requests.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of job_requests) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of job_requests) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return job_requests;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_requests = await db.job_requests.findByPk(id, options);
|
|
|
|
await job_requests.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await job_requests.destroy({
|
|
transaction
|
|
});
|
|
|
|
return job_requests;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_requests = await db.job_requests.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!job_requests) {
|
|
return job_requests;
|
|
}
|
|
|
|
const output = job_requests.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.job_quotes_job_request = await job_requests.getJob_quotes_job_request({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.assignments_job_request = await job_requests.getAssignments_job_request({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.safety_checklists_job_request = await job_requests.getSafety_checklists_job_request({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.messages_job_request = await job_requests.getMessages_job_request({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.client = await job_requests.getClient({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.category = await job_requests.getCategory({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.site_images = await job_requests.getSite_images({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.requirements_files = await job_requests.getRequirements_files({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await job_requests.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.users,
|
|
as: 'client',
|
|
|
|
where: filter.client ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.client.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.client.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.work_categories,
|
|
as: 'category',
|
|
|
|
where: filter.category ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.category.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.category.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'site_images',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'requirements_files',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.request_no) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'job_requests',
|
|
'request_no',
|
|
filter.request_no,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'job_requests',
|
|
'title',
|
|
filter.title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.description) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'job_requests',
|
|
'description',
|
|
filter.description,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.site_address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'job_requests',
|
|
'site_address',
|
|
filter.site_address,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.latitudeRange) {
|
|
const [start, end] = filter.latitudeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
latitude: {
|
|
...where.latitude,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
latitude: {
|
|
...where.latitude,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.longitudeRange) {
|
|
const [start, end] = filter.longitudeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
longitude: {
|
|
...where.longitude,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
longitude: {
|
|
...where.longitude,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.preferred_start_atRange) {
|
|
const [start, end] = filter.preferred_start_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
preferred_start_at: {
|
|
...where.preferred_start_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
preferred_start_at: {
|
|
...where.preferred_start_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.preferred_end_atRange) {
|
|
const [start, end] = filter.preferred_end_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
preferred_end_at: {
|
|
...where.preferred_end_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
preferred_end_at: {
|
|
...where.preferred_end_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
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.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.work_height_level) {
|
|
where = {
|
|
...where,
|
|
work_height_level: filter.work_height_level,
|
|
};
|
|
}
|
|
|
|
if (filter.urgency) {
|
|
where = {
|
|
...where,
|
|
urgency: filter.urgency,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
if (filter.needs_invoice) {
|
|
where = {
|
|
...where,
|
|
needs_invoice: filter.needs_invoice,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.job_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, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'job_requests',
|
|
'title',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.job_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,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|