Bladeren bron

Updating genres now includes genre playlists for a station

Kristian Vos 4 jaren geleden
bovenliggende
commit
5693693bd1
3 gewijzigde bestanden met toevoegingen van 107 en 15 verwijderingen
  1. 103 13
      backend/logic/actions/stations.js
  2. 2 2
      backend/logic/db/schemas/station.js
  3. 2 0
      backend/logic/stations.js

+ 103 - 13
backend/logic/actions/stations.js

@@ -1432,25 +1432,115 @@ export default {
 	 * @param cb
 	 */
 	updateGenres: isOwnerRequired(async function updateGenres(session, stationId, newGenres, cb) {
-		const stationModel = await DBModule.runJob(
-			"GET_MODEL",
-			{
-				modelName: "station"
-			},
-			this
-		);
 		async.waterfall(
 			[
 				next => {
-					stationModel.updateOne(
-						{ _id: stationId },
-						{ $set: { genres: newGenres } },
-						{ runValidators: true },
-						next
+					StationsModule.runJob("GET_STATION", { stationId }, this)
+						.then(station => {
+							next(null, station);
+						})
+						.catch(next);
+				},
+
+				(station, next) => {
+					const playlists = [];
+					async.eachLimit(
+						newGenres,
+						1,
+						(genre, next) => {
+							PlaylistsModule.runJob("GET_GENRE_PLAYLIST", { genre, includeSongs: false }, this).then(
+								response => {
+									playlists.push(response.playlist);
+									next();
+								}
+							);
+						},
+						err => {
+							next(
+								err,
+								station,
+								playlists.map(playlist => playlist._id.toString())
+							);
+						}
 					);
 				},
 
-				(res, next) => {
+				(station, playlists, next) => {
+					const playlistsToRemoveFromExcluded = playlists.filter(
+						playlistId => station.excludedPlaylists.indexOf(playlistId) !== -1
+					);
+					console.log(
+						`playlistsToRemoveFromExcluded: ${playlistsToRemoveFromExcluded.length}`,
+						playlistsToRemoveFromExcluded
+					);
+
+					async.eachLimit(
+						playlistsToRemoveFromExcluded,
+						1,
+						(playlistId, next) => {
+							StationsModule.runJob("REMOVE_EXCLUDED_PLAYLIST", { stationId, playlistId }, this)
+								.then(() => {
+									next();
+								})
+								.catch(next);
+						},
+						err => {
+							next(err, station, playlists);
+						}
+					);
+				},
+
+				(station, playlists, next) => {
+					const playlistsToRemoveFromIncluded = station.includedPlaylists.filter(
+						playlistId => playlists.indexOf(playlistId) === -1
+					);
+					console.log(
+						`playlistsToRemoveFromIncluded: ${playlistsToRemoveFromIncluded.length}`,
+						playlistsToRemoveFromIncluded
+					);
+
+					async.eachLimit(
+						playlistsToRemoveFromIncluded,
+						1,
+						(playlistId, next) => {
+							StationsModule.runJob("REMOVE_INCLUDED_PLAYLIST", { stationId, playlistId }, this)
+								.then(() => {
+									next();
+								})
+								.catch(next);
+						},
+						err => {
+							next(err, station, playlists);
+						}
+					);
+				},
+
+				(station, playlists, next) => {
+					const playlistsToAddToIncluded = playlists.filter(
+						playlistId => station.includedPlaylists.indexOf(playlistId) === -1
+					);
+					console.log(
+						`playlistsToAddToIncluded: ${playlistsToAddToIncluded.length}`,
+						playlistsToAddToIncluded
+					);
+
+					async.eachLimit(
+						playlistsToAddToIncluded,
+						1,
+						(playlistId, next) => {
+							StationsModule.runJob("INCLUDE_PLAYLIST", { stationId, playlistId }, this)
+								.then(() => {
+									next();
+								})
+								.catch(next);
+						},
+						err => {
+							next(err);
+						}
+					);
+				},
+
+				next => {
 					StationsModule.runJob("UPDATE_STATION", { stationId }, this)
 						.then(station => next(null, station))
 						.catch(next);

+ 2 - 2
backend/logic/db/schemas/station.js

@@ -47,7 +47,7 @@ export default {
 	privatePlaylist: { type: mongoose.Schema.Types.ObjectId },
 	partyMode: { type: Boolean },
 	theme: { type: String, enum: ["blue", "purple", "teal", "orange"], default: "blue" },
-	includedPlaylists: [{ type: mongoose.Schema.Types.ObjectId }],
-	excludedPlaylists: [{ type: mongoose.Schema.Types.ObjectId }],
+	includedPlaylists: [{ type: String }],
+	excludedPlaylists: [{ type: String }],
 	documentVersion: { type: Number, default: 1, required: true }
 };

+ 2 - 0
backend/logic/stations.js

@@ -1,4 +1,5 @@
 import async from "async";
+import mongoose from "mongoose";
 
 import CoreClass from "../core";
 
@@ -1485,6 +1486,7 @@ class _StationsModule extends CoreClass {
 	 */
 	REMOVE_EXCLUDED_PLAYLIST(payload) {
 		return new Promise((resolve, reject) => {
+			console.log(112, payload);
 			async.waterfall(
 				[
 					next => {