893 lines
23 KiB
JavaScript
893 lines
23 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 Trip_preferencesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trip_preferences = await db.trip_preferences.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title
|
|
||
|
|
null
|
|
,
|
|
|
|
destination: data.destination
|
|
||
|
|
null
|
|
,
|
|
|
|
start_date: data.start_date
|
|
||
|
|
null
|
|
,
|
|
|
|
end_date: data.end_date
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_total: data.budget_total
|
|
||
|
|
null
|
|
,
|
|
|
|
currency: data.currency
|
|
||
|
|
null
|
|
,
|
|
|
|
travel_style: data.travel_style
|
|
||
|
|
null
|
|
,
|
|
|
|
mobility_level: data.mobility_level
|
|
||
|
|
null
|
|
,
|
|
|
|
group_type: data.group_type
|
|
||
|
|
null
|
|
,
|
|
|
|
adults_count: data.adults_count
|
|
||
|
|
null
|
|
,
|
|
|
|
children_count: data.children_count
|
|
||
|
|
null
|
|
,
|
|
|
|
needs_kid_friendly: data.needs_kid_friendly
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
needs_accessible: data.needs_accessible
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_hot_air_balloon: data.include_hot_air_balloon
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_museums: data.include_museums
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_hiking: data.include_hiking
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_food_experiences: data.include_food_experiences
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_photography_spots: data.include_photography_spots
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
must_see_notes: data.must_see_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
avoid_notes: data.avoid_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await trip_preferences.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return trip_preferences;
|
|
}
|
|
|
|
|
|
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 trip_preferencesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
title: item.title
|
|
||
|
|
null
|
|
,
|
|
|
|
destination: item.destination
|
|
||
|
|
null
|
|
,
|
|
|
|
start_date: item.start_date
|
|
||
|
|
null
|
|
,
|
|
|
|
end_date: item.end_date
|
|
||
|
|
null
|
|
,
|
|
|
|
budget_total: item.budget_total
|
|
||
|
|
null
|
|
,
|
|
|
|
currency: item.currency
|
|
||
|
|
null
|
|
,
|
|
|
|
travel_style: item.travel_style
|
|
||
|
|
null
|
|
,
|
|
|
|
mobility_level: item.mobility_level
|
|
||
|
|
null
|
|
,
|
|
|
|
group_type: item.group_type
|
|
||
|
|
null
|
|
,
|
|
|
|
adults_count: item.adults_count
|
|
||
|
|
null
|
|
,
|
|
|
|
children_count: item.children_count
|
|
||
|
|
null
|
|
,
|
|
|
|
needs_kid_friendly: item.needs_kid_friendly
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
needs_accessible: item.needs_accessible
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_hot_air_balloon: item.include_hot_air_balloon
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_museums: item.include_museums
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_hiking: item.include_hiking
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_food_experiences: item.include_food_experiences
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
include_photography_spots: item.include_photography_spots
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
must_see_notes: item.must_see_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
avoid_notes: item.avoid_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const trip_preferences = await db.trip_preferences.bulkCreate(trip_preferencesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return trip_preferences;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const trip_preferences = await db.trip_preferences.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
|
|
if (data.destination !== undefined) updatePayload.destination = data.destination;
|
|
|
|
|
|
if (data.start_date !== undefined) updatePayload.start_date = data.start_date;
|
|
|
|
|
|
if (data.end_date !== undefined) updatePayload.end_date = data.end_date;
|
|
|
|
|
|
if (data.budget_total !== undefined) updatePayload.budget_total = data.budget_total;
|
|
|
|
|
|
if (data.currency !== undefined) updatePayload.currency = data.currency;
|
|
|
|
|
|
if (data.travel_style !== undefined) updatePayload.travel_style = data.travel_style;
|
|
|
|
|
|
if (data.mobility_level !== undefined) updatePayload.mobility_level = data.mobility_level;
|
|
|
|
|
|
if (data.group_type !== undefined) updatePayload.group_type = data.group_type;
|
|
|
|
|
|
if (data.adults_count !== undefined) updatePayload.adults_count = data.adults_count;
|
|
|
|
|
|
if (data.children_count !== undefined) updatePayload.children_count = data.children_count;
|
|
|
|
|
|
if (data.needs_kid_friendly !== undefined) updatePayload.needs_kid_friendly = data.needs_kid_friendly;
|
|
|
|
|
|
if (data.needs_accessible !== undefined) updatePayload.needs_accessible = data.needs_accessible;
|
|
|
|
|
|
if (data.include_hot_air_balloon !== undefined) updatePayload.include_hot_air_balloon = data.include_hot_air_balloon;
|
|
|
|
|
|
if (data.include_museums !== undefined) updatePayload.include_museums = data.include_museums;
|
|
|
|
|
|
if (data.include_hiking !== undefined) updatePayload.include_hiking = data.include_hiking;
|
|
|
|
|
|
if (data.include_food_experiences !== undefined) updatePayload.include_food_experiences = data.include_food_experiences;
|
|
|
|
|
|
if (data.include_photography_spots !== undefined) updatePayload.include_photography_spots = data.include_photography_spots;
|
|
|
|
|
|
if (data.must_see_notes !== undefined) updatePayload.must_see_notes = data.must_see_notes;
|
|
|
|
|
|
if (data.avoid_notes !== undefined) updatePayload.avoid_notes = data.avoid_notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await trip_preferences.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await trip_preferences.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return trip_preferences;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trip_preferences = await db.trip_preferences.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of trip_preferences) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of trip_preferences) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return trip_preferences;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trip_preferences = await db.trip_preferences.findByPk(id, options);
|
|
|
|
await trip_preferences.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await trip_preferences.destroy({
|
|
transaction
|
|
});
|
|
|
|
return trip_preferences;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trip_preferences = await db.trip_preferences.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!trip_preferences) {
|
|
return trip_preferences;
|
|
}
|
|
|
|
const output = trip_preferences.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.trips_trip_preference = await trip_preferences.getTrips_trip_preference({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await trip_preferences.getUser({
|
|
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: 'user',
|
|
|
|
where: filter.user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trip_preferences',
|
|
'title',
|
|
filter.title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.destination) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trip_preferences',
|
|
'destination',
|
|
filter.destination,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.currency) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trip_preferences',
|
|
'currency',
|
|
filter.currency,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.must_see_notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trip_preferences',
|
|
'must_see_notes',
|
|
filter.must_see_notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.avoid_notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trip_preferences',
|
|
'avoid_notes',
|
|
filter.avoid_notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
start_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
end_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.start_dateRange) {
|
|
const [start, end] = filter.start_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
start_date: {
|
|
...where.start_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
start_date: {
|
|
...where.start_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.end_dateRange) {
|
|
const [start, end] = filter.end_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
end_date: {
|
|
...where.end_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
end_date: {
|
|
...where.end_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.budget_totalRange) {
|
|
const [start, end] = filter.budget_totalRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
budget_total: {
|
|
...where.budget_total,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
budget_total: {
|
|
...where.budget_total,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.adults_countRange) {
|
|
const [start, end] = filter.adults_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
adults_count: {
|
|
...where.adults_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
adults_count: {
|
|
...where.adults_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.children_countRange) {
|
|
const [start, end] = filter.children_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
children_count: {
|
|
...where.children_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
children_count: {
|
|
...where.children_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.travel_style) {
|
|
where = {
|
|
...where,
|
|
travel_style: filter.travel_style,
|
|
};
|
|
}
|
|
|
|
if (filter.mobility_level) {
|
|
where = {
|
|
...where,
|
|
mobility_level: filter.mobility_level,
|
|
};
|
|
}
|
|
|
|
if (filter.group_type) {
|
|
where = {
|
|
...where,
|
|
group_type: filter.group_type,
|
|
};
|
|
}
|
|
|
|
if (filter.needs_kid_friendly) {
|
|
where = {
|
|
...where,
|
|
needs_kid_friendly: filter.needs_kid_friendly,
|
|
};
|
|
}
|
|
|
|
if (filter.needs_accessible) {
|
|
where = {
|
|
...where,
|
|
needs_accessible: filter.needs_accessible,
|
|
};
|
|
}
|
|
|
|
if (filter.include_hot_air_balloon) {
|
|
where = {
|
|
...where,
|
|
include_hot_air_balloon: filter.include_hot_air_balloon,
|
|
};
|
|
}
|
|
|
|
if (filter.include_museums) {
|
|
where = {
|
|
...where,
|
|
include_museums: filter.include_museums,
|
|
};
|
|
}
|
|
|
|
if (filter.include_hiking) {
|
|
where = {
|
|
...where,
|
|
include_hiking: filter.include_hiking,
|
|
};
|
|
}
|
|
|
|
if (filter.include_food_experiences) {
|
|
where = {
|
|
...where,
|
|
include_food_experiences: filter.include_food_experiences,
|
|
};
|
|
}
|
|
|
|
if (filter.include_photography_spots) {
|
|
where = {
|
|
...where,
|
|
include_photography_spots: filter.include_photography_spots,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.trip_preferences.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(
|
|
'trip_preferences',
|
|
'title',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.trip_preferences.findAll({
|
|
attributes: [ 'id', 'title' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.title,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|