forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchService.js
More file actions
176 lines (155 loc) · 5.8 KB
/
Copy pathsearchService.js
File metadata and controls
176 lines (155 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import Course from "../../models/Course.js";
import Book from "../../models/Book.js";
import User from "../../models/User.js";
import Space from "../../models/Space.js";
import Reel from "../../models/Reel.js";
import logger from "../../config/logger.js";
import { APIError } from "../../middlewares/errorHandler.js";
const escapeRegex = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const getSearchQuery = (q) => {
if (!q) return {};
if (q.length < 3) {
const safeQ = escapeRegex(q);
return {
$or: [
{ title: { $regex: new RegExp(`^${safeQ}`, "i") } },
{ name: { $regex: new RegExp(`^${safeQ}`, "i") } },
{ description: { $regex: new RegExp(`^${safeQ}`, "i") } }
]
};
}
return { $text: { $search: q } };
};
const applyFilters = (baseQuery, filters, allowedFilters) => {
const query = { ...baseQuery };
if (allowedFilters.includes("category") && filters.category) {
query.category = filters.category;
}
if (allowedFilters.includes("price")) {
if (filters.free === "true") {
query.price = 0;
} else {
if (filters.minPrice) query.price = { ...query.price, $gte: Number(filters.minPrice) };
if (filters.maxPrice) query.price = { ...query.price, $lte: Number(filters.maxPrice) };
}
}
if (allowedFilters.includes("rating") && filters.minRating !== undefined) {
query.rating = { $gte: filters.minRating };
}
return query;
};
const getSortOption = (q, sort) => {
let sortOption = {};
if (sort === "price") sortOption.price = 1;
else if (sort === "price_desc") sortOption.price = -1;
else if (sort === "rating") sortOption.rating = -1;
else if (q && q.length >= 3) {
sortOption.score = { $meta: "textScore" };
} else {
sortOption.createdAt = -1;
}
return sortOption;
};
const getProjection = (q, baseProjection) => {
if (q && q.length >= 3) {
return { ...baseProjection, score: { $meta: "textScore" } };
}
return baseProjection;
};
export const searchCollections = async ({ q, type = "all", page = 1, limit = 10, sort, filters = {} }) => {
const normalizedFilters = { ...filters };
if (filters.minRating !== undefined) {
const minRating = Number(filters.minRating);
if (filters.minRating === "" || !Number.isFinite(minRating) || !Number.isInteger(minRating) || minRating < 0 || minRating > 5) {
throw new APIError("minRating must be a whole number between 0 and 5", 400);
}
normalizedFilters.minRating = minRating;
}
const validPage = Math.max(1, Number(page) || 1);
const validLimit = Math.min(100, Math.max(1, Number(limit) || 10));
const skip = (validPage - 1) * validLimit;
const parsedLimit = validLimit;
const searchQuery = getSearchQuery(q);
const sortOption = getSortOption(q, sort);
const results = {};
const pagination = {};
const searchModel = async (Model, queryBase, allowedFilters, projection, typeName) => {
const finalQuery = applyFilters({ ...queryBase, ...searchQuery }, normalizedFilters, allowedFilters);
const finalProjection = getProjection(q, projection);
let dbQuery = Model.find(finalQuery, finalProjection);
if (Object.keys(sortOption).length > 0) {
dbQuery = dbQuery.sort(sortOption);
}
const [items, total] = await Promise.all([
dbQuery.skip(skip).limit(parsedLimit).lean(),
Model.countDocuments(finalQuery)
]);
results[typeName] = items;
pagination[typeName] = {
total,
page: validPage,
limit: parsedLimit,
pages: Math.ceil(total / parsedLimit),
};
};
const tasks = [];
if (type === "all" || type === "courses") {
tasks.push(searchModel(Course, {}, ["category", "price", "rating"], { title: 1, description: 1, price: 1, thumbnail: 1, category: 1, rating: 1, numReviews: 1 }, "courses"));
}
if (type === "all" || type === "books") {
tasks.push(searchModel(Book, {}, ["category", "price", "rating"], { title: 1, description: 1, category: 1, price: 1, image: 1, author: 1, rating: 1, numReviews: 1 }, "books"));
}
if (type === "all" || type === "spaces") {
tasks.push(searchModel(Space, {}, ["category", "price"], { title: 1, description: 1, price: 1, status: 1, eventDate: 1, duration: 1, host: 1, category: 1 }, "spaces"));
}
if (type === "all" || type === "reels") {
tasks.push(searchModel(Reel, {}, [], { description: 1, createdBy: 1 }, "reels"));
}
if (type === "all" || type === "educators") {
tasks.push((async () => {
const educatorsRes = await searchEducators({ q, interest: filters.interest, page, limit });
results.educators = educatorsRes.results;
pagination.educators = educatorsRes.pagination;
})());
}
await Promise.all(tasks);
return { results, pagination };
};
export const searchEducators = async ({ q, interest, page = 1, limit = 10 }) => {
const validPage = Math.max(1, Number(page) || 1);
const validLimit = Math.min(100, Math.max(1, Number(limit) || 10));
const skip = (validPage - 1) * validLimit;
const parsedLimit = validLimit;
let query = { role: "mentor" };
if (q) {
if (q.length < 3) {
const safeQ = escapeRegex(q);
query.$or = [
{ name: { $regex: new RegExp(`^${safeQ}`, "i") } }
];
} else {
query.$text = { $search: q };
}
}
if (interest) {
query.interests = interest;
}
let dbQuery = User.find(query, getProjection(q, { name: 1, avatar: 1, bio: 1, stat: 1, interests: 1 }));
const sortOption = getSortOption(q, "relevance");
if (Object.keys(sortOption).length > 0) {
dbQuery = dbQuery.sort(sortOption);
}
const [educators, total] = await Promise.all([
dbQuery.skip(skip).limit(parsedLimit).lean(),
User.countDocuments(query)
]);
return {
results: educators,
pagination: {
total,
page: validPage,
limit: parsedLimit,
pages: Math.ceil(total / parsedLimit),
}
};
};