-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaggregateData.js
240 lines (218 loc) · 6.64 KB
/
aggregateData.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const { config } = require("./config");
const { getPipelineByNames } = require("./getPipelineByNames");
const { getPipelineFromTVShow } = require("./getPipelineFromTVShow");
const { getPopularityFilters } = require("./getPopularityFilters");
const { getRatingsFilters } = require("./getRatingsFilters");
const { MongoClient, ServerApiVersion } = require("mongodb");
const uri = `mongodb+srv://${config.mongoDbCredentials}${config.mongoDbCredentialsLastPart}`;
const client = new MongoClient(uri, {
serverApi: ServerApiVersion.v1,
});
const database = client.db(config.dbName);
const collectionData = database.collection(config.collectionName);
const aggregateData = async (
critics_rating_details_query,
directors_query,
episodes_details_query,
genres_query,
id_path,
is_active_query,
item_type_query,
limit_query,
minimum_ratings_query,
page_query,
platforms_query,
popularity_filters_query,
ratings_filters_query,
release_date_query,
seasons_number_query,
status_query,
) => {
const critics_rating_details =
critics_rating_details_query === "true" ? true : false;
const directors =
typeof directors_query !== "undefined" && directors_query
? directors_query
: "";
const episodes_details = episodes_details_query === "true" ? true : false;
const genres =
typeof genres_query !== "undefined" && genres_query ? genres_query : "";
const id = isNaN(id_path) ? "" : id_path;
const is_active =
typeof is_active_query !== "undefined" && is_active_query
? is_active_query
: true;
const item_type =
typeof item_type_query !== "undefined" && item_type_query
? item_type_query
: "movie";
const limit = isNaN(limit_query) ? config.limit : limit_query;
const minimum_ratings =
typeof minimum_ratings_query !== "undefined" && minimum_ratings_query
? minimum_ratings_query
: "";
const page = isNaN(page_query) ? config.page : page_query;
const platforms =
typeof platforms_query !== "undefined" && platforms_query
? platforms_query
: "";
const popularity_filters_query_value =
typeof popularity_filters_query !== "undefined" && popularity_filters_query
? popularity_filters_query
: "all";
const popularity_filters = await getPopularityFilters(
popularity_filters_query_value,
);
const ratings_filters_query_value =
typeof ratings_filters_query !== "undefined" && ratings_filters_query
? ratings_filters_query
: "all";
const ratings_filters = await getRatingsFilters(ratings_filters_query_value);
const release_date =
typeof release_date_query !== "undefined" && release_date_query
? release_date_query
: "";
const seasons_number =
typeof seasons_number_query !== "undefined" && seasons_number_query
? seasons_number_query
: "";
const status =
typeof status_query !== "undefined" && status_query ? status_query : "";
const addFields_popularity_and_ratings = {
$addFields: {
popularity_average: { $avg: popularity_filters },
ratings_average: {
$round: [
{
$avg: ratings_filters,
},
1,
],
},
releaseDateAsDate: { $dateFromString: { dateString: "$release_date" } },
sortAvgField: {
$cond: [
{ $eq: [{ $avg: popularity_filters }, null] },
Infinity,
{ $avg: popularity_filters },
],
},
},
};
let is_active_item = {
is_active: is_active === "true" || is_active === true,
};
const is_active_all = { $or: [{ is_active: true }, { is_active: false }] };
is_active_item =
is_active === "true,false" || is_active === "false,true"
? is_active_all
: is_active_item;
let item_type_default = { item_type: item_type };
const item_type_all = {
$or: [{ item_type: "movie" }, { item_type: "tvshow" }],
};
item_type_default =
item_type === "movie,tvshow" || item_type === "tvshow,movie"
? item_type_all
: item_type_default;
const match_id = { $match: { id: id } };
const match_item_type = {
$match: { $and: [item_type_default, is_active_item] },
};
const minimum_ratings_sorted = minimum_ratings.includes(",")
? parseFloat(
minimum_ratings
.split(",")
.map(parseFloat)
.sort((a, b) => a - b)
.join(","),
)
: parseFloat(minimum_ratings);
const matchConditions = [
{
ratings_average: {
$gte: !isNaN(minimum_ratings_sorted)
? minimum_ratings_sorted
: -Infinity,
},
},
];
if (release_date.split(",").includes("new")) {
const sixOrEighteenMonthsAgo = new Date();
item_type === "movie"
? sixOrEighteenMonthsAgo.setMonth(sixOrEighteenMonthsAgo.getMonth() - 6)
: sixOrEighteenMonthsAgo.setMonth(sixOrEighteenMonthsAgo.getMonth() - 18);
matchConditions.push({
releaseDateAsDate: { $gte: sixOrEighteenMonthsAgo },
});
}
const match_min_ratings_and_release_date = {
$match: {
$and: matchConditions,
},
};
const limit_results = { $limit: limit };
const skip_results = { $skip: (page - 1) * limit };
const sort_popularity_and_ratings = {
$sort: {
sortAvgField: 1,
popularity_average: 1,
ratings_average: -1,
title: 1,
},
};
// Dynamically build the remove_keys object based on query parameters
const remove_keys = {
$project: {
releaseDateAsDate: 0,
sortAvgField: 0,
...(critics_rating_details
? {}
: { "allocine.critics_rating_details": 0 }),
...(episodes_details ? {} : { episodes_details: 0 }),
},
};
const facet = {
$facet: {
results: [
addFields_popularity_and_ratings,
match_min_ratings_and_release_date,
sort_popularity_and_ratings,
remove_keys,
skip_results,
limit_results,
],
total_results: [{ $count: "total_results" }],
},
};
const pipeline = [];
if (id) {
pipeline.push(match_id);
} else if (item_type === "tvshow") {
getPipelineFromTVShow(
config,
is_active_item,
item_type,
pipeline,
seasons_number,
status,
);
} else {
pipeline.push(match_item_type);
}
if (!id) {
getPipelineByNames(directors, pipeline, "directors", is_active_item);
getPipelineByNames(genres, pipeline, "genres", is_active_item);
getPipelineByNames(platforms, pipeline, "platforms_links", is_active_item);
}
pipeline.push(facet);
const data = await collectionData.aggregate(pipeline);
const items = await data.toArray();
return {
items: items,
limit: limit,
page: page,
is_active_item: is_active_item,
};
};
module.exports = { aggregateData };