39017-vm/backend/src/db/api/tickets.js
2026-03-05 20:07:34 +00:00

601 lines
15 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 TicketsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tickets = await db.tickets.create(
{
id: data.id || undefined,
zammad_numeric_key: data.zammad_numeric_key
||
null
,
number_text: data.number_text
||
null
,
title_text: data.title_text
||
null
,
priority_name: data.priority_name
||
null
,
owner_name: data.owner_name
||
null
,
customer_name: data.customer_name
||
null
,
kanban_value: data.kanban_value
||
null
,
fetched_at: data.fetched_at
||
null
,
updated_at_remote: data.updated_at_remote
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await tickets.setGroup( data.group || null, {
transaction,
});
return tickets;
}
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 ticketsData = data.map((item, index) => ({
id: item.id || undefined,
zammad_numeric_key: item.zammad_numeric_key
||
null
,
number_text: item.number_text
||
null
,
title_text: item.title_text
||
null
,
priority_name: item.priority_name
||
null
,
owner_name: item.owner_name
||
null
,
customer_name: item.customer_name
||
null
,
kanban_value: item.kanban_value
||
null
,
fetched_at: item.fetched_at
||
null
,
updated_at_remote: item.updated_at_remote
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const tickets = await db.tickets.bulkCreate(ticketsData, { transaction });
// For each item created, replace relation files
return tickets;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tickets = await db.tickets.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.zammad_numeric_key !== undefined) updatePayload.zammad_numeric_key = data.zammad_numeric_key;
if (data.number_text !== undefined) updatePayload.number_text = data.number_text;
if (data.title_text !== undefined) updatePayload.title_text = data.title_text;
if (data.priority_name !== undefined) updatePayload.priority_name = data.priority_name;
if (data.owner_name !== undefined) updatePayload.owner_name = data.owner_name;
if (data.customer_name !== undefined) updatePayload.customer_name = data.customer_name;
if (data.kanban_value !== undefined) updatePayload.kanban_value = data.kanban_value;
if (data.fetched_at !== undefined) updatePayload.fetched_at = data.fetched_at;
if (data.updated_at_remote !== undefined) updatePayload.updated_at_remote = data.updated_at_remote;
updatePayload.updatedById = currentUser.id;
await tickets.update(updatePayload, {transaction});
if (data.group !== undefined) {
await tickets.setGroup(
data.group,
{ transaction }
);
}
return tickets;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tickets = await db.tickets.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of tickets) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of tickets) {
await record.destroy({transaction});
}
});
return tickets;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tickets = await db.tickets.findByPk(id, options);
await tickets.update({
deletedBy: currentUser.id
}, {
transaction,
});
await tickets.destroy({
transaction
});
return tickets;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const tickets = await db.tickets.findOne(
{ where },
{ transaction },
);
if (!tickets) {
return tickets;
}
const output = tickets.get({plain: true});
output.group = await tickets.getGroup({
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.zammad_groups,
as: 'group',
where: filter.group ? {
[Op.or]: [
{ id: { [Op.in]: filter.group.split('|').map(term => Utils.uuid(term)) } },
{
group_name: {
[Op.or]: filter.group.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.number_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'tickets',
'number_text',
filter.number_text,
),
};
}
if (filter.title_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'tickets',
'title_text',
filter.title_text,
),
};
}
if (filter.owner_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'tickets',
'owner_name',
filter.owner_name,
),
};
}
if (filter.customer_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'tickets',
'customer_name',
filter.customer_name,
),
};
}
if (filter.kanban_value) {
where = {
...where,
[Op.and]: Utils.ilike(
'tickets',
'kanban_value',
filter.kanban_value,
),
};
}
if (filter.zammad_numeric_keyRange) {
const [start, end] = filter.zammad_numeric_keyRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
zammad_numeric_key: {
...where.zammad_numeric_key,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
zammad_numeric_key: {
...where.zammad_numeric_key,
[Op.lte]: end,
},
};
}
}
if (filter.fetched_atRange) {
const [start, end] = filter.fetched_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
fetched_at: {
...where.fetched_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
fetched_at: {
...where.fetched_at,
[Op.lte]: end,
},
};
}
}
if (filter.updated_at_remoteRange) {
const [start, end] = filter.updated_at_remoteRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
updated_at_remote: {
...where.updated_at_remote,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
updated_at_remote: {
...where.updated_at_remote,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.priority_name) {
where = {
...where,
priority_name: filter.priority_name,
};
}
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.tickets.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(
'tickets',
'title_text',
query,
),
],
};
}
const records = await db.tickets.findAll({
attributes: [ 'id', 'title_text' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title_text', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title_text,
}));
}
};