38358-vm/backend/src/db/api/ticket_worklogs.js
2026-02-11 11:44:25 +00:00

547 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 Ticket_worklogsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ticket_worklogs = await db.ticket_worklogs.create(
{
id: data.id || undefined,
work_started_at: data.work_started_at
||
null
,
work_ended_at: data.work_ended_at
||
null
,
hours: data.hours
||
null
,
comment: data.comment
||
null
,
external_vtenext_worklog_key: data.external_vtenext_worklog_key
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await ticket_worklogs.setTicket( data.ticket || null, {
transaction,
});
await ticket_worklogs.setUser( data.user || null, {
transaction,
});
return ticket_worklogs;
}
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 ticket_worklogsData = data.map((item, index) => ({
id: item.id || undefined,
work_started_at: item.work_started_at
||
null
,
work_ended_at: item.work_ended_at
||
null
,
hours: item.hours
||
null
,
comment: item.comment
||
null
,
external_vtenext_worklog_key: item.external_vtenext_worklog_key
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const ticket_worklogs = await db.ticket_worklogs.bulkCreate(ticket_worklogsData, { transaction });
// For each item created, replace relation files
return ticket_worklogs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ticket_worklogs = await db.ticket_worklogs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.work_started_at !== undefined) updatePayload.work_started_at = data.work_started_at;
if (data.work_ended_at !== undefined) updatePayload.work_ended_at = data.work_ended_at;
if (data.hours !== undefined) updatePayload.hours = data.hours;
if (data.comment !== undefined) updatePayload.comment = data.comment;
if (data.external_vtenext_worklog_key !== undefined) updatePayload.external_vtenext_worklog_key = data.external_vtenext_worklog_key;
updatePayload.updatedById = currentUser.id;
await ticket_worklogs.update(updatePayload, {transaction});
if (data.ticket !== undefined) {
await ticket_worklogs.setTicket(
data.ticket,
{ transaction }
);
}
if (data.user !== undefined) {
await ticket_worklogs.setUser(
data.user,
{ transaction }
);
}
return ticket_worklogs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ticket_worklogs = await db.ticket_worklogs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of ticket_worklogs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of ticket_worklogs) {
await record.destroy({transaction});
}
});
return ticket_worklogs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ticket_worklogs = await db.ticket_worklogs.findByPk(id, options);
await ticket_worklogs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await ticket_worklogs.destroy({
transaction
});
return ticket_worklogs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const ticket_worklogs = await db.ticket_worklogs.findOne(
{ where },
{ transaction },
);
if (!ticket_worklogs) {
return ticket_worklogs;
}
const output = ticket_worklogs.get({plain: true});
output.ticket = await ticket_worklogs.getTicket({
transaction
});
output.user = await ticket_worklogs.getUser({
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.tickets,
as: 'ticket',
where: filter.ticket ? {
[Op.or]: [
{ id: { [Op.in]: filter.ticket.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.ticket.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}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.comment) {
where = {
...where,
[Op.and]: Utils.ilike(
'ticket_worklogs',
'comment',
filter.comment,
),
};
}
if (filter.external_vtenext_worklog_key) {
where = {
...where,
[Op.and]: Utils.ilike(
'ticket_worklogs',
'external_vtenext_worklog_key',
filter.external_vtenext_worklog_key,
),
};
}
if (filter.work_started_atRange) {
const [start, end] = filter.work_started_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
work_started_at: {
...where.work_started_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
work_started_at: {
...where.work_started_at,
[Op.lte]: end,
},
};
}
}
if (filter.work_ended_atRange) {
const [start, end] = filter.work_ended_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
work_ended_at: {
...where.work_ended_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
work_ended_at: {
...where.work_ended_at,
[Op.lte]: end,
},
};
}
}
if (filter.hoursRange) {
const [start, end] = filter.hoursRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
hours: {
...where.hours,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
hours: {
...where.hours,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
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.ticket_worklogs.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(
'ticket_worklogs',
'comment',
query,
),
],
};
}
const records = await db.ticket_worklogs.findAll({
attributes: [ 'id', 'comment' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['comment', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.comment,
}));
}
};