@@ -7,6 +7,7 @@ const color = require('./color');
77const image = require ( './image' ) ;
88const util = require ( './util' ) ;
99const Playlist = require ( '../models/playlist' ) ;
10+ const Contributor = require ( '../models/contributor' ) ;
1011const Wrapped2020Tracks = require ( '../models/Wrapped2020Tracks' ) ;
1112
1213const trigger = async ( { day, month, year } ) => {
@@ -107,27 +108,110 @@ const trigger = async ({ day, month, year }) => {
107108} ;
108109
109110/**
110- * This method relies on the existence of the `2020Tracks` collection which is created
111- * using a special aggregation found in `scripts/customAggregations/get2020Tracks.js`
112- * Subsequent years can be done by creating the collection, model and adding the mapping
113- * to this function.
114- * @param {String } year
115- * @param {Number } limit
111+ * Used to get the associated model for a particular year's collection of
112+ * tracks.
113+ * @param {string } year
116114 */
117- const getTopArtists = async ( { year, limit } ) => {
115+ const getYearTracksModel = ( { year } ) => {
118116 const years = {
119117 2020 : Wrapped2020Tracks ,
120118 } ;
121119
122120 // confirm it's a year we have data for.
123- if ( Object . keys ( years ) . indexOf ( year ) < 0 ) {
124- return [ ] ;
121+ if ( ! years [ year ] ) {
122+ return null ;
125123 }
126124
127- const model = years [ year ] ;
125+ return years [ year ] ;
126+ } ;
127+
128+ /**
129+ * This method relies on the existence of the year's tracks collection e.g. `2020Tracks`,
130+ * created using an aggregation you can find in `scripts/customAggregations/get2020Tracks.js`
131+ * Subsequent years can be done by creating the collection, model and adding the mapping
132+ * to the `getYearTracksModel` function.
133+ * @param {String } year
134+ * @param {Number } limit
135+ */
136+ const getTopArtists = async ( { year, limit } ) => {
137+ const model = getYearTracksModel ( { year } ) ;
138+
139+ if ( ! model ) return [ ] ;
140+
141+ const agg = [
142+ {
143+ '$project' : {
144+ 'artist_names' : {
145+ '$split' : [
146+ '$artist_names' , ', ' ,
147+ ] ,
148+ } ,
149+ } ,
150+ } , {
151+ '$unwind' : {
152+ 'path' : '$artist_names' ,
153+ 'preserveNullAndEmptyArrays' : false ,
154+ } ,
155+ } , {
156+ '$sortByCount' : '$artist_names' ,
157+ } , {
158+ '$limit' : Number ( limit ) ,
159+ } , {
160+ '$lookup' : {
161+ 'from' : 'artists' ,
162+ 'localField' : '_id' ,
163+ 'foreignField' : 'name' ,
164+ 'as' : 'details' ,
165+ } ,
166+ } , {
167+ '$project' : {
168+ '_id' : {
169+ '$arrayElemAt' : [
170+ '$details._id' , 0 ,
171+ ] ,
172+ } ,
173+ 'name' : '$_id' ,
174+ 'url' : {
175+ '$arrayElemAt' : [
176+ '$details.url' , 0 ,
177+ ] ,
178+ } ,
179+ 'spotifyId' : {
180+ '$arrayElemAt' : [
181+ '$details.spotifyId' , 0 ,
182+ ] ,
183+ } ,
184+ 'tracks' : '$count' ,
185+ } ,
186+ } ,
187+ ] ;
188+ return model . aggregate ( agg ) ;
189+ } ;
190+
191+ /**
192+ * This method relies on the existence of the year's tracks collection e.g. `2020Tracks`,
193+ * created using an aggregation you can find in `scripts/customAggregations/get2020Tracks.js`
194+ * Subsequent years can be done by creating the collection, model and adding the mapping
195+ * to the `getYearTracksModel` function.
196+ * @param {String } year
197+ * @param {ObjectId } contributorId
198+ * @param {Number } limit
199+ */
200+ const getContributorTopArtists = async ( year , contributorId , limit ) => {
201+ const model = getYearTracksModel ( { year } ) ;
202+
203+ if ( ! model ) return [ ] ;
128204
129205 const agg = [
130206 {
207+ '$match' : {
208+ 'contributors' : {
209+ '$in' : [
210+ contributorId ,
211+ ] ,
212+ } ,
213+ } ,
214+ } , {
131215 '$project' : {
132216 'artist_names' : {
133217 '$split' : [
@@ -173,10 +257,15 @@ const getTopArtists = async ({ year, limit }) => {
173257 } ,
174258 } ,
175259 ] ;
260+
176261 return model . aggregate ( agg ) ;
177262} ;
178263
264+ const getContributorByName = async ( { name } ) => Contributor . findOne ( { name } ) ;
265+
179266module . exports = {
180267 trigger,
181268 getTopArtists,
269+ getContributorByName,
270+ getContributorTopArtists,
182271} ;
0 commit comments