39251-vm/backend/src/db/api/improvement_actions.js
2026-03-20 15:31:29 +00:00

582 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 Improvement_actionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const improvement_actions = await db.improvement_actions.create(
{
id: data.id || undefined,
action: data.action
||
null
,
instruction: data.instruction
||
null
,
before_content: data.before_content
||
null
,
after_content: data.after_content
||
null
,
status: data.status
||
null
,
requested_at: data.requested_at
||
null
,
completed_at: data.completed_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await improvement_actions.setUser( data.user || null, {
transaction,
});
await improvement_actions.setDocument( data.document || null, {
transaction,
});
return improvement_actions;
}
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 improvement_actionsData = data.map((item, index) => ({
id: item.id || undefined,
action: item.action
||
null
,
instruction: item.instruction
||
null
,
before_content: item.before_content
||
null
,
after_content: item.after_content
||
null
,
status: item.status
||
null
,
requested_at: item.requested_at
||
null
,
completed_at: item.completed_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const improvement_actions = await db.improvement_actions.bulkCreate(improvement_actionsData, { transaction });
// For each item created, replace relation files
return improvement_actions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const improvement_actions = await db.improvement_actions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.action !== undefined) updatePayload.action = data.action;
if (data.instruction !== undefined) updatePayload.instruction = data.instruction;
if (data.before_content !== undefined) updatePayload.before_content = data.before_content;
if (data.after_content !== undefined) updatePayload.after_content = data.after_content;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at;
updatePayload.updatedById = currentUser.id;
await improvement_actions.update(updatePayload, {transaction});
if (data.user !== undefined) {
await improvement_actions.setUser(
data.user,
{ transaction }
);
}
if (data.document !== undefined) {
await improvement_actions.setDocument(
data.document,
{ transaction }
);
}
return improvement_actions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const improvement_actions = await db.improvement_actions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of improvement_actions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of improvement_actions) {
await record.destroy({transaction});
}
});
return improvement_actions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const improvement_actions = await db.improvement_actions.findByPk(id, options);
await improvement_actions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await improvement_actions.destroy({
transaction
});
return improvement_actions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const improvement_actions = await db.improvement_actions.findOne(
{ where },
{ transaction },
);
if (!improvement_actions) {
return improvement_actions;
}
const output = improvement_actions.get({plain: true});
output.user = await improvement_actions.getUser({
transaction
});
output.document = await improvement_actions.getDocument({
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.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.documents,
as: 'document',
where: filter.document ? {
[Op.or]: [
{ id: { [Op.in]: filter.document.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.document.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.instruction) {
where = {
...where,
[Op.and]: Utils.ilike(
'improvement_actions',
'instruction',
filter.instruction,
),
};
}
if (filter.before_content) {
where = {
...where,
[Op.and]: Utils.ilike(
'improvement_actions',
'before_content',
filter.before_content,
),
};
}
if (filter.after_content) {
where = {
...where,
[Op.and]: Utils.ilike(
'improvement_actions',
'after_content',
filter.after_content,
),
};
}
if (filter.requested_atRange) {
const [start, end] = filter.requested_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.lte]: end,
},
};
}
}
if (filter.completed_atRange) {
const [start, end] = filter.completed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.action) {
where = {
...where,
action: filter.action,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.improvement_actions.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(
'improvement_actions',
'instruction',
query,
),
],
};
}
const records = await db.improvement_actions.findAll({
attributes: [ 'id', 'instruction' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['instruction', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.instruction,
}));
}
};