824 lines
22 KiB
JavaScript
824 lines
22 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 Roi_metricsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const roi_metrics = await db.roi_metrics.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
metric_scope: data.metric_scope
|
|
||
|
|
null
|
|
,
|
|
|
|
practice_group: data.practice_group
|
|
||
|
|
null
|
|
,
|
|
|
|
period_start_at: data.period_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
period_end_at: data.period_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
hours_saved: data.hours_saved
|
|
||
|
|
null
|
|
,
|
|
|
|
turnaround_hours_reduced: data.turnaround_hours_reduced
|
|
||
|
|
null
|
|
,
|
|
|
|
write_offs_avoided_amount: data.write_offs_avoided_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
revenue_impact_amount: data.revenue_impact_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
subscription_cost_amount: data.subscription_cost_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
adoption_rate_percent: data.adoption_rate_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
run_count: data.run_count
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await roi_metrics.setTool( data.tool || null, {
|
|
transaction,
|
|
});
|
|
|
|
await roi_metrics.setWorkflow( data.workflow || null, {
|
|
transaction,
|
|
});
|
|
|
|
await roi_metrics.setUse_case_request( data.use_case_request || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return roi_metrics;
|
|
}
|
|
|
|
|
|
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 roi_metricsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
metric_scope: item.metric_scope
|
|
||
|
|
null
|
|
,
|
|
|
|
practice_group: item.practice_group
|
|
||
|
|
null
|
|
,
|
|
|
|
period_start_at: item.period_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
period_end_at: item.period_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
hours_saved: item.hours_saved
|
|
||
|
|
null
|
|
,
|
|
|
|
turnaround_hours_reduced: item.turnaround_hours_reduced
|
|
||
|
|
null
|
|
,
|
|
|
|
write_offs_avoided_amount: item.write_offs_avoided_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
revenue_impact_amount: item.revenue_impact_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
subscription_cost_amount: item.subscription_cost_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
adoption_rate_percent: item.adoption_rate_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
run_count: item.run_count
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const roi_metrics = await db.roi_metrics.bulkCreate(roi_metricsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return roi_metrics;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const roi_metrics = await db.roi_metrics.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.metric_scope !== undefined) updatePayload.metric_scope = data.metric_scope;
|
|
|
|
|
|
if (data.practice_group !== undefined) updatePayload.practice_group = data.practice_group;
|
|
|
|
|
|
if (data.period_start_at !== undefined) updatePayload.period_start_at = data.period_start_at;
|
|
|
|
|
|
if (data.period_end_at !== undefined) updatePayload.period_end_at = data.period_end_at;
|
|
|
|
|
|
if (data.hours_saved !== undefined) updatePayload.hours_saved = data.hours_saved;
|
|
|
|
|
|
if (data.turnaround_hours_reduced !== undefined) updatePayload.turnaround_hours_reduced = data.turnaround_hours_reduced;
|
|
|
|
|
|
if (data.write_offs_avoided_amount !== undefined) updatePayload.write_offs_avoided_amount = data.write_offs_avoided_amount;
|
|
|
|
|
|
if (data.revenue_impact_amount !== undefined) updatePayload.revenue_impact_amount = data.revenue_impact_amount;
|
|
|
|
|
|
if (data.subscription_cost_amount !== undefined) updatePayload.subscription_cost_amount = data.subscription_cost_amount;
|
|
|
|
|
|
if (data.adoption_rate_percent !== undefined) updatePayload.adoption_rate_percent = data.adoption_rate_percent;
|
|
|
|
|
|
if (data.run_count !== undefined) updatePayload.run_count = data.run_count;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await roi_metrics.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.tool !== undefined) {
|
|
await roi_metrics.setTool(
|
|
|
|
data.tool,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.workflow !== undefined) {
|
|
await roi_metrics.setWorkflow(
|
|
|
|
data.workflow,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.use_case_request !== undefined) {
|
|
await roi_metrics.setUse_case_request(
|
|
|
|
data.use_case_request,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return roi_metrics;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const roi_metrics = await db.roi_metrics.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of roi_metrics) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of roi_metrics) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return roi_metrics;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const roi_metrics = await db.roi_metrics.findByPk(id, options);
|
|
|
|
await roi_metrics.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await roi_metrics.destroy({
|
|
transaction
|
|
});
|
|
|
|
return roi_metrics;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const roi_metrics = await db.roi_metrics.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!roi_metrics) {
|
|
return roi_metrics;
|
|
}
|
|
|
|
const output = roi_metrics.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.tool = await roi_metrics.getTool({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.workflow = await roi_metrics.getWorkflow({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.use_case_request = await roi_metrics.getUse_case_request({
|
|
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.ai_tools,
|
|
as: 'tool',
|
|
|
|
where: filter.tool ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.tool.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
tool_name: {
|
|
[Op.or]: filter.tool.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.ai_workflows,
|
|
as: 'workflow',
|
|
|
|
where: filter.workflow ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.workflow.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
workflow_name: {
|
|
[Op.or]: filter.workflow.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.ai_use_case_requests,
|
|
as: 'use_case_request',
|
|
|
|
where: filter.use_case_request ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.use_case_request.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
request_title: {
|
|
[Op.or]: filter.use_case_request.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
period_start_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
period_end_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.period_start_atRange) {
|
|
const [start, end] = filter.period_start_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
period_start_at: {
|
|
...where.period_start_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
period_start_at: {
|
|
...where.period_start_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.period_end_atRange) {
|
|
const [start, end] = filter.period_end_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
period_end_at: {
|
|
...where.period_end_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
period_end_at: {
|
|
...where.period_end_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.hours_savedRange) {
|
|
const [start, end] = filter.hours_savedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
hours_saved: {
|
|
...where.hours_saved,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
hours_saved: {
|
|
...where.hours_saved,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.turnaround_hours_reducedRange) {
|
|
const [start, end] = filter.turnaround_hours_reducedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
turnaround_hours_reduced: {
|
|
...where.turnaround_hours_reduced,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
turnaround_hours_reduced: {
|
|
...where.turnaround_hours_reduced,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.write_offs_avoided_amountRange) {
|
|
const [start, end] = filter.write_offs_avoided_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
write_offs_avoided_amount: {
|
|
...where.write_offs_avoided_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
write_offs_avoided_amount: {
|
|
...where.write_offs_avoided_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.revenue_impact_amountRange) {
|
|
const [start, end] = filter.revenue_impact_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
revenue_impact_amount: {
|
|
...where.revenue_impact_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
revenue_impact_amount: {
|
|
...where.revenue_impact_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.subscription_cost_amountRange) {
|
|
const [start, end] = filter.subscription_cost_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
subscription_cost_amount: {
|
|
...where.subscription_cost_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
subscription_cost_amount: {
|
|
...where.subscription_cost_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.adoption_rate_percentRange) {
|
|
const [start, end] = filter.adoption_rate_percentRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
adoption_rate_percent: {
|
|
...where.adoption_rate_percent,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
adoption_rate_percent: {
|
|
...where.adoption_rate_percent,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.run_countRange) {
|
|
const [start, end] = filter.run_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
run_count: {
|
|
...where.run_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
run_count: {
|
|
...where.run_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.metric_scope) {
|
|
where = {
|
|
...where,
|
|
metric_scope: filter.metric_scope,
|
|
};
|
|
}
|
|
|
|
if (filter.practice_group) {
|
|
where = {
|
|
...where,
|
|
practice_group: filter.practice_group,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.roi_metrics.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(
|
|
'roi_metrics',
|
|
'metric_scope',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.roi_metrics.findAll({
|
|
attributes: [ 'id', 'metric_scope' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['metric_scope', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.metric_scope,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|