729 lines
18 KiB
JavaScript
729 lines
18 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 Booking_requestsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const booking_requests = await db.booking_requests.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
guest_full_name: data.guest_full_name
|
|
||
|
|
null
|
|
,
|
|
|
|
guest_email: data.guest_email
|
|
||
|
|
null
|
|
,
|
|
|
|
guest_phone: data.guest_phone
|
|
||
|
|
null
|
|
,
|
|
|
|
check_in: data.check_in
|
|
||
|
|
null
|
|
,
|
|
|
|
check_out: data.check_out
|
|
||
|
|
null
|
|
,
|
|
|
|
adults: data.adults
|
|
||
|
|
null
|
|
,
|
|
|
|
children: data.children
|
|
||
|
|
null
|
|
,
|
|
|
|
special_requests: data.special_requests
|
|
||
|
|
null
|
|
,
|
|
|
|
channel: data.channel
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
requested_at: data.requested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await booking_requests.setRoom_type( data.room_type || null, {
|
|
transaction,
|
|
});
|
|
|
|
await booking_requests.setHandled_by( data.handled_by || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return booking_requests;
|
|
}
|
|
|
|
|
|
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 booking_requestsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
guest_full_name: item.guest_full_name
|
|
||
|
|
null
|
|
,
|
|
|
|
guest_email: item.guest_email
|
|
||
|
|
null
|
|
,
|
|
|
|
guest_phone: item.guest_phone
|
|
||
|
|
null
|
|
,
|
|
|
|
check_in: item.check_in
|
|
||
|
|
null
|
|
,
|
|
|
|
check_out: item.check_out
|
|
||
|
|
null
|
|
,
|
|
|
|
adults: item.adults
|
|
||
|
|
null
|
|
,
|
|
|
|
children: item.children
|
|
||
|
|
null
|
|
,
|
|
|
|
special_requests: item.special_requests
|
|
||
|
|
null
|
|
,
|
|
|
|
channel: item.channel
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
requested_at: item.requested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const booking_requests = await db.booking_requests.bulkCreate(booking_requestsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return booking_requests;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const booking_requests = await db.booking_requests.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.guest_full_name !== undefined) updatePayload.guest_full_name = data.guest_full_name;
|
|
|
|
|
|
if (data.guest_email !== undefined) updatePayload.guest_email = data.guest_email;
|
|
|
|
|
|
if (data.guest_phone !== undefined) updatePayload.guest_phone = data.guest_phone;
|
|
|
|
|
|
if (data.check_in !== undefined) updatePayload.check_in = data.check_in;
|
|
|
|
|
|
if (data.check_out !== undefined) updatePayload.check_out = data.check_out;
|
|
|
|
|
|
if (data.adults !== undefined) updatePayload.adults = data.adults;
|
|
|
|
|
|
if (data.children !== undefined) updatePayload.children = data.children;
|
|
|
|
|
|
if (data.special_requests !== undefined) updatePayload.special_requests = data.special_requests;
|
|
|
|
|
|
if (data.channel !== undefined) updatePayload.channel = data.channel;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await booking_requests.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.room_type !== undefined) {
|
|
await booking_requests.setRoom_type(
|
|
|
|
data.room_type,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.handled_by !== undefined) {
|
|
await booking_requests.setHandled_by(
|
|
|
|
data.handled_by,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return booking_requests;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const booking_requests = await db.booking_requests.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of booking_requests) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of booking_requests) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return booking_requests;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const booking_requests = await db.booking_requests.findByPk(id, options);
|
|
|
|
await booking_requests.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await booking_requests.destroy({
|
|
transaction
|
|
});
|
|
|
|
return booking_requests;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const booking_requests = await db.booking_requests.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!booking_requests) {
|
|
return booking_requests;
|
|
}
|
|
|
|
const output = booking_requests.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.room_type = await booking_requests.getRoom_type({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.handled_by = await booking_requests.getHandled_by({
|
|
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.room_types,
|
|
as: 'room_type',
|
|
|
|
where: filter.room_type ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.room_type.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.room_type.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'handled_by',
|
|
|
|
where: filter.handled_by ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.handled_by.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.handled_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.guest_full_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'booking_requests',
|
|
'guest_full_name',
|
|
filter.guest_full_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.guest_email) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'booking_requests',
|
|
'guest_email',
|
|
filter.guest_email,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.guest_phone) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'booking_requests',
|
|
'guest_phone',
|
|
filter.guest_phone,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.special_requests) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'booking_requests',
|
|
'special_requests',
|
|
filter.special_requests,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
check_in: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
check_out: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.check_inRange) {
|
|
const [start, end] = filter.check_inRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
check_in: {
|
|
...where.check_in,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
check_in: {
|
|
...where.check_in,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.check_outRange) {
|
|
const [start, end] = filter.check_outRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
check_out: {
|
|
...where.check_out,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
check_out: {
|
|
...where.check_out,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.adultsRange) {
|
|
const [start, end] = filter.adultsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
adults: {
|
|
...where.adults,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
adults: {
|
|
...where.adults,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.childrenRange) {
|
|
const [start, end] = filter.childrenRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
children: {
|
|
...where.children,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
children: {
|
|
...where.children,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.requested_atRange) {
|
|
const [start, end] = filter.requested_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
requested_at: {
|
|
...where.requested_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
requested_at: {
|
|
...where.requested_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.channel) {
|
|
where = {
|
|
...where,
|
|
channel: filter.channel,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.booking_requests.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(
|
|
'booking_requests',
|
|
'guest_full_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.booking_requests.findAll({
|
|
attributes: [ 'id', 'guest_full_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['guest_full_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.guest_full_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|