Browse Source

refactor: when a playlist is deleted, it's now also removed from included/excluded playlists in stations

Kristian Vos 3 years ago
parent
commit
1a89c9d351
2 changed files with 57 additions and 1 deletions
  1. 11 0
      backend/logic/playlists.js
  2. 46 1
      backend/logic/stations.js

+ 11 - 0
backend/logic/playlists.js

@@ -943,6 +943,17 @@ class _PlaylistsModule extends CoreClass {
 						)
 							.then(() => next())
 							.catch(next);
+					},
+
+					next => {
+						StationsModule.runJob(
+							"REMOVE_INCLUDED_OR_EXCLUDED_PLAYLIST_FROM_STATIONS",
+							{ playlistId: payload.playlistId },
+							this
+						).then(() => {
+							next();
+						})
+						.catch(err => next(err));
 					}
 				],
 				err => {

+ 46 - 1
backend/logic/stations.js

@@ -1439,7 +1439,6 @@ class _StationsModule extends CoreClass {
 	 */
 	REMOVE_EXCLUDED_PLAYLIST(payload) {
 		return new Promise((resolve, reject) => {
-			console.log(112, payload);
 			async.waterfall(
 				[
 					next => {
@@ -1504,6 +1503,52 @@ class _StationsModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Removes included or excluded playlist from a station
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {string} payload.playlistId - the playlist id
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	REMOVE_INCLUDED_OR_EXCLUDED_PLAYLIST_FROM_STATIONS(payload) {
+		return new Promise((resolve, reject) => {
+			async.waterfall(
+				[
+					next => {
+						if (!payload.playlistId) next("Please specify a playlist id");
+						else next();
+					},
+
+					next => {
+						StationsModule.stationModel.updateMany(
+							{
+								$or: [{ includedPlaylists: payload.playlistId }, { excludedPlaylists: payload.playlistId }]
+							},
+							{
+								$pull: {
+									includedPlaylists: payload.playlistId,
+									excludedPlaylists: payload.playlistId
+								}
+							},
+							(err) => {
+								if (err) next(err);
+								else next();
+							}
+						);
+					}
+				],
+				async err => {
+					if (err && err !== true) {
+						err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+						return reject(new Error(err));
+					}
+
+					return resolve();
+				}
+			);
+		});
+	}
+
 	/**
 	 * Gets stations that include or exclude a specific playlist
 	 *