38689-vm/backend/src/db/api/issue_taxonomy.js
2026-02-22 15:41:40 +00:00

436 lines
10 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 Issue_taxonomyDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const issue_taxonomy = await db.issue_taxonomy.create(
{
id: data.id || undefined,
issue_name: data.issue_name
||
null
,
issue_type: data.issue_type
||
null
,
description: data.description
||
null
,
severity_default: data.severity_default
||
null
,
active: data.active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return issue_taxonomy;
}
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 issue_taxonomyData = data.map((item, index) => ({
id: item.id || undefined,
issue_name: item.issue_name
||
null
,
issue_type: item.issue_type
||
null
,
description: item.description
||
null
,
severity_default: item.severity_default
||
null
,
active: item.active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const issue_taxonomy = await db.issue_taxonomy.bulkCreate(issue_taxonomyData, { transaction });
// For each item created, replace relation files
return issue_taxonomy;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const issue_taxonomy = await db.issue_taxonomy.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.issue_name !== undefined) updatePayload.issue_name = data.issue_name;
if (data.issue_type !== undefined) updatePayload.issue_type = data.issue_type;
if (data.description !== undefined) updatePayload.description = data.description;
if (data.severity_default !== undefined) updatePayload.severity_default = data.severity_default;
if (data.active !== undefined) updatePayload.active = data.active;
updatePayload.updatedById = currentUser.id;
await issue_taxonomy.update(updatePayload, {transaction});
return issue_taxonomy;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const issue_taxonomy = await db.issue_taxonomy.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of issue_taxonomy) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of issue_taxonomy) {
await record.destroy({transaction});
}
});
return issue_taxonomy;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const issue_taxonomy = await db.issue_taxonomy.findByPk(id, options);
await issue_taxonomy.update({
deletedBy: currentUser.id
}, {
transaction,
});
await issue_taxonomy.destroy({
transaction
});
return issue_taxonomy;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const issue_taxonomy = await db.issue_taxonomy.findOne(
{ where },
{ transaction },
);
if (!issue_taxonomy) {
return issue_taxonomy;
}
const output = issue_taxonomy.get({plain: true});
output.recurring_issue_signals_issue = await issue_taxonomy.getRecurring_issue_signals_issue({
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 = [
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.issue_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'issue_taxonomy',
'issue_name',
filter.issue_name,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'issue_taxonomy',
'description',
filter.description,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.issue_type) {
where = {
...where,
issue_type: filter.issue_type,
};
}
if (filter.severity_default) {
where = {
...where,
severity_default: filter.severity_default,
};
}
if (filter.active) {
where = {
...where,
active: filter.active,
};
}
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.issue_taxonomy.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(
'issue_taxonomy',
'issue_name',
query,
),
],
};
}
const records = await db.issue_taxonomy.findAll({
attributes: [ 'id', 'issue_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['issue_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.issue_name,
}));
}
};