migration12.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import async from "async";
  2. /**
  3. * Migration 12
  4. *
  5. * Migration for updated style of reports
  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 12. Finding reports with document version 2.`);
  16. reportModel.find({ documentVersion: 2 }, (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 = [];
  24. if (reporti.description !== "")
  25. issues.push({ category: "custom", info: reporti.description });
  26. reporti.issues.forEach(category =>
  27. category.reasons.forEach(info => issues.push({ category: category.name, info }))
  28. );
  29. reportModel.updateOne(
  30. { _id: reporti._id },
  31. {
  32. $set: {
  33. documentVersion: 4,
  34. issues
  35. },
  36. $unset: {
  37. description: ""
  38. }
  39. },
  40. next
  41. );
  42. },
  43. err => {
  44. if (err) next(err);
  45. else {
  46. this.log("INFO", `Migration 12. Reports found: ${reports.length}.`);
  47. next();
  48. }
  49. }
  50. );
  51. }
  52. });
  53. }
  54. ],
  55. err => {
  56. if (err) reject(new Error(err));
  57. else resolve();
  58. }
  59. );
  60. });
  61. }