migration12.js 1.6 KB

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