/by-color endpoint for sort all winners by color.

Can also include query parameter includeWines for resolved wine
references.
This commit is contained in:
2021-01-17 17:26:37 +01:00
parent 53780878af
commit e9ece6963e
2 changed files with 85 additions and 7 deletions

View File

@@ -78,8 +78,6 @@ const byDate = date => {
};
const byName = (name, sort = "desc") => {
const populateOptions = { sort: "date" };
return Winner.findOne({ name }, ["name", "wins"])
.sort("-wins.date")
.populate("wins.wine")
@@ -183,9 +181,60 @@ const groupedByDate = (includeWines = false, sort = "desc") => {
return Winner.aggregate(query).then(lotteries => (sort != "asc" ? lotteries : lotteries.reverse()));
};
const byColor = (includeWines = false) => {
const query = [
{
$unwind: "$wins"
},
{
$group: {
_id: "$wins.color",
winners: {
$push: {
_id: "$_id",
name: "$name",
date: "$wins.date",
wine: "$wins.wine"
}
},
count: { $sum: 1 }
}
},
{
$project: {
color: "$_id",
count: "$count",
winners: "$winners"
}
},
{
$sort: {
_id: -1
}
}
];
console.log("includeWines:", includeWines);
console.log("includeWines:", includeWines == true);
if (includeWines) {
query.splice(1, 0, {
$lookup: {
from: "wines",
localField: "wins.wine",
foreignField: "_id",
as: "wins.wine"
}
});
}
return Winner.aggregate(query);
};
module.exports = {
all,
byDate,
latest,
groupedByDate
groupedByDate,
byColor
};