509 lines
12 KiB
JavaScript
509 lines
12 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 ReservasDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservas = await db.reservas.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
fecha_inicio: data.fecha_inicio || null,
|
|
fecha_fin: data.fecha_fin || null,
|
|
numero_de_jugadores: data.numero_de_jugadores || null,
|
|
estado: data.estado || null,
|
|
telefono: data.telefono || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await reservas.setJugador(data.jugador || null, {
|
|
transaction,
|
|
});
|
|
|
|
await reservas.setCancha(data.cancha || null, {
|
|
transaction,
|
|
});
|
|
|
|
await reservas.setComplejosdeportivos(data.complejosdeportivos || null, {
|
|
transaction,
|
|
});
|
|
|
|
return reservas;
|
|
}
|
|
|
|
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 reservasData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
fecha_inicio: item.fecha_inicio || null,
|
|
fecha_fin: item.fecha_fin || null,
|
|
numero_de_jugadores: item.numero_de_jugadores || null,
|
|
estado: item.estado || null,
|
|
telefono: item.telefono || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const reservas = await db.reservas.bulkCreate(reservasData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return reservas;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const reservas = await db.reservas.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.fecha_inicio !== undefined)
|
|
updatePayload.fecha_inicio = data.fecha_inicio;
|
|
|
|
if (data.fecha_fin !== undefined) updatePayload.fecha_fin = data.fecha_fin;
|
|
|
|
if (data.numero_de_jugadores !== undefined)
|
|
updatePayload.numero_de_jugadores = data.numero_de_jugadores;
|
|
|
|
if (data.estado !== undefined) updatePayload.estado = data.estado;
|
|
|
|
if (data.telefono !== undefined) updatePayload.telefono = data.telefono;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await reservas.update(updatePayload, { transaction });
|
|
|
|
if (data.jugador !== undefined) {
|
|
await reservas.setJugador(
|
|
data.jugador,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.cancha !== undefined) {
|
|
await reservas.setCancha(
|
|
data.cancha,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.complejosdeportivos !== undefined) {
|
|
await reservas.setComplejosdeportivos(
|
|
data.complejosdeportivos,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return reservas;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservas = await db.reservas.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of reservas) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of reservas) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return reservas;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservas = await db.reservas.findByPk(id, options);
|
|
|
|
await reservas.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await reservas.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return reservas;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservas = await db.reservas.findOne({ where }, { transaction });
|
|
|
|
if (!reservas) {
|
|
return reservas;
|
|
}
|
|
|
|
const output = reservas.get({ plain: true });
|
|
|
|
output.pagos_reserva = await reservas.getPagos_reserva({
|
|
transaction,
|
|
});
|
|
|
|
output.jugador = await reservas.getJugador({
|
|
transaction,
|
|
});
|
|
|
|
output.cancha = await reservas.getCancha({
|
|
transaction,
|
|
});
|
|
|
|
output.complejosdeportivos = await reservas.getComplejosdeportivos({
|
|
transaction,
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(filter, globalAccess, options) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userComplejosdeportivos =
|
|
(user && user.complejosdeportivos?.id) || null;
|
|
|
|
if (userComplejosdeportivos) {
|
|
if (options?.currentUser?.complejosdeportivosId) {
|
|
where.complejosdeportivosId = options.currentUser.complejosdeportivosId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.users,
|
|
as: 'jugador',
|
|
|
|
where: filter.jugador
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.jugador
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.jugador
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.canchas,
|
|
as: 'cancha',
|
|
|
|
where: filter.cancha
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.cancha
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
nombre: {
|
|
[Op.or]: filter.cancha
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.complejosdeportivos,
|
|
as: 'complejosdeportivos',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.telefono) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('reservas', 'telefono', filter.telefono),
|
|
};
|
|
}
|
|
|
|
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.numero_de_jugadoresRange) {
|
|
const [start, end] = filter.numero_de_jugadoresRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
numero_de_jugadores: {
|
|
...where.numero_de_jugadores,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
numero_de_jugadores: {
|
|
...where.numero_de_jugadores,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.estado) {
|
|
where = {
|
|
...where,
|
|
estado: filter.estado,
|
|
};
|
|
}
|
|
|
|
if (filter.complejosdeportivos) {
|
|
const listItems = filter.complejosdeportivos.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
complejosdeportivosId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (globalAccess) {
|
|
delete where.complejosdeportivosId;
|
|
}
|
|
|
|
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.reservas.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,
|
|
globalAccess,
|
|
organizationId,
|
|
) {
|
|
let where = {};
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike('reservas', 'estado', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.reservas.findAll({
|
|
attributes: ['id', 'estado'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['estado', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.estado,
|
|
}));
|
|
}
|
|
};
|