migration23.js 1.2 KB

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