-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start refactoring. Implement complicated sorting by rating for artists.
- Loading branch information
Showing
16 changed files
with
579 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { IArtistModel, ArtistModel } from "@app/models/artist.model"; | ||
|
||
type SortTypes = "sort"; | ||
|
||
// default sort by rating (see tests) | ||
// sorting priority: | ||
// rounds | ||
// wins/rounds | ||
// overall_judges | ||
// overall_popular | ||
const getAllArtists = async ( | ||
sort: SortTypes = "sort" | ||
): Promise<IArtistModel[]> => { | ||
const artists = await ArtistModel.aggregate([ | ||
{ | ||
$lookup: { | ||
from: "tracks", | ||
localField: "tracks", | ||
foreignField: "_id", | ||
as: "tracks" | ||
} | ||
}, | ||
{ | ||
$addFields: { | ||
rounds: { | ||
$size: "$tracks" | ||
}, | ||
wins: { | ||
$size: { | ||
$filter: { | ||
input: "$tracks", | ||
as: "track", | ||
cond: { | ||
$or: [ | ||
{ $eq: ["$$track.status", 1] }, | ||
{ $eq: ["$$track.status", 3] } | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
overall_judges_rating: { | ||
$sum: "$tracks.judges_rating" | ||
}, | ||
overall_popular_rating: { | ||
$sum: "$tracks.popular_rating" | ||
} | ||
} | ||
}, | ||
{ | ||
$addFields: { | ||
wins_of_rounds: { | ||
$divide: ["$wins", "$rounds"] | ||
} | ||
} | ||
}, | ||
{ | ||
$sort: { | ||
rounds: -1, | ||
wins_of_rounds: -1, | ||
overall_judges_rating: -1, | ||
overall_popular_rating: -1 | ||
} | ||
} | ||
]); | ||
return artists; | ||
}; | ||
|
||
export default { | ||
getAllArtists | ||
}; |
Oops, something went wrong.