589 lines
14 KiB
JavaScript
589 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 ProvisionesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const provisiones = await db.provisiones.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
referencia: data.referencia
|
|
||
|
|
null
|
|
,
|
|
|
|
fecha_inicio: data.fecha_inicio
|
|
||
|
|
null
|
|
,
|
|
|
|
fecha_fin: data.fecha_fin
|
|
||
|
|
null
|
|
,
|
|
|
|
tipo: data.tipo
|
|
||
|
|
null
|
|
,
|
|
|
|
observaciones: data.observaciones
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await provisiones.setEmpleado( data.empleado || null, {
|
|
transaction,
|
|
});
|
|
|
|
await provisiones.setCargo( data.cargo || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.provisiones.getTableName(),
|
|
belongsToColumn: 'documentos',
|
|
belongsToId: provisiones.id,
|
|
},
|
|
data.documentos,
|
|
options,
|
|
);
|
|
|
|
|
|
return provisiones;
|
|
}
|
|
|
|
|
|
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 provisionesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
referencia: item.referencia
|
|
||
|
|
null
|
|
,
|
|
|
|
fecha_inicio: item.fecha_inicio
|
|
||
|
|
null
|
|
,
|
|
|
|
fecha_fin: item.fecha_fin
|
|
||
|
|
null
|
|
,
|
|
|
|
tipo: item.tipo
|
|
||
|
|
null
|
|
,
|
|
|
|
observaciones: item.observaciones
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const provisiones = await db.provisiones.bulkCreate(provisionesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < provisiones.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.provisiones.getTableName(),
|
|
belongsToColumn: 'documentos',
|
|
belongsToId: provisiones[i].id,
|
|
},
|
|
data[i].documentos,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return provisiones;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const provisiones = await db.provisiones.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.referencia !== undefined) updatePayload.referencia = data.referencia;
|
|
|
|
|
|
if (data.fecha_inicio !== undefined) updatePayload.fecha_inicio = data.fecha_inicio;
|
|
|
|
|
|
if (data.fecha_fin !== undefined) updatePayload.fecha_fin = data.fecha_fin;
|
|
|
|
|
|
if (data.tipo !== undefined) updatePayload.tipo = data.tipo;
|
|
|
|
|
|
if (data.observaciones !== undefined) updatePayload.observaciones = data.observaciones;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await provisiones.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.empleado !== undefined) {
|
|
await provisiones.setEmpleado(
|
|
|
|
data.empleado,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.cargo !== undefined) {
|
|
await provisiones.setCargo(
|
|
|
|
data.cargo,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.provisiones.getTableName(),
|
|
belongsToColumn: 'documentos',
|
|
belongsToId: provisiones.id,
|
|
},
|
|
data.documentos,
|
|
options,
|
|
);
|
|
|
|
|
|
return provisiones;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const provisiones = await db.provisiones.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of provisiones) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of provisiones) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return provisiones;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const provisiones = await db.provisiones.findByPk(id, options);
|
|
|
|
await provisiones.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await provisiones.destroy({
|
|
transaction
|
|
});
|
|
|
|
return provisiones;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const provisiones = await db.provisiones.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!provisiones) {
|
|
return provisiones;
|
|
}
|
|
|
|
const output = provisiones.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.empleado = await provisiones.getEmpleado({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.cargo = await provisiones.getCargo({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.documentos = await provisiones.getDocumentos({
|
|
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.empleados,
|
|
as: 'empleado',
|
|
|
|
where: filter.empleado ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.empleado.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
nombre_completo: {
|
|
[Op.or]: filter.empleado.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.cargos,
|
|
as: 'cargo',
|
|
|
|
where: filter.cargo ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.cargo.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
titulo: {
|
|
[Op.or]: filter.cargo.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'documentos',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.referencia) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'provisiones',
|
|
'referencia',
|
|
filter.referencia,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.observaciones) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'provisiones',
|
|
'observaciones',
|
|
filter.observaciones,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
fecha_inicio: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
fecha_fin: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.fecha_inicioRange) {
|
|
const [start, end] = filter.fecha_inicioRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
fecha_inicio: {
|
|
...where.fecha_inicio,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
fecha_inicio: {
|
|
...where.fecha_inicio,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.fecha_finRange) {
|
|
const [start, end] = filter.fecha_finRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
fecha_fin: {
|
|
...where.fecha_fin,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
fecha_fin: {
|
|
...where.fecha_fin,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.tipo) {
|
|
where = {
|
|
...where,
|
|
tipo: filter.tipo,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.provisiones.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(
|
|
'provisiones',
|
|
'referencia',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.provisiones.findAll({
|
|
attributes: [ 'id', 'referencia' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['referencia', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.referencia,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|