Browse Source

refactor: store replacements in playlists

Kristian Vos 2 years ago
parent
commit
53988ab45f
2 changed files with 13 additions and 1 deletions
  1. 7 0
      backend/logic/db/schemas/playlist.js
  2. 6 1
      backend/logic/playlists.js

+ 7 - 0
backend/logic/db/schemas/playlist.js

@@ -19,5 +19,12 @@ export default {
 	createdFor: { type: String },
 	privacy: { type: String, enum: ["public", "private"], default: "private" },
 	type: { type: String, enum: ["user", "user-liked", "user-disliked", "genre", "station", "admin"], required: true },
+	replacements: [
+		{
+			oldMediaSource: { type: String, required: true },
+			newMediaSource: { type: String, required: true },
+			replacedAt: { type: Date, required: true }
+		}
+	],
 	documentVersion: { type: Number, default: 7, required: true }
 };

+ 6 - 1
backend/logic/playlists.js

@@ -533,6 +533,8 @@ class _PlaylistsModule extends CoreClass {
 						if (!playlist) return next("Playlist not found.");
 						if (playlist.songs.find(song => song.mediaSource === newMediaSource))
 							return next("The new song is already in the playlist.");
+						if (!playlist.songs.find(song => song.mediaSource === oldMediaSource))
+							return next("The old song is not in the playlist.");
 						return next();
 					},
 
@@ -557,7 +559,10 @@ class _PlaylistsModule extends CoreClass {
 					(newSong, next) => {
 						PlaylistsModule.playlistModel.updateOne(
 							{ _id: playlistId, "songs.mediaSource": oldMediaSource },
-							{ $set: { "songs.$": newSong } },
+							{
+								$set: { "songs.$": newSong },
+								$push: { replacements: { oldMediaSource, newMediaSource, replacedAt: new Date() } }
+							},
 							{ runValidators: true },
 							err => {
 								if (err) return next(err);