news.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use strict';
  2. const async = require('async');
  3. const hooks = require('./hooks');
  4. const moduleManager = require("../../index");
  5. const db = moduleManager.modules["db"];
  6. const cache = moduleManager.modules["cache"];
  7. const utils = moduleManager.modules["utils"];
  8. const logger = moduleManager.modules["logger"];
  9. cache.sub('news.create', news => {
  10. utils.socketsFromUser(news.createdBy, sockets => {
  11. sockets.forEach(socket => {
  12. socket.emit('event:admin.news.created', news);
  13. });
  14. });
  15. });
  16. cache.sub('news.remove', news => {
  17. utils.socketsFromUser(news.createdBy, sockets => {
  18. sockets.forEach(socket => {
  19. socket.emit('event:admin.news.removed', news);
  20. });
  21. });
  22. });
  23. cache.sub('news.update', news => {
  24. utils.socketsFromUser(news.createdBy, sockets => {
  25. sockets.forEach(socket => {
  26. socket.emit('event:admin.news.updated', news);
  27. });
  28. });
  29. });
  30. module.exports = {
  31. /**
  32. * Gets all news items
  33. *
  34. * @param {Object} session - the session object automatically added by socket.io
  35. * @param {Function} cb - gets called with the result
  36. */
  37. index: (session, cb) => {
  38. async.waterfall([
  39. (next) => {
  40. db.models.news.find({}).sort({ createdAt: 'desc' }).exec(next);
  41. }
  42. ], async (err, news) => {
  43. if (err) {
  44. err = await utils.getError(err);
  45. logger.error("NEWS_INDEX", `Indexing news failed. "${err}"`);
  46. return cb({status: 'failure', message: err});
  47. }
  48. logger.success("NEWS_INDEX", `Indexing news successful.`, false);
  49. return cb({ status: 'success', data: news });
  50. });
  51. },
  52. /**
  53. * Creates a news item
  54. *
  55. * @param {Object} session - the session object automatically added by socket.io
  56. * @param {Object} data - the object of the news data
  57. * @param {Function} cb - gets called with the result
  58. */
  59. create: hooks.adminRequired((session, data, cb) => {
  60. async.waterfall([
  61. (next) => {
  62. data.createdBy = session.userId;
  63. data.createdAt = Date.now();
  64. db.models.news.create(data, next);
  65. }
  66. ], async (err, news) => {
  67. if (err) {
  68. err = await utils.getError(err);
  69. logger.error("NEWS_CREATE", `Creating news failed. "${err}"`);
  70. return cb({ 'status': 'failure', 'message': err });
  71. }
  72. cache.pub('news.create', news);
  73. logger.success("NEWS_CREATE", `Creating news successful.`);
  74. return cb({ 'status': 'success', 'message': 'Successfully created News' });
  75. });
  76. }),
  77. /**
  78. * Gets the latest news item
  79. *
  80. * @param {Object} session - the session object automatically added by socket.io
  81. * @param {Function} cb - gets called with the result
  82. */
  83. newest: (session, cb) => {
  84. async.waterfall([
  85. (next) => {
  86. db.models.news.findOne({}).sort({ createdAt: 'desc' }).exec(next);
  87. }
  88. ], async (err, news) => {
  89. if (err) {
  90. err = await utils.getError(err);
  91. logger.error("NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
  92. return cb({ 'status': 'failure', 'message': err });
  93. }
  94. logger.success("NEWS_NEWEST", `Successfully got the latest news.`, false);
  95. return cb({ status: 'success', data: news });
  96. });
  97. },
  98. /**
  99. * Removes a news item
  100. *
  101. * @param {Object} session - the session object automatically added by socket.io
  102. * @param {Object} news - the news object
  103. * @param {Function} cb - gets called with the result
  104. */
  105. //TODO Pass in an id, not an object
  106. //TODO Fix this
  107. remove: hooks.adminRequired((session, news, cb) => {
  108. db.models.news.deleteOne({ _id: news._id }, async err => {
  109. if (err) {
  110. err = await utils.getError(err);
  111. logger.error("NEWS_REMOVE", `Removing news "${news._id}" failed for user "${session.userId}". "${err}"`);
  112. return cb({ 'status': 'failure', 'message': err });
  113. } else {
  114. cache.pub('news.remove', news);
  115. logger.success("NEWS_REMOVE", `Removing news "${news._id}" successful by user "${session.userId}".`);
  116. return cb({ 'status': 'success', 'message': 'Successfully removed News' });
  117. }
  118. });
  119. }),
  120. /**
  121. * Removes a news item
  122. *
  123. * @param {Object} session - the session object automatically added by socket.io
  124. * @param {String} _id - the news id
  125. * @param {Object} news - the news object
  126. * @param {Function} cb - gets called with the result
  127. */
  128. //TODO Fix this
  129. update: hooks.adminRequired((session, _id, news, cb) => {
  130. db.models.news.updateOne({ _id }, news, { upsert: true }, async err => {
  131. if (err) {
  132. err = await utils.getError(err);
  133. logger.error("NEWS_UPDATE", `Updating news "${_id}" failed for user "${session.userId}". "${err}"`);
  134. return cb({ 'status': 'failure', 'message': err });
  135. } else {
  136. cache.pub('news.update', news);
  137. logger.success("NEWS_UPDATE", `Updating news "${_id}" successful for user "${session.userId}".`);
  138. return cb({ 'status': 'success', 'message': 'Successfully updated News' });
  139. }
  140. });
  141. }),
  142. };