migration9.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. this.log("INFO", `Migration 9. Finding news with document version 1.`);
  17. newsModel.find({ documentVersion: 1 }, (err, news) => {
  18. if (err) next(err);
  19. else {
  20. async.eachLimit(
  21. news.map(newi => newi._doc),
  22. 1,
  23. (newi, next) => {
  24. newi.markdown = `# ${newi.title}\n\n`;
  25. newi.markdown += `## ${newi.description}\n\n`;
  26. if (newi.bugs) {
  27. newi.markdown += `**Bugs:**\n\n${newi.bugs.join(", ")}\n\n`;
  28. }
  29. if (newi.features) {
  30. newi.markdown += `**Features:**\n\n${newi.features.join(", ")}\n\n`;
  31. }
  32. if (newi.improvements) {
  33. newi.markdown += `**Improvements:**\n\n${newi.improvements.join(", ")}\n\n`;
  34. }
  35. if (newi.upcoming) {
  36. newi.markdown += `**Upcoming:**\n\n${newi.upcoming.join(", ")}\n`;
  37. }
  38. newsModel.updateOne(
  39. { _id: newi._id },
  40. {
  41. $set: {
  42. markdown: newi.markdown,
  43. status: "published",
  44. documentVersion: 2
  45. },
  46. $unset: {
  47. description: "",
  48. bugs: "",
  49. features: "",
  50. improvements: "",
  51. upcoming: ""
  52. }
  53. },
  54. next
  55. );
  56. },
  57. err => {
  58. if (err) next(err);
  59. else {
  60. this.log("INFO", `Migration 9. News found: ${news.length}.`);
  61. next();
  62. }
  63. }
  64. );
  65. }
  66. });
  67. }
  68. ],
  69. err => {
  70. if (err) {
  71. reject(new Error(err));
  72. } else {
  73. resolve();
  74. }
  75. }
  76. );
  77. });
  78. }