40109-vm/backend/src/db/api/workflow_runs.js
2026-05-27 09:25:22 +00:00

709 lines
17 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 Workflow_runsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const workflow_runs = await db.workflow_runs.create(
{
id: data.id || undefined,
review_status: data.review_status
||
null
,
output_disposition: data.output_disposition
||
null
,
issues_found: data.issues_found
||
null
,
actual_hours_saved: data.actual_hours_saved
||
null
,
run_cost: data.run_cost
||
null
,
ran_at: data.ran_at
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await workflow_runs.setUse_case( data.use_case || null, {
transaction,
});
await workflow_runs.setUser( data.user || null, {
transaction,
});
await workflow_runs.setTool( data.tool || null, {
transaction,
});
await workflow_runs.setPractice_group( data.practice_group || null, {
transaction,
});
await workflow_runs.setMatter_type( data.matter_type || null, {
transaction,
});
return workflow_runs;
}
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 workflow_runsData = data.map((item, index) => ({
id: item.id || undefined,
review_status: item.review_status
||
null
,
output_disposition: item.output_disposition
||
null
,
issues_found: item.issues_found
||
null
,
actual_hours_saved: item.actual_hours_saved
||
null
,
run_cost: item.run_cost
||
null
,
ran_at: item.ran_at
||
null
,
notes: item.notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const workflow_runs = await db.workflow_runs.bulkCreate(workflow_runsData, { transaction });
// For each item created, replace relation files
return workflow_runs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const workflow_runs = await db.workflow_runs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.review_status !== undefined) updatePayload.review_status = data.review_status;
if (data.output_disposition !== undefined) updatePayload.output_disposition = data.output_disposition;
if (data.issues_found !== undefined) updatePayload.issues_found = data.issues_found;
if (data.actual_hours_saved !== undefined) updatePayload.actual_hours_saved = data.actual_hours_saved;
if (data.run_cost !== undefined) updatePayload.run_cost = data.run_cost;
if (data.ran_at !== undefined) updatePayload.ran_at = data.ran_at;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await workflow_runs.update(updatePayload, {transaction});
if (data.use_case !== undefined) {
await workflow_runs.setUse_case(
data.use_case,
{ transaction }
);
}
if (data.user !== undefined) {
await workflow_runs.setUser(
data.user,
{ transaction }
);
}
if (data.tool !== undefined) {
await workflow_runs.setTool(
data.tool,
{ transaction }
);
}
if (data.practice_group !== undefined) {
await workflow_runs.setPractice_group(
data.practice_group,
{ transaction }
);
}
if (data.matter_type !== undefined) {
await workflow_runs.setMatter_type(
data.matter_type,
{ transaction }
);
}
return workflow_runs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const workflow_runs = await db.workflow_runs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of workflow_runs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of workflow_runs) {
await record.destroy({transaction});
}
});
return workflow_runs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const workflow_runs = await db.workflow_runs.findByPk(id, options);
await workflow_runs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await workflow_runs.destroy({
transaction
});
return workflow_runs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const workflow_runs = await db.workflow_runs.findOne(
{ where },
{ transaction },
);
if (!workflow_runs) {
return workflow_runs;
}
const output = workflow_runs.get({plain: true});
output.review_exceptions_workflow_run = await workflow_runs.getReview_exceptions_workflow_run({
transaction
});
output.use_case = await workflow_runs.getUse_case({
transaction
});
output.user = await workflow_runs.getUser({
transaction
});
output.tool = await workflow_runs.getTool({
transaction
});
output.practice_group = await workflow_runs.getPractice_group({
transaction
});
output.matter_type = await workflow_runs.getMatter_type({
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_use_cases,
as: 'use_case',
where: filter.use_case ? {
[Op.or]: [
{ id: { [Op.in]: filter.use_case.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.use_case.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.ai_tools,
as: 'tool',
where: filter.tool ? {
[Op.or]: [
{ id: { [Op.in]: filter.tool.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.tool.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.practice_groups,
as: 'practice_group',
where: filter.practice_group ? {
[Op.or]: [
{ id: { [Op.in]: filter.practice_group.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.practice_group.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.matter_types,
as: 'matter_type',
where: filter.matter_type ? {
[Op.or]: [
{ id: { [Op.in]: filter.matter_type.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.matter_type.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'workflow_runs',
'notes',
filter.notes,
),
};
}
if (filter.actual_hours_savedRange) {
const [start, end] = filter.actual_hours_savedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
actual_hours_saved: {
...where.actual_hours_saved,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
actual_hours_saved: {
...where.actual_hours_saved,
[Op.lte]: end,
},
};
}
}
if (filter.run_costRange) {
const [start, end] = filter.run_costRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
run_cost: {
...where.run_cost,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
run_cost: {
...where.run_cost,
[Op.lte]: end,
},
};
}
}
if (filter.ran_atRange) {
const [start, end] = filter.ran_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ran_at: {
...where.ran_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ran_at: {
...where.ran_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.review_status) {
where = {
...where,
review_status: filter.review_status,
};
}
if (filter.output_disposition) {
where = {
...where,
output_disposition: filter.output_disposition,
};
}
if (filter.issues_found) {
where = {
...where,
issues_found: filter.issues_found,
};
}
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.workflow_runs.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(
'workflow_runs',
'notes',
query,
),
],
};
}
const records = await db.workflow_runs.findAll({
attributes: [ 'id', 'notes' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['notes', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.notes,
}));
}
};