migration23.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import async from "async";
  2. /**
  3. * Migration 23
  4. *
  5. * Migration for renaming default user role from default to user
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const userModel = await MigrationModule.runJob("GET_MODEL", { modelName: "user" }, this);
  12. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. this.log("INFO", `Migration 23. Finding users with document version 3.`);
  17. userModel.find({ documentVersion: 3 }, (err, users) => {
  18. if (err) next(err);
  19. else {
  20. async.eachLimit(
  21. users.map(user => user._doc),
  22. 1,
  23. (user, next) => {
  24. userModel.updateOne(
  25. { _id: user._id },
  26. {
  27. $set: {
  28. role: user.role === "default" ? "user" : user.role,
  29. documentVersion: 4
  30. }
  31. },
  32. next
  33. );
  34. },
  35. err => {
  36. this.log("INFO", `Migration 23. Users found: ${users.length}.`);
  37. next(err);
  38. }
  39. );
  40. }
  41. });
  42. }
  43. ],
  44. err => {
  45. if (err) {
  46. reject(new Error(err));
  47. } else {
  48. resolve();
  49. }
  50. }
  51. );
  52. });
  53. }