Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
authUser = null,
sort = 'desc', // asc || desc
search = null
} = {}) {
const query = {};
let dateDirection = -1;
if (createdAt) query.createdAt = { $lt: moment(createdAt).toDate() };
if (sort === 'asc') {
dateDirection = 1;
if (createdAt) query.createdAt = { $gt: moment(createdAt).toDate() };
}
if (search) {
const titleSearch = {};
const searchWords = search.split(' ').join('|');
titleSearch['title.rendered'] = {
$regex: new RegExp(`${searchWords}`, 'i')
};
// @TODO: Add this when content doesn't have so much extra data
// let contentSearch = {}
// contentSearch['content.rendered'] = { $regex: new RegExp(`${search}`, 'i') };
query.$or = [titleSearch];
}
const limitOption = parseInt(limit, 10);
const orderBy = { createdAt: dateDirection };
const queryPromise = this.find(query, 'title content author createdAt')
.populate('author', '-email -password -__v -verified')
.sort(orderBy)
.limit(limitOption);
let numberOfPages = 0; //eslint-disable-line
let dateDirection = -1;
if (createdAtBefore) query.date = { $lt: moment(createdAtBefore).toDate() };
if (createdAfter) {
dateDirection = 1;
query.date = { $gt: moment(createdAfter).toDate() };
}
if (tags.length > 0) query.tags = { $all: tags };
if (categories.length > 0) query.categories = { $all: categories };
if (topic) query.topics = { $in: topic };
if (search) {
const titleSearch = {};
const searchWords = search.split(' ').join('|');
titleSearch['title.rendered'] = {
$regex: new RegExp(`${searchWords}`, 'i')
};
// @TODO: Add this when content doesn't have so much extra data
// let contentSearch = {}
// contentSearch['content.rendered'] = { $regex: new RegExp(`${search}`, 'i') };
query.$or = [titleSearch];
}
if (transcripts === 'true') {
query.transcriptUrl = { $exists: true };
} else if (transcripts === 'false') {
query.transcriptUrl = { $exists: false };
}