39159-vm/backend/src/db/api/issue_updates.js
2026-03-12 16:46:27 +00:00

575 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 Issue_updatesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const issue_updates = await db.issue_updates.create(
{
id: data.id || undefined,
update_type: data.update_type
||
null
,
new_status: data.new_status
||
null
,
message: data.message
||
null
,
updated_at_time: data.updated_at_time
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await issue_updates.setIssue_report( data.issue_report || null, {
transaction,
});
await issue_updates.setUpdated_by( data.updated_by || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.issue_updates.getTableName(),
belongsToColumn: 'photos',
belongsToId: issue_updates.id,
},
data.photos,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.issue_updates.getTableName(),
belongsToColumn: 'files',
belongsToId: issue_updates.id,
},
data.files,
options,
);
return issue_updates;
}
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_updatesData = data.map((item, index) => ({
id: item.id || undefined,
update_type: item.update_type
||
null
,
new_status: item.new_status
||
null
,
message: item.message
||
null
,
updated_at_time: item.updated_at_time
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const issue_updates = await db.issue_updates.bulkCreate(issue_updatesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < issue_updates.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.issue_updates.getTableName(),
belongsToColumn: 'photos',
belongsToId: issue_updates[i].id,
},
data[i].photos,
options,
);
}
for (let i = 0; i < issue_updates.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.issue_updates.getTableName(),
belongsToColumn: 'files',
belongsToId: issue_updates[i].id,
},
data[i].files,
options,
);
}
return issue_updates;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const issue_updates = await db.issue_updates.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.update_type !== undefined) updatePayload.update_type = data.update_type;
if (data.new_status !== undefined) updatePayload.new_status = data.new_status;
if (data.message !== undefined) updatePayload.message = data.message;
if (data.updated_at_time !== undefined) updatePayload.updated_at_time = data.updated_at_time;
updatePayload.updatedById = currentUser.id;
await issue_updates.update(updatePayload, {transaction});
if (data.issue_report !== undefined) {
await issue_updates.setIssue_report(
data.issue_report,
{ transaction }
);
}
if (data.updated_by !== undefined) {
await issue_updates.setUpdated_by(
data.updated_by,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.issue_updates.getTableName(),
belongsToColumn: 'photos',
belongsToId: issue_updates.id,
},
data.photos,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.issue_updates.getTableName(),
belongsToColumn: 'files',
belongsToId: issue_updates.id,
},
data.files,
options,
);
return issue_updates;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const issue_updates = await db.issue_updates.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of issue_updates) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of issue_updates) {
await record.destroy({transaction});
}
});
return issue_updates;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const issue_updates = await db.issue_updates.findByPk(id, options);
await issue_updates.update({
deletedBy: currentUser.id
}, {
transaction,
});
await issue_updates.destroy({
transaction
});
return issue_updates;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const issue_updates = await db.issue_updates.findOne(
{ where },
{ transaction },
);
if (!issue_updates) {
return issue_updates;
}
const output = issue_updates.get({plain: true});
output.issue_report = await issue_updates.getIssue_report({
transaction
});
output.updated_by = await issue_updates.getUpdated_by({
transaction
});
output.photos = await issue_updates.getPhotos({
transaction
});
output.files = await issue_updates.getFiles({
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.issue_reports,
as: 'issue_report',
where: filter.issue_report ? {
[Op.or]: [
{ id: { [Op.in]: filter.issue_report.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.issue_report.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'updated_by',
where: filter.updated_by ? {
[Op.or]: [
{ id: { [Op.in]: filter.updated_by.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.updated_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'photos',
},
{
model: db.file,
as: 'files',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.message) {
where = {
...where,
[Op.and]: Utils.ilike(
'issue_updates',
'message',
filter.message,
),
};
}
if (filter.updated_at_timeRange) {
const [start, end] = filter.updated_at_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
updated_at_time: {
...where.updated_at_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
updated_at_time: {
...where.updated_at_time,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.update_type) {
where = {
...where,
update_type: filter.update_type,
};
}
if (filter.new_status) {
where = {
...where,
new_status: filter.new_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.issue_updates.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_updates',
'update_type',
query,
),
],
};
}
const records = await db.issue_updates.findAll({
attributes: [ 'id', 'update_type' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['update_type', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.update_type,
}));
}
};