| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 | import async from "async";/** * Migration 2 * * Updates the document version 1 stations to add the includedPlaylists and excludedPlaylists properties, and to create a station playlist and link that playlist with the playlist2 property. * @param {object} MigrationModule - the MigrationModule * @returns {Promise} - returns promise */export default async function migrate(MigrationModule) {	const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);	const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);	return new Promise((resolve, reject) => {		async.waterfall(			[				next => {					this.log("INFO", `Migration 2. Finding stations with document version 1.`);					stationModel.find({ documentVersion: 1 }, (err, stations) => {						this.log("INFO", `Migration 2. Found ${stations.length} stations with document version 1.`);						next(							null,							stations.map(station => station._doc)						);					});				},				(stations, next) => {					async.eachLimit(						stations,						1,						(station, next) => {							this.log("INFO", `Migration 2. Creating station playlist for station ${station._id}.`);							playlistModel.create(								{									isUserModifiable: false,									displayName: `Station - ${station.displayName}`,									songs: [],									createdBy: station.type === "official" ? "Musare" : station.createdBy,									createdFor: `${station._id}`,									createdAt: Date.now(),									type: "station",									documentVersion: 1								},								(err, playlist2) => {									if (err) next(err);									else {										this.log("INFO", `Migration 2. Updating station ${station._id}.`);										stationModel.updateOne(											{ _id: station._id },											{												$set: {													playlist2: playlist2._id,													includedPlaylists: [],													excludedPlaylists: [],													playlist: station.type === "official" ? [] : station.playlist,													genres: [],													documentVersion: 2												}											},											(err, res) => {												if (err) next(err);												else {													this.log(														"INFO",														`Migration 2. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`													);												}											}										);										next();									}								}							);						},						next					);				}			],			(err, response) => {				if (err) {					reject(new Error(err));				} else {					resolve(response);				}			}		);	});}
 |