migration22.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import async from "async";
  2. /**
  3. * Migration 22
  4. *
  5. * Migration to fix issues in a previous migration (12), where report categories were not turned into lowercase
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const reportModel = await MigrationModule.runJob("GET_MODEL", { modelName: "report" }, this);
  12. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. this.log("INFO", `Migration 22. Finding reports with document version 5.`);
  17. reportModel.find({ documentVersion: 5 }, (err, reports) => {
  18. if (err) next(err);
  19. else {
  20. async.eachLimit(
  21. reports.map(reporti => reporti._doc),
  22. 1,
  23. (reporti, next) => {
  24. const issues = reporti.issues.map(issue => ({
  25. ...issue,
  26. category: issue.category.toLowerCase()
  27. }));
  28. reportModel.updateOne(
  29. { _id: reporti._id },
  30. {
  31. $set: {
  32. documentVersion: 6,
  33. issues
  34. },
  35. $unset: {
  36. description: ""
  37. }
  38. },
  39. next
  40. );
  41. },
  42. err => {
  43. if (err) next(err);
  44. else {
  45. this.log("INFO", `Migration 22. Reports found: ${reports.length}.`);
  46. next();
  47. }
  48. }
  49. );
  50. }
  51. });
  52. }
  53. ],
  54. err => {
  55. if (err) reject(new Error(err));
  56. else resolve();
  57. }
  58. );
  59. });
  60. }