migration9.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import async from "async";
  2. /**
  3. * Migration 9
  4. *
  5. * Migration for news
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const newsModel = await MigrationModule.runJob("GET_MODEL", { modelName: "news" }, this);
  12. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. // return next("BLOCKED");
  17. this.log("INFO", `Migration 9. Finding news with document version 1.`);
  18. newsModel.find({ documentVersion: 1 }, (err, news) => {
  19. if (err) next(err);
  20. else {
  21. async.eachLimit(
  22. news.map(newi => newi._doc),
  23. 1,
  24. (newi, next) => {
  25. newi.markdown = `# ${newi.title}\n\n`;
  26. newi.markdown += `## ${newi.description}\n\n`;
  27. if (newi.bugs) {
  28. newi.markdown += `**Bugs:**\n\n${newi.bugs.join(", ")}\n\n`;
  29. }
  30. if (newi.features) {
  31. newi.markdown += `**Features:**\n\n${newi.features.join(", ")}\n\n`;
  32. }
  33. if (newi.improvements) {
  34. newi.markdown += `**Improvements:**\n\n${newi.improvements.join(", ")}\n\n`;
  35. }
  36. if (newi.upcoming) {
  37. newi.markdown += `**Upcoming:**\n\n${newi.upcoming.join(", ")}\n`;
  38. }
  39. newsModel.updateOne(
  40. { _id: newi._id },
  41. {
  42. $set: {
  43. markdown: newi.markdown,
  44. status: "published",
  45. documentVersion: 2
  46. },
  47. $unset: {
  48. description: "",
  49. bugs: "",
  50. features: "",
  51. improvements: "",
  52. upcoming: ""
  53. }
  54. },
  55. next
  56. );
  57. },
  58. err => {
  59. if (err) next(err);
  60. else {
  61. this.log("INFO", `Migration 9. News found: ${news.length}.`);
  62. next();
  63. }
  64. }
  65. );
  66. }
  67. });
  68. }
  69. ],
  70. err => {
  71. if (err) {
  72. reject(new Error(err));
  73. } else {
  74. resolve();
  75. }
  76. }
  77. );
  78. });
  79. }