migration22.js 1.5 KB

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