699 lines
17 KiB
JavaScript
699 lines
17 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 RoutesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const routes = await db.routes.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name
|
|
||
|
|
null
|
|
,
|
|
|
|
subtitle: data.subtitle
|
|
||
|
|
null
|
|
,
|
|
|
|
difficulty: data.difficulty
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_minutes: data.estimated_minutes
|
|
||
|
|
null
|
|
,
|
|
|
|
stops_count: data.stops_count
|
|
||
|
|
null
|
|
,
|
|
|
|
summary: data.summary
|
|
||
|
|
null
|
|
,
|
|
|
|
featured: data.featured
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
google_maps_directions_url: data.google_maps_directions_url
|
|
||
|
|
null
|
|
,
|
|
|
|
published: data.published
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await routes.setRegion( data.region || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await routes.setTags(data.tags || [], {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.routes.getTableName(),
|
|
belongsToColumn: 'preview_map_images',
|
|
belongsToId: routes.id,
|
|
},
|
|
data.preview_map_images,
|
|
options,
|
|
);
|
|
|
|
|
|
return routes;
|
|
}
|
|
|
|
|
|
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 routesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name
|
|
||
|
|
null
|
|
,
|
|
|
|
subtitle: item.subtitle
|
|
||
|
|
null
|
|
,
|
|
|
|
difficulty: item.difficulty
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_minutes: item.estimated_minutes
|
|
||
|
|
null
|
|
,
|
|
|
|
stops_count: item.stops_count
|
|
||
|
|
null
|
|
,
|
|
|
|
summary: item.summary
|
|
||
|
|
null
|
|
,
|
|
|
|
featured: item.featured
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
google_maps_directions_url: item.google_maps_directions_url
|
|
||
|
|
null
|
|
,
|
|
|
|
published: item.published
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const routes = await db.routes.bulkCreate(routesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < routes.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.routes.getTableName(),
|
|
belongsToColumn: 'preview_map_images',
|
|
belongsToId: routes[i].id,
|
|
},
|
|
data[i].preview_map_images,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return routes;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const routes = await db.routes.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
|
|
if (data.subtitle !== undefined) updatePayload.subtitle = data.subtitle;
|
|
|
|
|
|
if (data.difficulty !== undefined) updatePayload.difficulty = data.difficulty;
|
|
|
|
|
|
if (data.estimated_minutes !== undefined) updatePayload.estimated_minutes = data.estimated_minutes;
|
|
|
|
|
|
if (data.stops_count !== undefined) updatePayload.stops_count = data.stops_count;
|
|
|
|
|
|
if (data.summary !== undefined) updatePayload.summary = data.summary;
|
|
|
|
|
|
if (data.featured !== undefined) updatePayload.featured = data.featured;
|
|
|
|
|
|
if (data.google_maps_directions_url !== undefined) updatePayload.google_maps_directions_url = data.google_maps_directions_url;
|
|
|
|
|
|
if (data.published !== undefined) updatePayload.published = data.published;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await routes.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.region !== undefined) {
|
|
await routes.setRegion(
|
|
|
|
data.region,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.tags !== undefined) {
|
|
await routes.setTags(data.tags, { transaction });
|
|
}
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.routes.getTableName(),
|
|
belongsToColumn: 'preview_map_images',
|
|
belongsToId: routes.id,
|
|
},
|
|
data.preview_map_images,
|
|
options,
|
|
);
|
|
|
|
|
|
return routes;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const routes = await db.routes.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of routes) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of routes) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return routes;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const routes = await db.routes.findByPk(id, options);
|
|
|
|
await routes.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await routes.destroy({
|
|
transaction
|
|
});
|
|
|
|
return routes;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const routes = await db.routes.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!routes) {
|
|
return routes;
|
|
}
|
|
|
|
const output = routes.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.route_stops_route = await routes.getRoute_stops_route({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.favorites_route = await routes.getFavorites_route({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.reviews_route = await routes.getReviews_route({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.check_ins_route = await routes.getCheck_ins_route({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.share_links_route = await routes.getShare_links_route({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.region = await routes.getRegion({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.preview_map_images = await routes.getPreview_map_images({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.tags = await routes.getTags({
|
|
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.tags,
|
|
as: 'tags',
|
|
required: false,
|
|
},
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'preview_map_images',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'routes',
|
|
'name',
|
|
filter.name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.subtitle) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'routes',
|
|
'subtitle',
|
|
filter.subtitle,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.summary) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'routes',
|
|
'summary',
|
|
filter.summary,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.google_maps_directions_url) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'routes',
|
|
'google_maps_directions_url',
|
|
filter.google_maps_directions_url,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.estimated_minutesRange) {
|
|
const [start, end] = filter.estimated_minutesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_minutes: {
|
|
...where.estimated_minutes,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_minutes: {
|
|
...where.estimated_minutes,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.stops_countRange) {
|
|
const [start, end] = filter.stops_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
stops_count: {
|
|
...where.stops_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
stops_count: {
|
|
...where.stops_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.difficulty) {
|
|
where = {
|
|
...where,
|
|
difficulty: filter.difficulty,
|
|
};
|
|
}
|
|
|
|
if (filter.featured) {
|
|
where = {
|
|
...where,
|
|
featured: filter.featured,
|
|
};
|
|
}
|
|
|
|
if (filter.published) {
|
|
where = {
|
|
...where,
|
|
published: filter.published,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.tags) {
|
|
const searchTerms = filter.tags.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.tags,
|
|
as: 'tags_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
|
|
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.routes.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(
|
|
'routes',
|
|
'name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.routes.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,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|