migration13.js 1.4 KB

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