465 lines
11 KiB
JavaScript
465 lines
11 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 ProposalsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proposals = await db.proposals.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
code: data.code || null,
|
|
client: data.client || null,
|
|
site: data.site || null,
|
|
status: data.status || null,
|
|
totals: data.totals || null,
|
|
steps: data.steps || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await proposals.setTemplate(data.template || null, {
|
|
transaction,
|
|
});
|
|
|
|
await proposals.setDepartments(data.departments || null, {
|
|
transaction,
|
|
});
|
|
|
|
await proposals.setDiscount_buckets(data.discount_buckets || [], {
|
|
transaction,
|
|
});
|
|
|
|
return proposals;
|
|
}
|
|
|
|
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 proposalsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
code: item.code || null,
|
|
client: item.client || null,
|
|
site: item.site || null,
|
|
status: item.status || null,
|
|
totals: item.totals || null,
|
|
steps: item.steps || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const proposals = await db.proposals.bulkCreate(proposalsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return proposals;
|
|
}
|
|
|
|
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 proposals = await db.proposals.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.code !== undefined) updatePayload.code = data.code;
|
|
|
|
if (data.client !== undefined) updatePayload.client = data.client;
|
|
|
|
if (data.site !== undefined) updatePayload.site = data.site;
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
if (data.totals !== undefined) updatePayload.totals = data.totals;
|
|
|
|
if (data.steps !== undefined) updatePayload.steps = data.steps;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await proposals.update(updatePayload, { transaction });
|
|
|
|
if (data.template !== undefined) {
|
|
await proposals.setTemplate(
|
|
data.template,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.departments !== undefined) {
|
|
await proposals.setDepartments(
|
|
data.departments,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.discount_buckets !== undefined) {
|
|
await proposals.setDiscount_buckets(data.discount_buckets, {
|
|
transaction,
|
|
});
|
|
}
|
|
|
|
return proposals;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proposals = await db.proposals.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of proposals) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of proposals) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return proposals;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proposals = await db.proposals.findByPk(id, options);
|
|
|
|
await proposals.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await proposals.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return proposals;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proposals = await db.proposals.findOne({ where }, { transaction });
|
|
|
|
if (!proposals) {
|
|
return proposals;
|
|
}
|
|
|
|
const output = proposals.get({ plain: true });
|
|
|
|
output.approval_requests_proposal =
|
|
await proposals.getApproval_requests_proposal({
|
|
transaction,
|
|
});
|
|
|
|
output.proposal_items_proposal = await proposals.getProposal_items_proposal(
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
output.template = await proposals.getTemplate({
|
|
transaction,
|
|
});
|
|
|
|
output.discount_buckets = await proposals.getDiscount_buckets({
|
|
transaction,
|
|
});
|
|
|
|
output.departments = await proposals.getDepartments({
|
|
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 userDepartments = (user && user.departments?.id) || null;
|
|
|
|
if (userDepartments) {
|
|
if (options?.currentUser?.departmentsId) {
|
|
where.departmentsId = options.currentUser.departmentsId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.templates,
|
|
as: 'template',
|
|
|
|
where: filter.template
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.template
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.template
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.departments,
|
|
as: 'departments',
|
|
},
|
|
|
|
{
|
|
model: db.discount_buckets,
|
|
as: 'discount_buckets',
|
|
required: false,
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('proposals', 'code', filter.code),
|
|
};
|
|
}
|
|
|
|
if (filter.client) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('proposals', 'client', filter.client),
|
|
};
|
|
}
|
|
|
|
if (filter.site) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('proposals', 'site', filter.site),
|
|
};
|
|
}
|
|
|
|
if (filter.totals) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('proposals', 'totals', filter.totals),
|
|
};
|
|
}
|
|
|
|
if (filter.steps) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('proposals', 'steps', filter.steps),
|
|
};
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
if (filter.departments) {
|
|
const listItems = filter.departments.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
departmentsId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
if (filter.discount_buckets) {
|
|
const searchTerms = filter.discount_buckets.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.discount_buckets,
|
|
as: 'discount_buckets_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
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.departmentsId;
|
|
}
|
|
|
|
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.proposals.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('proposals', 'code', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.proposals.findAll({
|
|
attributes: ['id', 'code'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['code', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.code,
|
|
}));
|
|
}
|
|
};
|