migration13.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import async from "async";
  2. /**
  3. * Migration 13
  4. *
  5. * Migration for allowing titles, descriptions and individual resolving for report issues
  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 13. Finding reports with document version 4.`);
  16. reportModel.find({ documentVersion: 4 }, (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;
  24. issues.forEach(issue => {
  25. issue.title = issue.info;
  26. issue.resolved = reporti.resolved;
  27. delete issue.info;
  28. });
  29. reportModel.updateOne(
  30. { _id: reporti._id },
  31. {
  32. $set: {
  33. documentVersion: 5,
  34. issues
  35. }
  36. },
  37. next
  38. );
  39. },
  40. err => {
  41. this.log("INFO", `Migration 13. Reports found: ${reports.length}.`);
  42. next(err);
  43. }
  44. );
  45. }
  46. });
  47. }
  48. ],
  49. err => {
  50. if (err) reject(new Error(err));
  51. else resolve();
  52. }
  53. );
  54. });
  55. }