migration9.js 2.0 KB

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