417 lines
10 KiB
JavaScript
417 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 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,
|
|
|
|
customer_name: data.customer_name || null,
|
|
address: data.address || null,
|
|
issue: data.issue || null,
|
|
handled: data.handled || null,
|
|
start: data.start || null,
|
|
end: data.end || null,
|
|
replaced_tools: data.replaced_tools || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await tickets.setTechnician(data.technician || 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,
|
|
|
|
customer_name: item.customer_name || null,
|
|
address: item.address || null,
|
|
issue: item.issue || null,
|
|
handled: item.handled || null,
|
|
start: item.start || null,
|
|
end: item.end || null,
|
|
replaced_tools: item.replaced_tools || 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.customer_name !== undefined)
|
|
updatePayload.customer_name = data.customer_name;
|
|
|
|
if (data.address !== undefined) updatePayload.address = data.address;
|
|
|
|
if (data.issue !== undefined) updatePayload.issue = data.issue;
|
|
|
|
if (data.handled !== undefined) updatePayload.handled = data.handled;
|
|
|
|
if (data.start !== undefined) updatePayload.start = data.start;
|
|
|
|
if (data.end !== undefined) updatePayload.end = data.end;
|
|
|
|
if (data.replaced_tools !== undefined)
|
|
updatePayload.replaced_tools = data.replaced_tools;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await tickets.update(updatePayload, { transaction });
|
|
|
|
if (data.technician !== undefined) {
|
|
await tickets.setTechnician(
|
|
data.technician,
|
|
|
|
{ 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.technician = await tickets.getTechnician({
|
|
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: 'technician',
|
|
|
|
where: filter.technician
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.technician
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.technician
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.customer_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'tickets',
|
|
'customer_name',
|
|
filter.customer_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('tickets', 'address', filter.address),
|
|
};
|
|
}
|
|
|
|
if (filter.issue) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('tickets', 'issue', filter.issue),
|
|
};
|
|
}
|
|
|
|
if (filter.handled) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('tickets', 'handled', filter.handled),
|
|
};
|
|
}
|
|
|
|
if (filter.replaced_tools) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'tickets',
|
|
'replaced_tools',
|
|
filter.replaced_tools,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
start: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
end: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
if (filter.startRange) {
|
|
const [start, end] = filter.startRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
start: {
|
|
...where.start,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
start: {
|
|
...where.start,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.endRange) {
|
|
const [start, end] = filter.endRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
end: {
|
|
...where.end,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
end: {
|
|
...where.end,
|
|
[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.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', 'customer_name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.tickets.findAll({
|
|
attributes: ['id', 'customer_name'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['customer_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.customer_name,
|
|
}));
|
|
}
|
|
};
|