39950-vm/backend/src/db/api/approval_steps.js
2026-05-11 09:55:08 +00:00

574 lines
14 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 Approval_stepsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const approval_steps = await db.approval_steps.create(
{
id: data.id || undefined,
step_type: data.step_type
||
null
,
decision: data.decision
||
null
,
comments: data.comments
||
null
,
assigned_at: data.assigned_at
||
null
,
decided_at: data.decided_at
||
null
,
step_order: data.step_order
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await approval_steps.setUse_case( data.use_case || null, {
transaction,
});
await approval_steps.setAssigned_reviewer( data.assigned_reviewer || null, {
transaction,
});
return approval_steps;
}
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 approval_stepsData = data.map((item, index) => ({
id: item.id || undefined,
step_type: item.step_type
||
null
,
decision: item.decision
||
null
,
comments: item.comments
||
null
,
assigned_at: item.assigned_at
||
null
,
decided_at: item.decided_at
||
null
,
step_order: item.step_order
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const approval_steps = await db.approval_steps.bulkCreate(approval_stepsData, { transaction });
// For each item created, replace relation files
return approval_steps;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const approval_steps = await db.approval_steps.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.step_type !== undefined) updatePayload.step_type = data.step_type;
if (data.decision !== undefined) updatePayload.decision = data.decision;
if (data.comments !== undefined) updatePayload.comments = data.comments;
if (data.assigned_at !== undefined) updatePayload.assigned_at = data.assigned_at;
if (data.decided_at !== undefined) updatePayload.decided_at = data.decided_at;
if (data.step_order !== undefined) updatePayload.step_order = data.step_order;
updatePayload.updatedById = currentUser.id;
await approval_steps.update(updatePayload, {transaction});
if (data.use_case !== undefined) {
await approval_steps.setUse_case(
data.use_case,
{ transaction }
);
}
if (data.assigned_reviewer !== undefined) {
await approval_steps.setAssigned_reviewer(
data.assigned_reviewer,
{ transaction }
);
}
return approval_steps;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const approval_steps = await db.approval_steps.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of approval_steps) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of approval_steps) {
await record.destroy({transaction});
}
});
return approval_steps;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const approval_steps = await db.approval_steps.findByPk(id, options);
await approval_steps.update({
deletedBy: currentUser.id
}, {
transaction,
});
await approval_steps.destroy({
transaction
});
return approval_steps;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const approval_steps = await db.approval_steps.findOne(
{ where },
{ transaction },
);
if (!approval_steps) {
return approval_steps;
}
const output = approval_steps.get({plain: true});
output.use_case = await approval_steps.getUse_case({
transaction
});
output.assigned_reviewer = await approval_steps.getAssigned_reviewer({
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: 'assigned_reviewer',
where: filter.assigned_reviewer ? {
[Op.or]: [
{ id: { [Op.in]: filter.assigned_reviewer.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.assigned_reviewer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.comments) {
where = {
...where,
[Op.and]: Utils.ilike(
'approval_steps',
'comments',
filter.comments,
),
};
}
if (filter.assigned_atRange) {
const [start, end] = filter.assigned_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
assigned_at: {
...where.assigned_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
assigned_at: {
...where.assigned_at,
[Op.lte]: end,
},
};
}
}
if (filter.decided_atRange) {
const [start, end] = filter.decided_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
decided_at: {
...where.decided_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
decided_at: {
...where.decided_at,
[Op.lte]: end,
},
};
}
}
if (filter.step_orderRange) {
const [start, end] = filter.step_orderRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
step_order: {
...where.step_order,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
step_order: {
...where.step_order,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.step_type) {
where = {
...where,
step_type: filter.step_type,
};
}
if (filter.decision) {
where = {
...where,
decision: filter.decision,
};
}
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.approval_steps.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(
'approval_steps',
'comments',
query,
),
],
};
}
const records = await db.approval_steps.findAll({
attributes: [ 'id', 'comments' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['comments', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.comments,
}));
}
};