38321-vm/backend/src/db/api/tourist_spots.js
2026-02-09 15:34:26 +00:00

723 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 Tourist_spotsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tourist_spots = await db.tourist_spots.create(
{
id: data.id || undefined,
name: data.name
||
null
,
short_description: data.short_description
||
null
,
description: data.description
||
null
,
category: data.category
||
null
,
average_price: data.average_price
||
null
,
price_level: data.price_level
||
null
,
latitude: data.latitude
||
null
,
longitude: data.longitude
||
null
,
address: data.address
||
null
,
website: data.website
||
null
,
opening_hours: data.opening_hours
||
null
,
is_featured: data.is_featured
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await tourist_spots.setRegion( data.region || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.tourist_spots.getTableName(),
belongsToColumn: 'photos',
belongsToId: tourist_spots.id,
},
data.photos,
options,
);
return tourist_spots;
}
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 tourist_spotsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
short_description: item.short_description
||
null
,
description: item.description
||
null
,
category: item.category
||
null
,
average_price: item.average_price
||
null
,
price_level: item.price_level
||
null
,
latitude: item.latitude
||
null
,
longitude: item.longitude
||
null
,
address: item.address
||
null
,
website: item.website
||
null
,
opening_hours: item.opening_hours
||
null
,
is_featured: item.is_featured
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const tourist_spots = await db.tourist_spots.bulkCreate(tourist_spotsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < tourist_spots.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.tourist_spots.getTableName(),
belongsToColumn: 'photos',
belongsToId: tourist_spots[i].id,
},
data[i].photos,
options,
);
}
return tourist_spots;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tourist_spots = await db.tourist_spots.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.short_description !== undefined) updatePayload.short_description = data.short_description;
if (data.description !== undefined) updatePayload.description = data.description;
if (data.category !== undefined) updatePayload.category = data.category;
if (data.average_price !== undefined) updatePayload.average_price = data.average_price;
if (data.price_level !== undefined) updatePayload.price_level = data.price_level;
if (data.latitude !== undefined) updatePayload.latitude = data.latitude;
if (data.longitude !== undefined) updatePayload.longitude = data.longitude;
if (data.address !== undefined) updatePayload.address = data.address;
if (data.website !== undefined) updatePayload.website = data.website;
if (data.opening_hours !== undefined) updatePayload.opening_hours = data.opening_hours;
if (data.is_featured !== undefined) updatePayload.is_featured = data.is_featured;
updatePayload.updatedById = currentUser.id;
await tourist_spots.update(updatePayload, {transaction});
if (data.region !== undefined) {
await tourist_spots.setRegion(
data.region,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.tourist_spots.getTableName(),
belongsToColumn: 'photos',
belongsToId: tourist_spots.id,
},
data.photos,
options,
);
return tourist_spots;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tourist_spots = await db.tourist_spots.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of tourist_spots) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of tourist_spots) {
await record.destroy({transaction});
}
});
return tourist_spots;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tourist_spots = await db.tourist_spots.findByPk(id, options);
await tourist_spots.update({
deletedBy: currentUser.id
}, {
transaction,
});
await tourist_spots.destroy({
transaction
});
return tourist_spots;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const tourist_spots = await db.tourist_spots.findOne(
{ where },
{ transaction },
);
if (!tourist_spots) {
return tourist_spots;
}
const output = tourist_spots.get({plain: true});
output.recommendations_tourist_spot = await tourist_spots.getRecommendations_tourist_spot({
transaction
});
output.reviews_tourist_spot = await tourist_spots.getReviews_tourist_spot({
transaction
});
output.region = await tourist_spots.getRegion({
transaction
});
output.photos = await tourist_spots.getPhotos({
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.regions,
as: 'region',
where: filter.region ? {
[Op.or]: [
{ id: { [Op.in]: filter.region.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.region.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'photos',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'tourist_spots',
'name',
filter.name,
),
};
}
if (filter.short_description) {
where = {
...where,
[Op.and]: Utils.ilike(
'tourist_spots',
'short_description',
filter.short_description,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'tourist_spots',
'description',
filter.description,
),
};
}
if (filter.address) {
where = {
...where,
[Op.and]: Utils.ilike(
'tourist_spots',
'address',
filter.address,
),
};
}
if (filter.website) {
where = {
...where,
[Op.and]: Utils.ilike(
'tourist_spots',
'website',
filter.website,
),
};
}
if (filter.opening_hours) {
where = {
...where,
[Op.and]: Utils.ilike(
'tourist_spots',
'opening_hours',
filter.opening_hours,
),
};
}
if (filter.average_priceRange) {
const [start, end] = filter.average_priceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
average_price: {
...where.average_price,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
average_price: {
...where.average_price,
[Op.lte]: end,
},
};
}
}
if (filter.latitudeRange) {
const [start, end] = filter.latitudeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
latitude: {
...where.latitude,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
latitude: {
...where.latitude,
[Op.lte]: end,
},
};
}
}
if (filter.longitudeRange) {
const [start, end] = filter.longitudeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
longitude: {
...where.longitude,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
longitude: {
...where.longitude,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.category) {
where = {
...where,
category: filter.category,
};
}
if (filter.price_level) {
where = {
...where,
price_level: filter.price_level,
};
}
if (filter.is_featured) {
where = {
...where,
is_featured: filter.is_featured,
};
}
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.tourist_spots.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(
'tourist_spots',
'name',
query,
),
],
};
}
const records = await db.tourist_spots.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};