-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetPipelineByNames.js
46 lines (40 loc) · 1.35 KB
/
getPipelineByNames.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
/**
* Constructs a MongoDB aggregation pipeline to filter items based on names.
*
* @param {string} names - The names to retrieve, as a comma-separated string.
* @param {Array} pipeline - The current MongoDB aggregation pipeline to add the filter to.
* @param {string} key_value - The key in the documents to match against the names (e.g., "platforms_links" or "genres").
* @param {boolean} is_active_item - The active item flag to include in the match condition.
* @returns {Array} - The updated MongoDB aggregation pipeline.
*/
const getPipelineByNames = (names, pipeline, key_value, is_active_item) => {
if (names) {
const decodedNames = decodeURIComponent(names);
const decodedNamesArray = decodedNames.split(",");
if (decodedNamesArray.includes("all")) return pipeline;
let condition;
if (key_value === "platforms_links") {
condition = {
[key_value]: {
$elemMatch: {
name: {
$in: decodedNamesArray,
},
},
},
};
} else if (key_value === "directors" || key_value === "genres") {
condition = {
[key_value]: {
$in: decodedNamesArray,
},
};
}
const match = {
$match: { $and: [is_active_item, condition] },
};
pipeline.push(match);
}
return pipeline;
};
module.exports = { getPipelineByNames };