migration20.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. /**
  4. * Migration 20
  5. *
  6. * Migration for station overhaul (WIP)
  7. *
  8. * @param {object} MigrationModule - the MigrationModule
  9. * @returns {Promise} - returns promise
  10. */
  11. export default async function migrate(MigrationModule) {
  12. const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
  13. return new Promise((resolve, reject) => {
  14. async.waterfall(
  15. [
  16. next => {
  17. this.log("INFO", `Migration 20. Finding stations with document version 7.`);
  18. stationModel.find(
  19. {
  20. documentVersion: 7
  21. },
  22. (err, stations) => {
  23. if (err) next(err);
  24. else {
  25. async.eachLimit(
  26. stations.map(station => station._doc),
  27. 1,
  28. (station, next) => {
  29. stationModel.updateOne(
  30. { _id: station._id },
  31. {
  32. $unset: {
  33. includedPlaylists: "",
  34. excludedPlaylists: "",
  35. playMode: "",
  36. partyMode: "",
  37. locked: ""
  38. },
  39. $set: {
  40. autofill: {
  41. enabled: !station.partyMode,
  42. playlists: station.includedPlaylists.map(playlist =>
  43. mongoose.Types.ObjectId(playlist)
  44. ),
  45. limit: 30,
  46. mode: station.playMode
  47. },
  48. requests: {
  49. enabled: station.partyMode,
  50. access: station.locked ? "owner" : "user",
  51. limit: 5
  52. },
  53. blacklist: station.excludedPlaylists.map(playlist =>
  54. mongoose.Types.ObjectId(playlist)
  55. ),
  56. documentVersion: 8
  57. }
  58. },
  59. next
  60. );
  61. },
  62. err => {
  63. this.log("INFO", `Migration 20. Stations found: ${stations.length}.`);
  64. next(err);
  65. }
  66. );
  67. }
  68. }
  69. );
  70. }
  71. ],
  72. err => {
  73. if (err) reject(new Error(err));
  74. else resolve();
  75. }
  76. );
  77. });
  78. }