478 lines
11 KiB
JavaScript
478 lines
11 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 ReservationsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservations = await db.reservations.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
start_time: data.start_time || null,
|
|
end_time: data.end_time || null,
|
|
customer_name: data.customer_name || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await reservations.setService(data.service || null, {
|
|
transaction,
|
|
});
|
|
|
|
await reservations.setBarber(data.barber || null, {
|
|
transaction,
|
|
});
|
|
|
|
await reservations.setBarbershops(data.barbershops || null, {
|
|
transaction,
|
|
});
|
|
|
|
return reservations;
|
|
}
|
|
|
|
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 reservationsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
start_time: item.start_time || null,
|
|
end_time: item.end_time || null,
|
|
customer_name: item.customer_name || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const reservations = await db.reservations.bulkCreate(reservationsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return reservations;
|
|
}
|
|
|
|
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 reservations = await db.reservations.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.start_time !== undefined)
|
|
updatePayload.start_time = data.start_time;
|
|
|
|
if (data.end_time !== undefined) updatePayload.end_time = data.end_time;
|
|
|
|
if (data.customer_name !== undefined)
|
|
updatePayload.customer_name = data.customer_name;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await reservations.update(updatePayload, { transaction });
|
|
|
|
if (data.service !== undefined) {
|
|
await reservations.setService(
|
|
data.service,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.barber !== undefined) {
|
|
await reservations.setBarber(
|
|
data.barber,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.barbershops !== undefined) {
|
|
await reservations.setBarbershops(
|
|
data.barbershops,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return reservations;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservations = await db.reservations.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of reservations) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of reservations) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return reservations;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservations = await db.reservations.findByPk(id, options);
|
|
|
|
await reservations.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await reservations.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return reservations;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const reservations = await db.reservations.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!reservations) {
|
|
return reservations;
|
|
}
|
|
|
|
const output = reservations.get({ plain: true });
|
|
|
|
output.service = await reservations.getService({
|
|
transaction,
|
|
});
|
|
|
|
output.barber = await reservations.getBarber({
|
|
transaction,
|
|
});
|
|
|
|
output.barbershops = await reservations.getBarbershops({
|
|
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 userBarbershops = (user && user.barbershops?.id) || null;
|
|
|
|
if (userBarbershops) {
|
|
if (options?.currentUser?.barbershopsId) {
|
|
where.barbershopsId = options.currentUser.barbershopsId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.services,
|
|
as: 'service',
|
|
|
|
where: filter.service
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.service
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.service
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'barber',
|
|
|
|
where: filter.barber
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.barber
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.barber
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.barbershops,
|
|
as: 'barbershops',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.customer_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'reservations',
|
|
'customer_name',
|
|
filter.customer_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
start_time: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
end_time: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
if (filter.start_timeRange) {
|
|
const [start, end] = filter.start_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
start_time: {
|
|
...where.start_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
start_time: {
|
|
...where.start_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.end_timeRange) {
|
|
const [start, end] = filter.end_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
end_time: {
|
|
...where.end_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
end_time: {
|
|
...where.end_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.barbershops) {
|
|
const listItems = filter.barbershops.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
barbershopsId: { [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.barbershopsId;
|
|
}
|
|
|
|
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.reservations.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('reservations', 'customer_name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.reservations.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,
|
|
}));
|
|
}
|
|
};
|